Decision and control statements
In Java, decision and control statements allow you to select and execute specific blocks of the code while skipping other sections or statements.
The if statement
The if
statement consists of a condition followed by one or more statements.
The syntax of an if
statement is as follows:
if(condition) { //Statements will execute if the condition is true }
An example of this is as follows:
package MyFirstPackage; public class IfCondition { public static void main(String[] args) { int empSal = 20000; if (empSal >= 10000) { System.out.println("he is a manager...!"); } else { System.out.println("he is NOT a manager...!"); } } }
The output of the preceding code is as follows:
he is a manager...!
The if...else statement
The if
statement can be followed by an optional else
statement, which executes when the condition is false
.
The syntax of an if...else
statement is:
if(condition){ //Executes when the...