Control Statements in Java
Control Statements in Java are controlling program flow and determining whether the other statements will be executed or not. Java statement is a complete unit of execution, terminated with a semicolon. Control flow statements can be applied to single expressions as well as a block of code.
- IF STATEMENTS
The if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in Java.
- if statement
- if-else statement
- if-else-if ladder
- nested if-else statement
i) if statement :
The Java if statement tests the condition. It executes the if block if condition is true.
Syntax:
Example:
ii) if-else Statement :
The Java if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed.
Syntax:
Example :
iii) if-else-if ladder Statement :
The if-else-if ladder statement executes one condition from multiple statements
Syntax:
Example:
iv) nested if-else statement :
when there is an if statement inside another if statement then it is known as nested if else. Nested if else can also be simplified using Java Switchcase Statement.
Syntax:
Example:
2. SWITCH STATEMENT
Java has a built-in multiple decision statement known as switch. The switch statement tests the value of a given variable (or expression) against a list of case values and when a match is found , a block of statements associated with the case is executed.
Syntax:
Example: