What are control statements in Java with an example?

Chris 82 Published: 09/10/2024

What are control statements in Java with an example?

I'd be happy to explain Control Statements in Java and provide an example.

Control statements in Java are used to direct the flow of a program's execution. They help decide which part of the code should be executed next, based on certain conditions or actions. The five types of control statements in Java are:

If-else Statements Switch Statements Loops (For, While, Do-While) Jump Statements (Break, Continue, Return)

Let's take a closer look at each of these types and an example for each.

If-else Statements

The if-else statement is used to execute a block of code if a certain condition is true. If the condition is false, it can execute another block of code. Here's an example:

int x = 10;

if(x > 5) {

System.out.println("x is greater than 5");

} else {

System.out.println("x is less than or equal to 5");

}

In this example, the if statement checks if x is greater than 5. If it is, then it prints "x is greater than 5". If not, then it prints "x is less than or equal to 5".

Switch Statements

The switch statement is used when you have multiple values and you want to execute different blocks of code based on those values. Here's an 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");

}

In this example, the switch statement checks the value of day. If it's 1, then it prints "Monday". If it's 2, then it prints "Tuesday", and so on. If the value is not one of these cases (default), then it prints "Invalid day".

Loops

There are three types of loops in Java: For Loop, While Loop, and Do-While Loop.

For Loop

The for loop is used when you need to execute a block of code for each element in an array or collection. Here's an example:

int[] scores = {90, 80, 70};

for(int score : scores) {

if(score > 85) {

System.out.println("Score " + score + " is an A");

} else if(score > 75) {

System.out.println("Score " + score + " is a B");

} else {

System.out.println("Score " + score + " is a C or below");

}

}

In this example, the for loop iterates over each element in the scores array. For each score, it checks if it's greater than 85, then prints "A". If not, it checks if it's greater than 75, then prints "B", and so on.

While Loop

The while loop is used when you want to execute a block of code as long as a certain condition is true. Here's an example:

int x = 0;

while(x < 5) {

System.out.println("x is " + x);

x++;

}

In this example, the while loop will continue to execute until x is no longer less than 5.

Do-While Loop

The do-while loop is similar to the while loop, but it first executes the block of code and then checks if the condition is true. Here's an example:

int x = 0;

do {

System.out.println("x is " + x);

x++;

} while(x < 5);

In this example, the do-while loop will execute at least once (since it executes before checking the condition), and then continue to execute until x is no longer less than 5.

Jump Statements

The jump statements in Java are used to transfer control to another part of your code. They can be used to exit a loop or skip certain parts of your code. Here's an example:

int x = 0;

while(x < 10) {

if(x == 5) {

break; // exits the loop when x is 5

}

System.out.println("x is " + x);

x++;

}

In this example, the break statement will exit the while loop when x is 5.

In conclusion, control statements in Java are essential for directing the flow of your program's execution. They provide a way to execute different blocks of code based on conditions or actions. You can use if-else statements, switch statements, loops (for, while, do-while), and jump statements (break, continue, return) to create more complex logic in your Java programs.

Is do while a decision-making statement in Java?

I'd be happy to respond in English!

A do while loop is a type of control structure commonly used in programming languages, including Java. In Java, the do while loop is used to execute a block of code as long as a specified condition remains true.

The basic syntax of a do while loop in Java is as follows:

do {

// code to be executed

} while (condition);

Here, the code inside the do block will be executed at least once before the loop condition is evaluated. The condition is checked after each iteration, and if it's true, the loop continues; otherwise, it exits.

To illustrate how a do while loop works in Java, let's consider an example:

int i = 0;

do {

System.out.println("Value of i: " + i);

i++;

} while (i < 5);

This code will output the values of i from 0 to 4, since the condition i < 5 is true. The loop will continue as long as i is less than 5.

Now, let's see how this relates to decision-making in Java. A do while loop can be used to implement a decision-making process where you want to execute code repeatedly until a specific condition is met or exceeded. This is particularly useful when working with input/output operations, such as reading and processing user input.

Here's an example of using a do while loop for decision-making:

Scanner scanner = new Scanner(System.in);

int age;

do {

System.out.print("Enter your age (or 'quit' to exit): ");

String input = scanner.nextLine();

if (input.equalsIgnoreCase("quit")) {

break;

} else {

try {

age = Integer.parseInt(input);

} catch (NumberFormatException e) {

System.out.println("Invalid input. Please enter a number.");

}

}

} while (age <= 0 || age >= 150); // restrict age range

In this example, the user is prompted to enter their age until they type 'quit'. The loop checks if the input is valid and within a specific range (1-150). If the input doesn't meet these conditions, it asks the user to re-enter their age.

By using a do while loop in Java, you can create a decision-making process that iterates through different scenarios until a specific condition is met or exceeded. This control structure allows for more complex logic and flexibility in your code.

In summary, a do while loop in Java is a type of control structure used to execute code repeatedly while a specified condition remains true. It can be used for decision-making processes where you want to iterate through different scenarios until a specific condition is met or exceeded.