Mastering Control Statements in Java

Control Statements in Java

  • In Java, control statements are utilized to control the flow of execution in a program.
  • They empower the program to make decisions, repeat certain activities, and department to diverse parts of code based on particular conditions.
  • Control statements are basic for composing logic in programs, allowing for decision-making and iteration.
  • The most types of control statements in Java are conditional statements, looping statements, and branching statements.

Control Statements in java
Control Statements in java

1. Conditional Statements

Conditional statements permit you to execute particular code based on whether a condition is true or false. In Java, the most types of conditional statements are:

if Statement

The if statement evaluates a boolean expression and executes a block of code if the condition is true.

  if (condition) {
// Code to execute if the condition is true
}
Example:
int age = 18;
if (age >= 18) {
System.out.println("You are an adult.");
}

if-else Statement

An if-else statement checks a condition and executes one block of code in case the condition is true, and another block in case the condition is false.

if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}

 

Example:
int age = 16;
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}

if-else if-else Statement

This permits you to check different conditions sequentially. In case the first condition is false, the next else if is assessed, and in case none of the conditions are true, the else block will execute.

if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if none of the conditions are true
}
Example:
int score = 85;
if (score >= 90) {
System.out.println("Grade A");
} else if (score >= 80) {
System.out.println("Grade B");
} else {
System.out.println("Grade C");
}

switch Statement

The switch statement is utilized once you got to assess an expression against numerous possible values. It’s more productive than multiple if-else if statements when checking a single variable against numerous possible values.

switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
default:
// Code to execute if none of the cases match
}
Example:
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}

2. Looping Statements

Looping statements permit you to execute a block of code numerous times, based on a condition. The essential loop types in Java are:

for Loop

The for loop repeats a block of code a settled number of times, based on a counter.

for (initialization; condition; increment/decrement) {
// Code to execute in each iteration
}
Example:
for (int i = 0; i < 5; i++) {
System.out.println(i); // Prints 0, 1, 2, 3, 4
}

while Loop

The while loop repeats a block of code as long as the condition is true. In case the condition is false at the starting, the code inside the loop may not execute at all.

while (condition) {
// Code to execute as long as the condition is true
}
Example:
int i = 0;
while (i < 5) {
System.out.println(i); // Prints 0, 1, 2, 3, 4
i++;
}

do-while Loop

The do-while loop is comparable to the while loop, but the condition is assessed after the code block executes. This guarantees that the code inside the loop is executed at least once, in any case of the condition.

do {
// Code to execute at least once
} while (condition);

 

Example:
int i = 0;
do {
System.out.println(i); // Prints 0, 1, 2, 3, 4
i++;
} while (i < 5);

3. Branching Statements

Branching statements are utilized to control the flow of execution by permitting you to jump from one part of code to another.

break Statement

The break statement is used to exit a loop or a switch statement prematurely, regardless of whether the loop’s condition is satisfied.

for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Exits the loop when i equals 5
}
System.out.println(i);
}
Example Output:
0
1
2
3
4
continue Statement

The continue statement skips the current iteration of the loop and proceeds to the next iteration, skipping any remaining code in the loop body for that iteration.

for (int i = 0; i < 5; i++) {
if (i == 3) {
continue; // Skips the iteration when i equals 3
}
System.out.println(i);
}
Example Output:
0
1
2
4

return Statement

The return statement is used to exit from a method and optionally return a value. It can also be used to stop the execution of a method early.

public int add(int a, int b) {
return a + b; // Exits the method and returns the result
}

Summary of Control Statements in java

Type of StatementDescription
ifExecutes a block of code if a condition is true.
if-elseExecutes one block of code if a condition is true, another if false.
switchSelects one of many code blocks to execute based on a variable’s value.
for loopLoops a fixed number of times, useful when you know how many times to repeat.
while loopLoops as long as a condition is true, checks condition before each iteration.
do-while loopLoops as long as a condition is true, checks condition after each iteration.
breakExits a loop or switch statement early.
continueSkips the current iteration of a loop and moves to the next iteration.
returnExits from a method and optionally returns a value.

 

Control statements in java are essential  for controlling the flow of a program, making decisions, and repeating tasks. They permit developers to compose adaptable, proficient, and dynamic code that can handle a variety of circumstances. Understanding these control structures is pivotal for composing logical and functional programs.

Click here for more details

 

Leave a Comment