Java Decision Making
Java Decision Making
Java Decision Making
1) If statement
2) if else
3) else If
4) switch
statement 2; ...
if(b>a)
System.out.println("b is greater");
{
//execute your code
}
else
{
//execute your code
}
Example of a Java Program to Demonstrate If else statements
public class Sample
if (b > a)
{
System.out.println("b is greater");
}
else if(a >b){
System.out.println("a is greater");
}
else {
System.out.println("Both are equal");
}
}
}
Syntax:
switch(variable)
{
case 1:
//execute your code
break;
case n:
//execute your code
break;
default:
//execute your code
break;
}
Example of a Java Program to Demonstrate Switch Statement
public class Sample {
switch (a) {
case 1:
System.out.println("You chose One");
break;
case 2:
System.out.println("You chose Two");
break;
case 3:
System.out.println("You chose Three");
break;
case 4:
System.out.println("You chose Four");
break;
case 5:
System.out.println("You chose Five");
break;
default:
System.out.println("Invalid Choice. Enter a no between 1 and 5");
break;
}
}
}