FAQ: Conditional Operators - Combining Conditional Operators

This community-built FAQ covers the “Combining Conditional Operators” exercise from the lesson “Conditional Operators”.

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

Build Basic Android Apps with Java

Learn Java

FAQs on the exercise Combining Conditional Operators

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 #get-help.

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

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

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 #community:Codecademy-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!

Never mind, I just got it. I was too busy getting scared off by the math aspect that I forgot to evaluate the statements for what they actually were solving if they are true or false. Followed the order, reminded myself of what && and || actually entails and voila!

Exercise Instructions: Using your understanding of the order of execution, find out whether the value of each expression is true or false .

When you’re ready, uncomment the print statements to find out if you are right.

I am getting stuck here. I have run both sides and the left evaluates to true and the right evaluates to false. true && false evaluates to false but ex1 prints true. what am I missing?

[codebyte]
public class Operators {

public static void main(String args) {

int a = 6;

int b = 3;

boolean ex1 = !(a == 7 && (b >= a || a != a));

// !(a == 7); evaluates true

//b >= a || a != a; evaluates false

System.out.println(ex1);

[codebyte]

1 Like

I broke it down here that helped me understand ex 1:

int a = 6;

int b = 3;

boolean ex1 = !(a == 7 && (b >= a || a != a));
!(6 == 7 && (3>= 6 || 6 != 6));

Order of operation: parentheses first:

  1. (3>= 6 || 6 != 6)
    3>=6 is false
    6 != 6 is false

Now it looks like this:
(false || false)
Answer is false

How our answer looks with the rest:
!(6 == 7 && false);

Order of operation parentheses first:
(6 == 7 && false)

  1. 6 ==7 false

Now it looks like:
(false && false) is false

now we have:
!false

our answer is true.

Hope this helps!

1 Like

Thanks, I sent it to a friend because I couldn’t wait days for a reply! That is the one frustrating thing about codecademy projects. Appreciate you took the time. I hope it helps someone ~ :grinning:

1 Like

Can anyone provide rationale as to why the highlighted block is evaluating to true?

I’d like to know too :confused:

!(b > 3);
is what we focus on first here, according to our order of operations. If we plug in our int value b = 3, we have

!(3 > 3)
we know 3 is not greater than 3, they are equal. expression within parentheses will be false.

!(false)
the ! operator gives us the opposite value of what’s in the parentheses
boolean ex2 = a == b || true;

We know that operand || means OR, so we do not need to evaluate the other portion of the expression. The whole expression is now true.

Here’s how I worked through the expressions in this exercise. Hopefully it helps. :grinning:

  1. Added a multi-line comment before uncommenting the println() method.
  2. Copied and pasted the expression into the comment.
  3. On a new line in the comment, replaced the variable names with their values.
  4. Worked through the order of execution and replaced the evaluations with their boolean value (on a new line each time).

This helped me visualize what the compiler is doing.
Here is my “work” for the second expression.

boolean ex2 = a == b || !(b > 3);
/*
     a == b || !(b > 3)
     6 == 3 || !(3 > 3)
     6 == 3 || !(false)
     6 == 3 || true
     false || true
     true
*/
// System.out.println(ex2);

true is the answer because || will evaluate to true if either one of the booleans is true.

can some one help me understand the code below
boolean ex3 = !(b <= a && b != a + b);
boolean ex2 = a == b || !(b > 3);

The exercise lists the order of precedence for the conditional operators.

For a more complete list of operator precedence, see: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

int a = 6;
int b = 3;

boolean ex2 = a == b || !(b > 3);
boolean ex3 = !(b <= a && b != a + b);
// ex2
a == b || !(b > 3)
// Parenthesis has higher precedence.
a == b || !(3 > 3)
a == b || !(false)
a == b || true 
// Equality == has higher precedence than logical OR ||  
// See the operator precedence table in link.
6 == 3 || true
false || true
true 
// ex3
!(b <= a && b != a + b)
// Parentheses has higher precedence.
// Of the operators inside the parentheses, + has the highest precedence
!(b <= a && b != 6 + 3)
!(b <= a && b != 9)
// <= has higher precedence than && and !=
!(3 <= 6 && b != 9)
!(true && b != 9)
// != has higher precedence than &&
!(true && 3 != 9)
!(true && true)
!(true)
false

thank you really help full

1 Like