Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two conditions in one if statement does the second matter if the first is false?

Okay, so I have this piece of code tested and I found there isn't any exception thrown out.

public static void main(String[] args) {     int[] list = {1,2};     if (list.length>2 && list[3] == 2){         System.out.println(list[1]);     } } 

Does the statement here

if (list.length>2 && list[3] == 2) 

mean that if the first condition is false we don't even have to check the second condition?

Or it equals to

if (list.length>2){     if (list[3] == 2){         ...     } } 

?

And, what if it is written in C or python or some other languages?

Thanks

like image 345
albusshin Avatar asked May 17 '13 09:05

albusshin


People also ask

Does the order of conditions in an if statement matter?

Yes, the order of the conditions matters. In your code, you test if(a==b) first. If all 3 integers are the same, then this will be true and only return c; will execute. It won't even bother with the rest of the conditions.

Which operator does not check 2nd condition if 1st condition is false?

This is called lazy evaluation. In order to make it greedy, use the single ampersand logical operator. In that case, it will also evaluate the second conditional, even if the first turns out to be false.

How do you handle multiple if conditions?

Use two if statements if both if statement conditions could be true at the same time. In this example, both conditions can be true. You can pass and do great at the same time. Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false.

What happens when an if statement condition is found to be not true?

If the conditional is not true then the line of code immediately after the else statement will be executed and then "flow of control" will pass to the next statement following the if-else statement.


2 Answers

It is common for languages (Java and Python are among them) to evaluate the first argument of a logical AND and finish evaluation of the statement if the first argument is false. This is because:

From The Order of Evaluation of Logic Operators,

When Java evaluates the expression d = b && c;, it first checks whether b is true. Here b is false, so b && c must be false regardless of whether c is or is not true, so Java doesn't bother checking the value of c.

This is known as short-circuit evaluation, and is also referred to in the Java docs.

It is common to see list.count > 0 && list[0] == "Something" to check a list element, if it exists.


It is also worth mentioning that if (list.length>2 && list[3] == 2) is not equal to the second case

if (list.length>2){     if (list[3] == 2){         ...     } } 

if there is an else afterwards. The else will apply only to the if statement to which it is attached.

To demonstrate this gotcha:

if (x.kind.equals("Human")) {     if (x.name.equals("Jordan")) {         System.out.println("Hello Jordan!");     } } else {     System.out.println("You are not a human!"); } 

will work as expected, but

if (x.kind.equals("Human") && x.name.equals("Jordan")) {     System.out.println("Hello Jordan!"); } else {     System.out.println("You are not a human!"); } 

will also tell any Human who isn't Jordan they are not human.

like image 60
Elle Avatar answered Sep 18 '22 20:09

Elle


Yes. If the first condition is not satisfied then the remainder are not evaluated. This is known as short-circuiting. See here for more details. Note that this isn't particular to Java and lot of other languages will do the same.

like image 26
Brian Agnew Avatar answered Sep 19 '22 20:09

Brian Agnew