If ... Else
Control Flow Statements in Java:-
Control flow statements are used to control the flow of execution of a Java program.
They allow you to make decisions, repeat blocks of code, and exit loops prematurely.
If-Else statement:-
The If-Else statement is the simplest conditional statement in Java. It allows you to execute different blocks of code depending on whether a condition is true or false.
Example
int age = 17; if (age > 18) { System.out.println("You are an adult."); } else { System.out.println("You are not an adult."); }
else if statement:-
The else if statement in Java is a conditional statement that allows you to check multiple conditions and execute different blocks of code depending on which condition is true.
Example
int age = 17; if (age >= 18) { System.out.println( "You are an adult."); } else if (age >= 13) { System.out.println("You are a teenager."); } else { System.out.println( "You are a child."); }
The Switch statement:-
The Switch statement is a more complex conditional statement that allows you to execute different blocks of code depending on the value of an expression.
Example
String input = "hello"; switch (input) { case "hello": System.out.println( "Hello!"); break; case "CodeLines" System.out.println( "CodeLines!"); break; default: System.out.println( "I don't understand your input."); }