Conditional Statements in Java Control Statements
Conditional Statements
Java has two conditional statements: the if statement and the switch statement. By using these statements, you can control the flow of program execution based on conditions while the program is running.
if ~ else Statement
An if statement is a conditional statement that executes one of two statements depending on a condition.
if (condition)
statement1;
else
statement2;
The if statement returns true or false according to the condition expression. The else statement is written when needed and can be omitted when it is not necessary.
package com.devkuma.tutorial.control.statement;
public class IfElse {
public static void main(String[] args) {
int a = 0;
if (a == 1) {
System.out.println("a는 1이다.");
} else {
System.out.println("a는 1 아니다.");
}
}
}
if ~ else if Statement
The if ~ else if statement makes multiple selection possible by using if statements.
if (condition1)
statement1;
else if (condition2)
statement2;
...
else
statement3;
All Java statements are executed sequentially from top to bottom. If the first condition evaluates to true, the corresponding statement is executed and the if statement is exited. If the first condition evaluates to false, the following else if conditions are evaluated in order.
package com.devkuma.tutorial.control.statement;
public class IfElseIf {
public static void main(String[] args) {
int month = 3;
String season = null;
if (month == 1) {
season = "winter";
} else if (month == 2) {
season = "winter";
} else if (month == 3) {
season = "spring";
} else if (month == 4) {
season = "spring";
} else if (month == 5) {
season = "spring";
} else if (month == 6) {
season = "summer";
} else if (month == 7) {
season = "summer";
} else if (month == 8) {
season = "summer";
} else if (month == 9) {
season = "autumn";
} else if (month == 10) {
season = "autumn";
} else if (month == 11) {
season = "autumn";
} else if (month == 12) {
season = "winter";
}
System.out.println("month=" + month + ", season=" + season);
}
}
The result is as follows.
month=3, season=spring
switch ~ case Statement
The switch ~ case statement makes multiple selection possible.
switch (expression) {
case value1:
statement1;
break;
case value2:
statement2;
break;
.....
case valueN:
statementN;
break;
default:
default statement;
}
The break statement in each case is optional. If break is omitted, execution falls through to the case statement immediately below. This can be used to assign multiple case clauses to one statement block.
package com.devkuma.tutorial.control.statement;
public class SwitchCase1 {
public static void main(String[] args) {
int a = 257;
int result = a % 3;
switch (result) {
case 1:
System.out.println(a + "는 3의 배수가 아니다.");
break;
case 2:
System.out.println(a + "는 3의 배수가 아니다.");
break;
default:
System.out.println(a + "는 3의 배수이다.");
break;
}
}
}
The break statement in each case is optional. If the break statement is omitted, execution falls through to the case statement immediately below. Therefore, the example above can be changed as follows.
package com.devkuma.tutorial.control.statement;
public class SwitchCase2 {
public static void main(String[] args) {
int a = 257;
int result = a % 3;
switch (result) {
case 1:
case 2:
System.out.println(a + "는 3의 배수가 아니다.");
break;
default:
System.out.println(a + "는 3의 배수이다.");
break;
}
}
}
The statements in a case continue to execute until they encounter break, reach the final case, or reach the default statement.
The earlier if ~ else example can be rewritten as follows.
package com.devkuma.tutorial.control.statement;
public class SwitchCase3 {
public static void main(String[] args) {
int month = 3;
String season = null;
switch (month) {
case 1:
case 2:
case 12:
season = "winter";
break;
case 3:
case 4:
case 5:
season = "spring";
break;
case 6:
case 7:
case 8:
season = "summer";
break;
case 9:
case 10:
case 11:
season = "autumn";
break;
default:
break;
}
System.out.println("month=" + month + ", season=" + season);
}
}
Nested switch Statements
A switch statement can contain another switch statement. Nested switch statements can freely use case statements with the same values.
package com.devkuma.tutorial.control.statement;
public class SwitchCase4 {
public static void main(String[] args) {
int a = 1;
int b = 2;
switch (a) {
case 1:
switch (b) {
case 0:
System.out.println("a=1, b=0");
case 1:
System.out.println("a=1, b=1");
}
break;
case 2:
System.out.println("a=2");
break;
default:
break;
}
}
}