1 / 11

The if-else Statement

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?.

macha
Download Presentation

The if-else Statement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

More Related