Monday, September 7, 2015

Nested if…else Statements in java

A program can test multiple cases by placing ifelse statements inside other ifelse

statements to create nested ifelse statements. For example, the following pseudocode

represents a nested ifelse



If student’s grade is greater than or equal to 90


Print “A”


else


If student’s grade is greater than or equal to 80


Print “B”

else

If student’s grade is greater than or equal to 70

Print “C”

else

If student’s grade is greater than or equal to 60

Print “D”

else

Print “F”
 
 
 
This pseudocode may be written in Java as

 

public class Apples {

public static void main(String[] args){

int studentGrade = 91;



if ( studentGrade > 90 )

System.out.println( "he got an A" );



else if (studentGrade > 80 )

System.out.println(" he got B ");

else if (studentGrade > 75 )

System.out.println(" he got C ");

else if (studentGrade > 60 )

System.out.println(" he got D ");

else

System.out.println( "Failed" );


}

}


 

1 comment:

  1. Good class but I have a suggestion ... on the colored words could you use a lighter color as it is hard to identify the words on the darker colors . Thank you !!!

    ReplyDelete