Control Statement in Java
Control Statement in Java
Control statements in Java are instructions that manage the flow of execution of a program
based on certain conditions. They are used to make decisions, to loop through blocks of code
multiple times, and to jump to a different part of the code based on certain conditions. Control
statements are fundamental to any programming language, including Java, as they enable the
creation of dynamic and responsive programs.
• Decision-Making Statements
• Looping Statements
• Jump Statements
1. Decision-Making Statements
Decision-making statements in Java are constructs used to control the flow of execution
based on certain conditions. They allow a program to execute different parts of code depending
on whether a condition (or set of conditions) is true or false. The decision-making statements
in Java are primarily of 3 types, namely: if, if-else and switch statements.
The if statement in Java evaluates a boolean condition. If the condition is true, the block of
code inside the if statement is executed. It's the simplest form of decision-making in
programming, allowing for the execution of certain code based on a condition.
Syntax
if (condition) {
// Code to execute if the condition is true
}
Flowchart
1.2 'if-else' Statement in Java
The if-else statement in Java is used for multiple conditions. It comes after an if statement
and before an else statement. If the if condition is false, the program checks the if-else
condition. If the if-else condition is true, its code block is executed.
Syntax
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Flowchart
1.3 'switch' Statement in Java
The switch statement in Java allows for the selection of a block of code to be executed
based on the value of a variable or expression. It is an alternative to a series of if statements
and is often more concise and easier to read.
Syntax
switch (expression) {
case value1:
// Code to be executed if expression equals value1
break;
case value2:
// Code to be executed if expression equals value2
break;
// ...
default:
// Code to be executed if expression does not match any case
}
Flowchart
2. Looping Statements
Looping statements in Java are used to execute a block of code repeatedly based on a given
condition or set of conditions. These statements are fundamental to Java programming,
allowing for iteration over data structures, repetitive tasks, and more. The main types of
looping statements in Java are for loop, while loop and do-while loop.
The for loop in Java is a control structure that allows repeated execution of a block of code
for a specific number of times. It is typically used when the number of iterations is known
beforehand.
Syntax
Here,
• Initialization: Typically used to initialize a counter variable.
• Condition: The loop runs as long as this condition is true.
• Update: Updates the counter variable, usually incrementing or decrementing.
Flowchart
A while loop in Java repeatedly executes a block of code as long as a specified condition
remains true. It is ideal for situations where the number of iterations is not predetermined.
Syntax
while (condition) {
// code block to be executed
}
Here,
• Condition: The loop continues to run as long as this condition evaluates to true.
• Code Block: The statements inside the loop execute repeatedly until the condition
becomes false.
Flowchart
The do-while loop in Java is a post-tested loop, meaning it executes the body of the loop at
least once before checking the condition. This loop is useful when you need to ensure that the
loop body is executed at least once, regardless of whether the condition is true or false. After
the body of the loop has executed, the condition is tested. If the condition is true, the loop will
execute again. This process repeats until the condition becomes false.
Syntax
do {
// Statements to execute
} while (condition);
Here,
Flowchart
3. Jump Statements
Jump statements in Java are used to transfer control to another part of the program. They
are particularly useful for breaking out of loops or skipping to the next iteration of a loop.
The three main types of jump statements are break, continue, and return. Each serves a
different purpose in controlling the flow of execution.
The break statement is used to exit from a loop (for, while, do-while) or a switch statement.
When encountered, it terminates the loop or switch statement and transfers control to the
statement immediately following the loop or switch.
Syntax
break;
Flowchart
3.2 'continue' Statement in Java
The continue statement skips the current iteration of a loop (for, while, do-while) and
proceeds to the next iteration. It effectively jumps to the end of the loop's body and re-
evaluates the loop's condition.
Syntax
continue;
Flowchart
The return statement is used to exit from a method, with or without a value. For methods that
return a value, return specifies the value to return. For void methods, it causes the method to
exit before reaching the end of its body.
Syntax
return expression;
Here,
expression: This must evaluate to a value that is compatible with the method's declared
return type. If the method is supposed to return an int, for example, the expression must
evaluate to an integer value.
return;
For Example,
Let's consider a simpler real-life example using the return statement in Java. Develop a Java
program that can accurately determine whether a given year is a leap year. A year is considered
a leap year if it is divisible by 4 but not by 100 unless it is also divisible by 400. This method
takes an integer year as input and returns a boolean value to be true if the year is a leap year
and false otherwise.
if (isLeapYear) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}