Index

chapter1

chapter2page1

chapter2page2

chapter2page3

chapter2page4

chapter2page5

chapter2page6

chapter2page7

chapter2page8

chapter2page9

chapter2page10

chapter2page11

chapter2page12

chapter2page13

chapter2page14

chapter2page15

chapter2page16

chapter2page17

chapter2page18

chapter2page19

chapter2page20

chapter2page21

chapter2page22

chapter2page23

chapter2page24

chapter2page25

chapter2page26

chapter2page27

chapter2page28

chapter2page30

chapter2page31

chapter2page32

chapter2page33

chapter2page34

chapter3

link one

link one

link one

link one

link one

link one

2.7.4 -Branching According Multiple Choices

When ther is a more than two choices for branching, we can use nested if - else ladders. This is a nested structure and contains as much layers as the existing choices. The flowsheet of this structure is :

If-Else-If Ladder

2.7.4 - Image 1 Flowchart of the If -Else - If Ladder

The program of this flowchart :

package preliminary;

public class IfElseIfLadder {

public static void main(String[] args){
int x = 0;
int u = 1;
int v = 2;
int w = 3;
int y = 4;
int z = 5;
if (x == u) System.out.println("x = u)");
else if (x == v) System.out.println("x = v)");
else if (x == w) System.out.println("x = w)");
else if (x == y) System.out.println("x = w)");
else if (x == z) System.out.println("x = w)");
else System.out.println("No Match Found !!!");

 } // end main

} //end class IfElseIfLadder

The result of this program :

No Match Found !!!

Once one condition is satisfied, the remaining ones are not evaluated. Note that the last else belong to the last if, because this is the only if which is remained not served from any else's.

Although simple in the first sight, if - else if constructs have some drawbacks. We should open deeper if - else if layers with increasing choices for branching and this is not preferred in terms of compiler performance. The program coding becomes difficult when there is more than one statement to be executed per decision step. The codes blocks then may be difficult to follow.

If- else if constructs have been used with the early stages of programming languages. In modern times, switch statement is provided to handle conveniently the branching of the programs depending on multiple choices. Today, problems of branching according multiple choices is almost exclusively handled by more convenient switch statement that we'll introduce in the next section.

<< Index

Polyglott HTML5(XHTML5 compliant HTML5 code)