Decision Making statements
&
Switch cases
Decision Making statements
• if statements
• switch statement
if statement :
syntax:
if(condition) {
statement 1; //executes when condition is true
}
• If the Boolean expression evaluates to true then the block of code
inside the if statement will be executed.
• If not, the first set of code after the end of the if statement (after the
closing curly brace) will be executed.
Example
public class Test {
public static void main(String[] args) {
int x = 10;
if( x < 20 ) {
System.out.print("inside loop");
}
}
}
Else statement :
syntax:
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Executes the if block if condition is true otherwise else block is
executed.
Example
public class Test {
public static void main(String[] args) {
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
}
}
else if Statement
syntax:
if(condition 1) {
statement 1; //executes when condition 1 is true
}
else if(condition 2) {
statement 2; //executes when condition 2 is true
}
else {
statement 2; //executes when all the conditions are false
}
executes one condition from multiple statements
Example
public class Test {
public static void main(String[] args) {
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
} else if (time < 20) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
}
}
Nested if-statement :
syntax:
if(condition 1) {
statement 1; //executes when condition 1 is true
if(condition 2) {
statement 2; //executes when condition 2 is true
}
else{
statement 2; //executes when condition 2 is false
}
}
• if block within another if block.
• inner if block condition executes only when outer if block condition is
true.
Example
public class Test {
public static void main(String[] args) {
Double n1 = -1.0, n2 = 4.5, n3 = -5.3, largest;
if (n1 >= n2) {
// if...else statement inside the if block
// checks if n1 is greater than or equal to n3
if (n1 >= n3) {
largest = n1;
}
else {
largest = n3;
}
} else {
// if..else statement inside else block
// checks if n2 is greater than or equal to n3
if (n2 >= n3) {
largest = n2;
}
else {
largest = n3;
}
}
System.out.println("Largest Number: " + largest);}
}
Switch case
• execute a block of code among many alternatives
• Similar to if-else-if ladder statement
• Instead of writing many if..else statements, you can use the switch
statement
syntax
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
Example:
public class Test {
public static void main(String[] args) {
//Declaring a variable for switch expression
int number=10;
//Switch expression
switch(number){
//Case statements
case 10: System.out.println("10");
break;
case 20: System.out.println("20");
break;
case 30: System.out.println("30");
break;
//Default case statement
default:System.out.println("Not in 10, 20 or 30");
}
}
}
Example:
public class Test {
public static void main(String[] args) {
int number=20;
//switch expression with int value
switch(number){
//switch cases without break statements
case 10: System.out.println("10");
case 20: System.out.println("20");
case 30: System.out.println("30");
default:System.out.println("Not in 10, 20 or
30");
}
}
}
Nested Switch Statement
• switch statement inside other switch statement is known as nested
switch statement.
Example:
public class Test {
public static void main(String[] args) {
//C - CSE, E - ECE, M - Mechanical
char branch = 'C';
int collegeYear = 4;
switch( collegeYear )
{
case 1:
System.out.println("English, Maths, Science");
break;
case 2:
switch( branch )
{
case 'C':
System.out.println("Operating System, Java, Data Structure");
break;
case 'E':
System.out.println("Micro processors, Logic switching theory");
break;
case 'M':
System.out.println("Drawing, Manufacturing Machines");
break;
}
break;
case 3:
switch( branch )
{
case 'C':
System.out.println("Computer Organization, MultiMedia");
break;
case 'E':
System.out.println("Fundamentals of Logic Design, Microelectronics");
break;
case 'M':
System.out.println("Internal Combustion Engines, Mechanical Vibration");
break;
}
break;
case 4:
switch( branch )
{
case 'C':
System.out.println("Data Communication and Networks, MultiMedia");
break;
case 'E':
System.out.println("Embedded System, Image Processing");
break;
case 'M':
System.out.println("Production Technology, Thermal Engineering");
break;
}
break;
}
}
}
Enum in Switch Statement
• enum is a class that represent the group of constants. (immutable
such as final variables).
• keyword enum is used and put the constants in curly braces separated
by comma.
Example:
public class SwitchEnum {
public enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat }
public static void main(String[] args) {
Day[] DayNow = Day.values();
for (Day Now : DayNow)
{
switch (Now)
{
case Sun:
System.out.println("Sunday");
break;
case Mon:
System.out.println("Monday");
break;
case Tue:
System.out.println("Tuesday");
break;
case Wed:
System.out.println("Wednesday");
break;
case Thu:
System.out.println("Thursday");
break;
case Fri:
System.out.println("Friday");
break;
case Sat:
System.out.println("Saturday");
break;
}
}
}