⬅ Previous Topic
Java goto KeywordNext Topic ⮕
Java implements Keywordif
Keyword⬅ Previous Topic
Java goto KeywordNext Topic ⮕
Java implements Keywordif
Keyword in Java
The if
keyword in Java is your first step into controlling the flow of your program. It allows your code to make decisions — a fundamental aspect of programming logic.
Whenever you want something to happen only when a specific condition is true, if
is what you use.
if
Statementif (condition) {
// code to execute if condition is true
}
The condition inside the parentheses must evaluate to a boolean value — true
or false
. If the result is true
, the code block runs. If it's false
, the block is skipped.
if
Statementpublic class IfExample1 {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
System.out.println("Check completed.");
}
}
You are eligible to vote.
Check completed.
Here, the condition age >= 18
is true
because age
is 20. Therefore, the message inside the if
block is printed. The program then continues to the next line after the block.
public class IfExample2 {
public static void main(String[] args) {
int age = 16;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
System.out.println("Check completed.");
}
}
Check completed.
This time, age
is 16. Since age >= 18
is false
, the body of the if
statement is skipped. The program continues to print Check completed.
public class IfExample3 {
public static void main(String[] args) {
boolean isLoggedIn = true;
if (isLoggedIn) {
System.out.println("Welcome back, user!");
}
}
}
Welcome back, user!
Notice how we used a boolean variable directly in the if
condition. If it's true
, the block runs. This pattern is common when checking flags or system states.
public class IfExample4 {
public static void main(String[] args) {
int number = 10;
if (number > 5)
System.out.println("Number is greater than 5");
System.out.println("End");
}
}
Number is greater than 5
End
When there's only one statement to execute under an if
condition, Java lets you skip the curly braces. But be cautious — for readability and maintainability, it's usually better to keep them.
if
Statementspublic class IfExample5 {
public static void main(String[] args) {
int marks = 85;
if (marks >= 50) {
if (marks >= 80) {
System.out.println("Excellent score!");
}
}
}
}
Excellent score!
Nested if
statements are useful when you want to validate one condition only after another one is true. In this case, the program first checks if the score is at least 50, then checks if it’s 80 or more.
if
if (x > 10);
System.out.println("This will always run!");
A semicolon directly after if
ends the statement prematurely. The following block will run regardless of the condition — a classic beginner mistake.
if
in Java⬅ Previous Topic
Java goto KeywordNext Topic ⮕
Java implements KeywordYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.