Are the multiple if and else-if statements the same for mutually exclusive conditions?

Is there a difference between writing multiple if and if-else-if ?

When I tried to write a program with several if , it did not produce the expected results, but it worked with if-else-if .

The conditions were mutually exclusive.

+5
source share
5 answers

When you write several if statements, it is possible that more than one of them will be evaluated as true, since the statements are independent of each other.

When you write a single if else-if else-if ... else, only one condition can be evaluated as true (after the first condition that evaluates to true is found, the following else-if conditions are skipped).

You can make several if statements that behave like an if else-if .. else single if each of the condition blocks exits a block that contains if statements (for example, returning from a method or breaking a loop).

For instance:

 public void foo (int x) { if (x>5) { ... return; } if (x>7) { ... return; } } 

Will have the same behavior as:

 public void foo (int x) { if (x>5) { ... } else if (x>7) { ... } } 

But without return statements, this will have a different behavior when x> 5 and x> 7 are both true.

+10
source

Both are not the same. if statements will check all conditions. If you write several if statements, it will check each condition. If else checks the conditions until they are satisfied. Once, if / else if is executed, it will be outside this block.

0
source

Yes, that matters: see the if-then and if-then-else statements .

In addition, you can easily test it.

Code No. 1:

  int someValue = 10; if(someValue > 0){ System.out.println("someValue > 0"); } if(someValue > 5){ System.out.println("someValue > 5"); } 

It will display:

 someValue > 0 someValue > 5 

So far, code # 2:

  int someValue = 10; if(someValue > 0){ System.out.println("someValue > 0"); }else if(someValue > 5){ System.out.println("someValue > 5"); } 

Only:

 someValue > 0 

As you can see, code # 2 never goes to the second block, since the first statement (someValue> 0) evaluates to true .

0
source
 if() { stmt.. } else { stmt } if() { stmt } here compiler will check for both the if condition. 

Below, the compiler fragmentation checks the if conditions as soon as the first if the condition is true, if the condition is bypassed.

  if(){ } else if { } else if { } else if { } 
0
source

https://www.teeztareen.com/2019/06/nested-if-statement-in-java-programming.html "> nested if statement in Java programming

Nested if statement in a java program. The if statement inside the if statement is called if a control is nested in the nested Statement.in structure only if the external condition is true. Only one statement block is executed, and the remaining blocks are skipped automatically. The user can use many if statements inside another if statement as necessary. Increasing the nesting level increases the complexity of the nested if statement. The syntax of a nested if statement. If (condition) If (condition) {If operator (s)} Else {operator (s)} Else {operator (s)} Operation of the nested if statement. In a nested if statement, the external condition is the if condition, which is evaluated first. If this is true, the control is included in the if if.if internal condition, if the condition is false, the internal if condition is skipped, and the control directly moves to the rest of the external if. If the external if condition is true, then the control goes into the internal if statement. the internal if statement is printed according to a simple if statement. Flowchart The block diagram of the nested if statement is as follows; https://www.teeztareen.com/2019/06/nested-if-statement-in-java-programming.html "> nested if statement in Java programming

0
source

All Articles