Â
In Java, control statements are used to control the flow of execution of a program. There are several types of control statements, including:
1. Conditional statements: Conditional statements are used to execute a block of code only if a certain condition is met. The most common conditional statement is the if-else statement, which has the following syntax:
if(condition){
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
Â
2. Looping statements: Looping statements are used to repeatedly execute a block of code. The most common looping statements are the for loop and the while loop.
The for loop has the following syntax:
for(initialization; condition; increment/decrement){
// code to be executed
}
Â
The while loop has the following syntax:
while(condition){
// code to be executed
}
Â
3. Jump statements: Jump statements are used to transfer control to another part of the program. The most common jump statements are the break statement and the continue statement. The break statement is used to exit a loop or switch statement, while the continue statement is used to skip the current iteration of a loop.
4. Switch statement: Switch statement is used to execute one block of statement from multiple conditions. It's similar to if-else statement but it's used when we have multiple if-else conditions. It's syntax is:
switch(expression){
case value1:
// code to be executed if expression is equal to value1
break;
case value2:
// code to be executed if expression is equal to value2
break;
Â
default:
// code to be executed if expression is not equal to any of the case values
}
Post a Comment