if Conditional Statements
In this lesson, an explanation of if statements and how to write them using an example is provided.
The if
statement
The if statement is used when a statement or group of statements are to be executed if a condition is true
. The condition will be checked, and if the condition is true, then the certain operation will be executed, and if the condition is false, then nothing will happen. The program will move to the next executable statement. Let’s understand this with the below example:
Example
Press + to interact
class conditional {public static void main(String[] args) {int x = 10;if (x > 4) {System.out.println("x is greater than 4");}}}
Explanation
Line 5:
- Inside the parentheses is the condition; in this case, it is a test for greater than.
- It is a good practice to compare only the