3. Conditional Statement if else if
- This we can use to choose a option from more than two possibilities.
Program scenario:
if grade is greater than or equal to 90
print “A”
else if grade is greater than or equal to 80
print “B”
else if grade is greater than or equal to 70
print “C”
else if grade is greater than or equal to 60
print “D”
else
print “F”
--------------------------------------------------------------------------------
Program : Basic program on if else if statement
Program name : IfElseIfDemo1.java
Output :
C
class IfElseIfDemo1
{
public static void main(String args[])
{
int grade = 70;
if (grade >= 90)
{
System.out.println(“A”)
}
else if(grade >= 80)
{
System.out.println(“B”);
}
else if(grade >= 70)
{
System.out.println(“C”);
}
else if(grade >= 60)
{
System.out.println(“D”);
}
else
{
System.out.println(“F”);
}
}
}
Compile : javac IfElseIfDemo1.java
Run : java IfElseIfDemo1
Output :
C
--------------------------------------------------------------------------------
summary:
problem - one solution or nothing - if
problem - either one solution or another solution - if else
problem - where there are three or more possibilities - if, else if
--------------------------------------------------------------------------------
Lab Session
Examples for practice:
print “Enter a Teacher came to school: ”
if teacher came
print "go to school"
if teacher not came
print “No school, be happy!”
--------------------------------------------------------------------------------
Thanks for your time.
- Nireekshan