Among the decision making statements, if- statement is the most simple one.
It checks if the condition is true or not, and if it is true then it executes the block of code inside if otherwise, it will not. The block that is executed is placed within the curly braces({}).
Flowchart for if statement in java:
Syntax of if statement in Java:
1 2 3 4 5 | if (boolean expression) { /* if expression is true */ statements... ; /* Execute statements */ } |
Example of if statement in Java:
1 2 3 4 5 6 7 8 9 10 11 12 | public class IfTest { public static void main(String args[]) { int a = 5; if( a < 10) //checks for condition { System.out.println("If statement executed"); } } } |
Output:
1 | If statement executed |