Introduction
An if statement helps your program make choices by running code only when a condition is true.
An if statement helps your program make choices by running code only when a condition is true.
if (condition) { // code to run if condition is true }
The condition is a true or false question.
Curly braces { } group the code that runs when true.
age is 18 or more, then prints a message.if (age >= 18) { System.out.println("You can vote."); }
score is greater than 50.if (score > 50) { System.out.println("You passed!"); }
isRaining is true.if (isRaining) { System.out.println("Take an umbrella."); }
This program checks if the temperature is above 25 degrees. If yes, it prints a message.
public class Main { public static void main(String[] args) { int temperature = 30; if (temperature > 25) { System.out.println("It's hot outside."); } } }
If the condition is false, the code inside if does not run.
You can add else to run code when the condition is false.
If statements let your program choose actions based on conditions.
Use if (condition) { ... } to run code only when the condition is true.
Conditions are questions that answer true or false.
if statement do in Java?int x = 10;
if (x > 5) {
System.out.println("Big number");
} else {
System.out.println("Small number");
}int num = 7;
if num > 5 {
System.out.println("Greater");
}int n = 4; // Your code here
Pass the test and this pattern moves unknown