Hey Guys,
Today we discussed about IF-ELSE.
First time we input a values.And then i'll check given if condition is true or false.If if condition is true then it print all syntax of if block Otherwise it print else block.
⇒we have two ways of if-else
1. simple if-else
2. if-else ladder
Example1 ↬ Simple if-else
class Easy
{
public static void main(String[] args)
{
int a=10,b=20;
if(a>b)//a greater than b(false)
{
System.out.println("a is greater");
}
else
{
System.out.println("b is greater");
}
}
}
/*
### Output ###
b is greater
*/
Example2 ↬ if-else Ladder
import java.util.Scanner;
class Easy
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
float p; //declaring variable for percent
System.out.println("Enter your percent");
p=in.nextFloat();
if(p>=60)
System.out.println("First div");
else if(p>=45)
System.out.println("Second div");
else if(p>=33)
System.out.println("Third div");
else
System.out.println("Sorry!! you are fail!!");
}
}
/*
### Output ###
Enter your percent
50
Second div
*/
0 Comments
Post a Comment