Control Statements
What are Control Statements
Control statements are used to control the flow of execution of a program based on conditions. They help in decision-making.
google.com, pub-8434817042454839, DIRECT, f08c47fec0942fa0
if Statement
Used to execute a block of code if a condition is true.
int age = 18;
if(age >= 18){
System.out.println(“Eligible to vote”);
}
if(age >= 18){
System.out.println(“Eligible to vote”);
}
if-else Statement
Used when there are two conditions.
if(age >= 18){
System.out.println(“Adult”);
} else {
System.out.println(“Minor”);
}
System.out.println(“Adult”);
} else {
System.out.println(“Minor”);
}
Nested if Statement
Used when multiple conditions need to be checked.
if(age >= 18){
if(age < 60){
System.out.println(“Adult”);
}
}
if(age < 60){
System.out.println(“Adult”);
}
}
switch Statement
Used to select one option from multiple choices.
int day = 2;
switch(day){
case 1: System.out.println(“Monday”); break;
case 2: System.out.println(“Tuesday”); break;
default: System.out.println(“Invalid day”);
}
switch(day){
case 1: System.out.println(“Monday”); break;
case 2: System.out.println(“Tuesday”); break;
default: System.out.println(“Invalid day”);
}

