The Nested if-else statements are used where we want to make series of
decision making. Thus one if-else is nested within another if-else.
Therefore nested if-else give us to make a decision within a decision
already taken. Lets look at the syntax of nested if-else statements
SYNTAX:
if ( boolean -expression 1 ) { if ( boolean -expression 2 ) { statement 1 ; } else { statement 2 ; } } else { statement 3 ; } rest-of-code; FLOW CHART:
EXAMPLE PROGRAM:
class nested
{
public static void main(String args[])
{
char grade = ‘A’;
if ( grade == ‘A’)
System.out.println( “Excellent student”);
else
if ( grade == ‘B’ )
System.out.println( “Good student”);
else
if ( grade == ‘C’ )
System.out.println( “Average student”);
else
if ( grade == ‘D’ )
System.out.println( “Bad student”);
else System.out.println(“Failed student”);
}
}
|
No comments:
Post a Comment