0% found this document useful (0 votes)
43 views

Control Statement in Java

Uploaded by

Maanav Singala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Control Statement in Java

Uploaded by

Maanav Singala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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.

Types of Control Statements in Java

• 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.

1.1 'if' Statement in Java

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.

Let's discuss each one of them in detail.

2.1 'for loop' in Java

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

for (initialization; condition; update) {


// code block to be executed
}

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

2.2 'while loop' in Java

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

2.3 'do-while loop' in Java

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,

• do: This keyword starts the do-while loop.


• Statements to execute: These are the actions you want to perform. This block of
code is executed on each iteration of the loop, and it is guaranteed to run at least once.
• while: This keyword is followed by a condition in parentheses.
• condition: A Boolean expression that is evaluated after the loop body has executed. If
this condition evaluates to true, the loop will execute again. If it evaluates to false,
the loop will terminate, and control passes to the statement immediately following the
loop.

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.

Let's discuss each one of them in detail.

3.1 'break' Statement in Java

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

3.3 'return' Statement in Java

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

Syntax for Methods Returning a Value

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.

Syntax for Void Methods

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.

public class LeapYearChecker {


public static void main(String[] args) {
int year = 2024; // Sample year to check
boolean isLeapYear = checkLeapYear(year);

if (isLeapYear) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}

public static boolean checkLeapYear(int year) {


// Check if the year is a leap year
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
return true; // It's a leap year
} else {
return false; // It's not a leap year
}
}
}

You might also like