![]() |
![]() |
Java if statement | |
Java if-else statement | |
Java nested if-else | |
Java else-if ladder | |
Java Switch case statement | |
Java Nested Switch Case | |
Java Ternary/Conditional operator |
Java while loop | |
Java do-while loop | |
Java for loop | |
Java Enhanced loop/for each loop | |
Java Labelled for loop | |
Java Nesting of loops | |
Java Break and Continue |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
Normally, the program is executed sequentially. In some situation in the programming the flow is not applicable to execute sequentially. In that case we need repeat some statements until certain condition is encountered.
When a program does not follow the sequential flow or the program breaks the sequential flow and jumps to another part of code, then it is called as branching.
if the branching proceeds as per the occurrence of condition or decision then it is known as conditional branching
when branching is not based on a particular condition or decision then it is known as conditional branching.
If statement is a simplest decision making statement. It is used to control the flow of execution of statements.
if ( test - expression ) { // if block statements are executed when test-expression becomes true // write whatever you want }
In below example we are taking input from user throw commandline argument as ATM PIN and checking the available balance
class IfStatement { public static void main(String args[]) { int PIN; int balance = 15000; PIN = Integer.parseInt(arg[0]); if (PIN == 1234) { System.out.println("Welcome user"); System.out.println("Your balance is "+ balance); } System.out.println("\nThank you for login"); } }
In above example we have taken input from user and condition is written as PIN == 1234.
The relational operator = = is used to compare two different values. If both values are equal then if block statement is executed.
But the statements outside the if block are by default executed.Thats why 'thank you for login is get printed'.
There are 3 different types of if statement based on their need.