Java Control Statements
Types of Control Statements
- Selection Statements
- Iteration Statements
- Jump Statements
1.Selection Statements
Selection statements are an important part of programming languages.Java offers two types of selection statements:
- if-else statements
- switch statements
These statements allow developers to make decisions based on certain conditions and control the flow of execution in their programs.
1.1. if-else statements
If-else statements are an important part of Java programming that allows developers to make decisions based on specific conditions. The if-else statement executes a block of code if a particular condition is true, and another block of code if the condition is false.if (condition) {
// code to execute if the condition is true
} else {
// code to execute if the condition is false
}
Example 1.
int num = 10;
if (num > 0) {
System.out.println("Number is positive");
} else {
System.out.println("Number is not positive");
}
Example 2.
int num = -5;
if (num > 0) {
System.out.println("Number is positive");
} else {
System.out.println("Number is not positive");
}
if-else statements are a crucial part of Java programming, enabling developers to execute different blocks of code based on specific conditions. The syntax is straightforward and easy to understand, making it a fundamental concept for beginners to grasp. By mastering if-else statements, developers can build more complex programs that involve more intricate decision-making.
1.2.Switch statements
Switch statements are another type of control statement in Java that allows developers to execute different blocks of code based on the value of an expression. It provides a more concise way of writing multiple if-else statements.
Syntax for a switch statement
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 above cases are true
}
Here, the expression is a variable or expression that is evaluated. The switch statement then checks if the value of the expression matches any of the values specified in the case statements. If a match is found, the code inside that case statement is executed, and the break statement is used to exit the switch statement. If no match is found, the code inside the default block is executed.
Example 1.
int dayOfWeek = 3;
switch (dayOfWeek) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
default:
System.out.println("Invalid day");
}
In the above example, we have declared a variable dayOfWeek with the value 3. We use a switch statement to print the day of the week based on the value of dayOfWeek. Since the value of dayOfWeek is 3, the code inside the case statement for 3 is executed, which prints "Wednesday" to the console.
Example 2.
int dayOfWeek = 7;
switch (dayOfWeek) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
default:
System.out.println("Invalid day");
}
In the above example, we have declared a variable dayOfWeek with the value 7. We use a switch statement to print the day of the week based on the value of dayOfWeek. Since the value of dayOfWeek does not match any of the case statements, the code inside the default block is executed, which prints "Invalid day" to the console.
switch statements provide a more concise way of writing multiple if-else statements in Java. By using switch statements, developers can execute different blocks of code based on the value of an expression. The syntax is straightforward and easy to understand, making it a fundamental concept for beginners to grasp. By mastering switch statements, developers can build more complex programs that involve more intricate decision-making.
2.Iteration Statements
Iteration statements, also known as loops, are control structures in Java that allow developers to execute a block of code repeatedly until a specific condition is met.- for-each loop
- while
- do-while loop
- for loop
2.1. for-each loop
The for-each loop is a type of for loop introduced in Java 5 that simplifies iteration over arrays and collections. It is also known as the enhanced for loop or the for-in loop.
Syntax of the for-each loop.
Syntax of the for-each loop.
Syntax of the for-each loop
for (elementType element : array/collection) {
// code to execute
}
Here, elementType is the data type of the elements in the array/collection, element is a variable that represents each element in the array/collection, and array/collection is the array or collection that we want to iterate over.
Example of the for-each loop
Let's take an example to understand how the for-each loop works in Java
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.println(num);
}
Output:
1
2
3
4
5
Advantages of the for-each loop
- Simplicity: The for-each loop simplifies the code for iterating over arrays and collections.
- Safety: The for-each loop eliminates the possibility of index-out-of-bounds errors and makes the code safer.
- Readability: The for-each loop makes the code more readable and easier to understand.
- Performance: The for-each loop is generally faster than the traditional for loop for iterating over arrays and collections.
The for-each loop is a powerful construct in Java that simplifies iteration over arrays and collections. By using the for-each loop, developers can write cleaner, safer, and more readable code.
2.2. while loop
In Java, the while loop is a control statement that allows code to be executed repeatedly based on a boolean condition. The while loop will continue executing the code as long as the condition remains true. In this article, we will explore how to use the while loop in Java with an example.
Syntax of the while loop
while (condition) {
// code to execute
}
Here, condition is a boolean expression that is evaluated before each iteration of the loop. If the condition is true, the code inside the loop will execute. If the condition is false, the loop will terminate.
Example of the while loop
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
Output:
Advantages of the while loop
The while loop is a powerful construct in Java that allows code to be executed repeatedly based on a boolean condition. By using the while loop, developers can write flexible, simple, and efficient code.
1
2
3
4
5
Advantages of the while loop
- Flexibility: The while loop is very flexible and can be used in a variety of situations.
- Simplicity: The while loop is simple and easy to understand, making it a good choice for many programming tasks.
- Efficiency: The while loop is very efficient and can be used to process large amounts of data quickly.
The while loop is a powerful construct in Java that allows code to be executed repeatedly based on a boolean condition. By using the while loop, developers can write flexible, simple, and efficient code.
2.3. do-while loop
The do-while loop is a control statement that allows code to be executed repeatedly based on a boolean condition. The do-while loop is similar to the while loop, but the code inside the loop is executed at least once before the condition is checked. In this article, we will explore how to use the do-while loop in Java with an example.
Syntax of the do-while loop
do {
// code to execute
}
while (condition);
Here, condition is a boolean expression that is evaluated after each iteration of the loop. If the condition is true, the code inside the loop will execute again. If the condition is false, the loop will terminate.
Example of the do-while loop
int i = 1;
do {
System.out.println(i);
i++;
}
while (i <= 5);
Output:
Advantages of the do-while loop
the do-while loop is a powerful construct in Java that allows code to be executed repeatedly based on a boolean condition. By using the do-while loop, developers can write flexible, simple, and efficient code.
1
2
3
4
5
Advantages of the do-while loop
- Flexibility: The do-while loop is very flexible and can be used in a variety of situations.
- Simplicity: The do-while loop is simple and easy to understand, making it a good choice for many programming tasks.
- Efficiency: The do-while loop is very efficient and can be used to process large amounts of data quickly.
the do-while loop is a powerful construct in Java that allows code to be executed repeatedly based on a boolean condition. By using the do-while loop, developers can write flexible, simple, and efficient code.
2.4. for loop
In Java, the for loop is a control statement that allows code to be executed repeatedly based on a specified condition. The for loop is typically used when the number of iterations is known in advance. In this article, we will explore how to use the for loop in Java with an example.
Syntax of the for loop
for (initialization; condition; increment/decrement) {
// code to execute
}
Here, initialization is an expression that is executed once before the loop starts. condition is a boolean expression that is evaluated before each iteration of the loop. If the condition is true, the code inside the loop will execute. If the condition is false, the loop will terminate. increment/decrement is an expression that is executed at the end of each iteration of the loop.
Example of the for loop
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
In the above example, we use the for loop to print the numbers 1 through 5. The initialization expression creates a variable i and sets its initial value to 1. The condition expression checks whether i is less than or equal to 5. Inside the loop, we print the value of i using System.out.println(i). The increment/decrement expression increments i by 1 at the end of each iteration using the i++ operator.
Output:
Advantages of the for loop
the for loop is a powerful construct in Java that allows code to be executed repeatedly based on a specified condition. By using the for loop, developers can write flexible, simple, and efficient code.
1
2
3
4
5
Advantages of the for loop
- Flexibility: The for loop is very flexible and can be used in a variety of situations.
- Simplicity: The for loop is simple and easy to understand, making it a good choice for many programming tasks.
- Efficiency: The for loop is very efficient and can be used to process large amounts of data quickly.
the for loop is a powerful construct in Java that allows code to be executed repeatedly based on a specified condition. By using the for loop, developers can write flexible, simple, and efficient code.
3.Jump Statements
Jump Statements are control statements that allow the program to transfer control from one part of the program to another.Types of Jump Statements in Java
In this example, the for loop will iterate from 0 to 9, but when i becomes equal to 5, the break statement is encountered, and the loop is terminated.
Example 2:
In this example, the while loop will iterate from 0 to 9, but when i becomes equal to 5, the break statement is encountered, and the loop is terminated.
the break statement is a powerful tool for controlling the flow of a Java program. It can be used to terminate a loop or a switch statement and can even be used to exit from a nested loop.
In this example, the for loop will iterate from 0 to 9, but when i becomes equal to 5, the continue statement is encountered, and the remaining statements in that iteration are skipped.
The continue statement is often used in combination with an if statement to skip a certain iteration when a certain condition is met.
In this example, the while loop will iterate from 1 to 10, but when i is an even number, the continue statement is encountered, and the remaining statements in that iteration are skipped. Therefore, the output of the above code will be:
- break
- continue
3.1. Break
The break statement is used to exit a loop or a switch statement. When a break statement is encountered inside a loop, the loop is terminated, and program control is transferred to the next statement after the loop. Similarly, when a break statement is encountered inside a switch statement, the switch statement is terminated, and program control is transferred to the next statement after the switch.
Syntax of the Break
break;
Example 1:
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
In this example, the for loop will iterate from 0 to 9, but when i becomes equal to 5, the break statement is encountered, and the loop is terminated.
Output
0
1
2
3
4
int i = 0;
while (i < 10) {
if (i == 5) {
break;
}
System.out.println(i);
i++;
}
Output
0
1
2
3
4
the break statement is a powerful tool for controlling the flow of a Java program. It can be used to terminate a loop or a switch statement and can even be used to exit from a nested loop.
3.2. Continue
The continue statement is used to skip the current iteration of a loop and move to the next iteration. When a continue statement is encountered inside a loop, the loop skips the remaining statements in the current iteration and moves on to the next iteration.
continue;
Example 1 :
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
System.out.println(i);
}
Output :
0
1
2
3
4
6
7
8
9
Example 2 :
int i = 0;
while (i < 10)
{ i++;
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
1
3
5
7
9
Conclusion
Control Statements in Java are crucial for controlling the flow of execution of a program. Java provides several types of control statements, including selection statements, iteration statements, and jump statements. As a Java programmer, it is essential to understand how to use each type of control statement effectively to create efficient and effective programs.
You Know?
1. What are the control statements in Java?Control Statements means controlling the flow of execution of a program. Control statements are used to make decisions, perform loops, and alter the sequence of execution of a program.
No comments:
Post a Comment