Control Statements in Java
Decision-making statements in Java execute a block of code based on a condition.
Decision-making in programming is similar to decision-making in real life. In programming,
we also face situations where we want a certain block of code to be executed when some
condition is fulfilled.
A programming language uses control statements to control the flow of execution of a program
based on certain conditions.
These are used to cause the flow of execution to advance and branch based on changes to the
state of a program. Java provides several control statements to manage program flow,
including:
Conditional Statements: if, if-else, nested-if, if-else-if
Switch-Case: For multiple fixed-value checks
Jump Statements: break, continue, return
• When the program breaks the sequential flow and jumps to another
part of the code it is called branching
• When the branching is based on a particular condition it is known as
conditional branching
• If branching takes place without any decision it is known as
unconditional branching.
• Java language possesses such decision making capabilities and
supports the statements known as control or decision making
statements:
• If statement
• Switch statement
• Conditional operator statement
Decision making with IF statement
Java if Statement
The if statement is the most simple decision-making
statement.
It is used to decide whether a certain statement or block of
statements will be executed or not i.e.
if a certain condition is true then a block of statements is
executed otherwise not.
Syntax:
if(condition) {
// Statements to execute if
// condition is true
}
if Statement Execution Flow
• The below diagram demonstrates the flow chart of an "if Statement
execution flow" in programming.
Examples:
if (bank balance is zero)
Borrow money
If(room is dark)
Put on lights
Eg:
public class Main {
public static void main(String[] args) {
int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater than y");
}
}
}
O/p:x is greater than y
if-else Statement
The if statement alone tells us that if a condition is true it will execute a block of
statements and
if the condition is false it won't.
But what if we want to do something else if the condition is false? Here, comes the "else"
statement.
We can use the else statement with the if statement to execute a block of code when the
condition is false.
Syntax:
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
if-else Statement Execution flow
The below diagram demonstrates the flow chart of an "if-else Statement execution flow" in
programming
import java.util.*;
class Geeks {
public static void main(String args[])
{
int i = 10;
if (i < 15)
System.out.println("i is smaller than 15");
else
System.out.println("i is greater than 15");
}
}
Output: I is smaller the 15
Java nested-if Statement
A nested if is an if statement that is the target of another
if or else.
Nested if statements mean an if statement inside an if
statement.
Yes, java allows us to nest if statements within if
statements.
i.e, we can place an if statement inside another if
statement.
Syntax:
if (condition1) {
code to be executed if condition1 is true
}
else if (condition2) {
code to be executed if condition2 is true
}
else {
code to be executed if condition1 and condition2 are not
public static void main(String[]
args) {
int a = 30, b = 30;
if(a > b) {
System.out.println("a is bigger than
b.");
}
else if(a < b) {
System.out.println("a is smaller
than b.");
}
else {
System.out.println("a and b are
equal.");
}
}
O/P:
A and B are equal
Java if-else-if ladder
Here, a user can decide among multiple options. The if statements are executed from the top down.
As soon as one of the conditions controlling the if is true, the statement associated with that 'if' is
executed,
and the rest of the ladder is bypassed.
If none of the conditions is true, then the final else statement will be executed. There can be as many as
'else if' blocks
associated with one 'if' block but only
one 'else' block is allowed with one 'if' block.
Syntax:
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
} else {
// code to be executed if all conditions are false
}
if-else-if ladder Execution Flow
The below diagram demonstrates the flow chart of an "if-else-if ladder execution flow" in programming
import java.util.*;
class Geeks {
public static void main(String args[])
{
int i = 20;
if (i == 10)
System.out.println("i is 10");
else if (i == 15)
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present"); }}
O/P: I is 20
switch-case statement
Instead of writing many if..else statements, you can use
the switch statement.
The Switch Statements
switch statement selects one of many code blocks to be executed:
switch(choice)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
case 3:
execute code block 3
break;
default:
execute code if choice is different
break;
}
public class DecisionTest04 {
public static void main(String[]
args) {
int choice = 3;
switch(choice) {
case 1:
System.out.println("hello");
break;
case 2:
System.out.println("hi");
break;
case 3:
System.out.println("welcome");
break;
default:
System.out.println("bye");
break;
} }}
O/P: Welcome