Download presentation
Presentation is loading. Please wait.
Published byRoberta Edwards Modified over 8 years ago
1
The if-else StatementtMyn1 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.
2
The if-else StatementtMyn2 condition is true? yes no Statement or Block of Statements for true Next Statement or Block of Statements for false Figure 1. One of the two blocks in an if-else statement is always executed. if(condition) { //statements //when condition //is true } else { //statements //when condition //is false } next statement;
3
The if-else StatementtMyn3 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.
4
The if-else StatementtMyn4 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!"); }
5
The if-else StatementtMyn5 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)
6
The if-else StatementtMyn6 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:
7
The if-else StatementtMyn7 if(coffee==‘y’) if(donuts==‘y’) { System.out.println(“We have coffee and donuts.”); … } else System.out.println(“We have coffee, but not donuts.”); … 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. The else belongs to the test for donuts, not the test for coffee!!
8
The if-else StatementtMyn8 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.”); When an if is nested beneath an else, writing ‘else if’ on one line is accepted convention.
9
The if-else StatementtMyn9 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:
10
The if-else StatementtMyn10 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)
11
The if-else StatementtMyn11 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.