Decision Structure in Java (If, Else if, Nested If, Else if Ladder) for GSEB-XII Students.
// Program-1
public class DemoIf
{
public static void main(String args[])
{
int marks;
String result;
marks=20;
if(marks>=35)
result="Pass";
else
result="Fail";
System.out.println("Your Marks are: "+marks);
System.out.println("Your Result is : "+result);
}
}
// Program-2
public class DemoNestedIf
{
public static void main(String args[])
{
int n1, n2, n3, max;
n1=185;
n2=196;
n3=120;
if(n1>n2)
{
if(n1>n3)
max=n1;
else
max=n3;
}
else
{
if(n2>n3) max=n2;
else max=n3;
}
System.out.println("Number-1: "+n1);
System.out.println("Number-2: "+n2);
System.out.println("Number-3: "+n3);
System.out.println("========== ");
System.out.println("Maximum: "+max);
}
}
//Program-3
public class WeekName
{
public static void main(String args[])
{
int n;
n=3;
if(n==1)
System.out.println("Day-"+n+" is Monday");
else if(n==2)
System.out.println("Day-"+n+" is Tuesday");
else if(n==3)
System.out.println("Day-"+n+" is Wednesday");
else if(n==4)
System.out.println("Day-"+n+" is Thursday");
else if(n==5)
System.out.println("Day-"+n+" is Friday");
else if(n==6)
System.out.println("Day-"+n+" is Saturday");
else if(n==7)
System.out.println("Day-"+n+" is Sunday");
else
System.out.println("Number should be between 1 and 7");
}
}
//Program-4
public class Grades
{
public static void main(String args[])
{
float p; //p for percentage
p=105;
System.out.println("Your Percentage is: "+p);
if(p>90 && p<=100)
System.out.println("Grade - A1");
else if(p>80 && p<=90)
System.out.println("Grade - A2");
else if(p>70 && p<=80)
System.out.println("Grade - B1");
else if(p>60 && p<=70)
System.out.println("Grade - B2");
else if(p>50 && p<=60)
System.out.println("Grade - C1");
else if(p>40 && p<=50)
System.out.println("Grade - C2");
else if(p>=33 && p<=40)
System.out.println("Grade - D");
else if(p>20 && p<33)
System.out.println("Grade - E1");
else if(p>=0 && p<=20)
System.out.println("Grade - E2");
else
System.out.println("Wrong Value...");
}
}
Watch Video
No comments
Post your comments