Skip to main content
ICT
Lesson A8 - Control Structures
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

I. Nested if-else Statements page 11 of 17

  1. The statement inside of an if or else option can be another if-else statement. Placing an if-else inside another is known as nested if-else constructions. For example:

    if (expression1){
      if (expression2){
        statement1;
      }else{
        statement2;
      }
    }else{
      statement3;
    }

  2. Here, your braces will need to be correct to ensure that the ifs and elses get paired with their partners.

  3. The above example has three possible different outcomes as shown in the following chart:

  4. Technically, braces are not needed for if and if-else structures if you only want one statement to execute. However, caution must be shown when using else statements inside of nested if-else structures. For example:

    if (expression1)
      if (expression2)
        statement1;
    else
      statement2;

    Indentation is ignored by the compiler, hence it will pair the else statement with the inner if. If you want the else to get paired with the outer if as the indentation indicates, you need to add braces:

    if (expression1){
      if (expression2)
        statement1;
    }else
      statement2;

    The braces allow the else statement to be paired with the outer if.

    Important Concept However, if you always use braces when writing if and if-else statements, you will never have this problem.

  5. Another alternative to the example in Section 4 makes use of the && operator. A pair of nested if statements can be coded as a single compound && statement. Both of these blocks of code would have the exact same effect, but the second one is slightly easier to read.

  6. if(expression1){
      if(expression2){
        statement1;
      }
    }

    //or...

    if (expression1 && expression2){
      statement1;
    }

    The second block of code makes the conditions clearer to another programmer.

  7. Consider the following example of determining the type of triangle given the three sides A, B, and C.

    if ( (A == B) && (B == C) )
      System.out.println("Equilateral triangle");
    else if ( (A == B) || (B == C) || (A == C) )
      System.out.println("Isosceles triangle");
    else
      System.out.println("Scalene triangle");

    If an equilateral triangle is encountered, the rest of the code is ignored. This can help to reduce the execution time of a program.

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.