if (boolean expr)
{
statement 1: //expr true
}
statement 2;
if (boolean expr)
{
statement 1; //expr true
}
else
{
statement 2; //expr false
}
if (boolean expr1)
{
statement 1; // expr1 true
}
else if (boolean expr2)
{
Statement 2; // expr 1 false and expr 2 true
}
else
{
statement 3; // expr 2 false
}
Program to find largest number of three given numbers using if else.
class Test
{
public static void main(String args[])
{
int x = 20;
int y = 15;
int z = 30;
if (x > y)
{
if (x > z)
System.out.println("Largest number is: "+x);
else
System.out.println("Largest number is: "+z);
}
else
{
if (y > z)
System.out.println("Largest number is: "+y);
else
System.out.println("Largest number is: "+z);
}
}
}
class SwitchDemo
{
public static void main(String[] args)
{
int day = 6;
String dayString;
switch (day)
{
case 1: dayString = "Sunday";
break;
case 2: dayString = "Monday";
break;
case 3: dayString = "Tuesday";
break;
case 4: dayString = "Wednesday";
break;
case 5: dayString = "Thursday";
break;
case 6: dayString = "Friday";
break;
case 7: dayString = "Saturday";
break;
default: dayString = "Invalid day";
break;
}
System.out.println (dayString);
}
}