FAQ: Logical Operators - Logical Operator: &&

This community-built FAQ covers the “Logical Operator: &&” exercise from the lesson “Logical Operators”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise Logical Operator: &&

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why is hunger and anger "int"s, not "bool"s? Also, is there any reason why you would want to use “&&” instead of the more intuitive “and”? Same goes for “II” and “!” with regards to “or” and “not”.

4 Likes

Same question - why are hunger and anger designated as "int"s and not "bool"s in lines 5 & 6 of the code? Good question, @shampoomactavish!

This is a 5 months old question, should this be answered already because I’d also like to know that answer to this question as well.

Why is hunger and anger "int"s, not "bool"s?

Bool was introduced in c++ in 1998.Most people was no aware of it . Hence int were been used; before 1999 people use 0 as false and 1 as true .So the tradition was followed and it can also adjust with int. typing fatigue makes this thing easier for people.Sorry for my english.
Thank you.

#include
int main() {
int hunger = 1;

int anger = 1;

// Write the code below:

if (hunger && anger) {
std::cout<< “Hangry”;
}
}

3 Likes
#include <iostream>

int main()
{
    int hunger = 1;

    int anger = 1;

    // Write the code below:

    if (hunger && anger)
    {
        std::cout << "Hangry";
    }
}

Missing include.

Yes, I missed it .
thank you.

hey everyone i didn’t get this if (hunger && anger) where is the condition is this mean hunger =1 and anger =1 ???

In C++, any expression that evaluates to 0 is equivalent to false, whereas all non-zero expressions are equivalent to true.

The following declarations and initialization were made before the if statement:

int hunger = true;
int anger = true;

So, hunger and anger will both be 1 and the condition
if (hunger && anger)
will evaluate to true.

1 Like

Maybe we’ll get to this in the future, but why does the main function seemed to be initialized as an integer?

Exercise explicitly told us to output “Hangry” but the solution needs to output “Hangry!”