Articles → Java → Conditional Statements In Java
Conditional Statements In Java
What Are Conditional Statements?
- If there is no client call in the evening (Condition) I will go for movie (Action).
- If you want to play games (Condition) then we will go to gaming zone (Action) otherwise we will go home (Action if condition is not met).
- If air tickets are cheap (Condition) then I will go by flight (Action) or if train tickets are available (Condition) then I will go by train (Action) else I will hire a cab (Action if both the Conditions are not met).
Types Of Conditional Statements In Java
- If statement – executes some code if certain condition is true.
- If..else statement – Execute some code if condition is true or execute some other code if condition is false.
- If..elseif..else statement – Executes some code based on which condition is true.
- Switch statement – Similar to if..elseif..else statement switch statement executes some code based on which condition is true. There is a difference between if..else and switch statement which will be covered in last section.
Difference Between If..Else And Switch Statement
- In case of switch statement we cannot apply logical operators like less than, greater then etc.
- Switch statement can only contains constant values.
- Switch statement is quicker in execution as compared to if..else.