1.27k likes | 4.68k Views
The if-else Statement. It might be that we want to execute a particular statement or block of statements only when the condition is false. The if-else combination provides a choice between two options. The general logic of the if-else is shown in the Figure 1. no. condition is true?.
E N D
The if-else Statement • It might be that we want to execute a particular statement or block of statements only when the condition is false. • The if-else combination provides a choice between two options. • The general logic of the if-else is shown in the Figure 1. tMyn
no condition is true? if(condition) { //statements //when condition //is true } else { //statements //when condition //is false } next statement; yes Statement or Block of Statements for false Statement or Block of Statements for true Next Statement Figure 1. One of the two blocks in an if-else statement is always executed. tMyn
Often you'd want to execute a statement if a certain condition is met, and a different statement if the condition is not met. This is what else is for. else extends an if statement to execute a statement in case the expression in the if statement evaluates to FALSE. tMyn
package ifelsestatement; public class Main { public static void main(String[] args) throws java.io.IOException { char ch, answer='K'; System.out.println("I am thinking of a letter between A and Z."); System.out.print("Can you gess it: "); ch=(char)System.in.read(); if(ch==answer) System.out.println("***RIGHT***"); else System.out.println("Sorry, you did not guess it!"); } } tMyn
run: I am thinking of a letter between A and Z. Can you gess it: T Sorry, you did not guess it! BUILD SUCCESSFUL (total time: 10 seconds) tMyn
It is also possible to nest if-else statements within ifs, ifs within if-else statements, and indeed if-else statements within other if-else statements. • This provides us with plenty of versatility and considerable room for confusion. • Next an example of an if-else nested within an if: tMyn
The else belongs to the test for donuts, not the test for coffee!! An else ALWAYS belongs to the nearest preceding if that is not already spoken for by another else. The potential for confusion here is known as the dangling else problem. if(coffee==‘y’) if(donuts==‘y’) { System.out.println(“We have coffee and donuts.”); … } else System.out.println(“We have coffee, but not donuts.”); … tMyn
When an if is nested beneath an else, writing ‘else if’ on one line is accepted convention. • An example of an if nested within an if-else: if(coffee==‘y’) { if(donuts==‘y’) System.out.println(“We have coffee and donuts.”); } else if(tea==‘y’) System.out.println(“We have no coffee, but we have tea.”); tMyn
else if, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the else if conditional expression evaluates to TRUE. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b: tMyn
package elseifstatement; public class Main { public static void main(String[] args) { int a=5, b=5; if(a>b) System.out.println("a is bigger than b."); else if(a==b) System.out.println("a is equal to b."); else System.out.println("b is bigger than a."); } } run: a is equal to b. BUILD SUCCESSFUL (total time: 1 second) tMyn
There may be several else ifs within the same if statement. The first else if expression (if any) that evaluates to TRUE would be executed. • The else if statement is only executed if the preceding if expression and any preceding else if expressions evaluated to FALSE, and the current else if expression evaluated to TRUE. tMyn