If Statement in Java
The if statement in Java is like a gate. It opens only if a certain condition is true. Otherwise, it quietly ignores the code block inside it. This simple control structure forms the basis for decision-making in Java programs.
The if statement in Java is like a gate. It opens only if a certain condition is true. Otherwise, it quietly ignores the code block inside it. This simple control structure forms the basis for decision-making in Java programs.
if (condition) {
// code to execute if condition is true
}
The condition must evaluate to a boolean value, either true or false. If it's true, the code inside the block executes. If not, the program simply skips that block and moves on.
Use case: A software engineer is developing a system that checks a student's test score. If the score is greater than 5, the system should acknowledge that the student has passed the minimum benchmark.
Program description: In this Java program, we simulate a scenario where a student's test score is stored in the variable x. Let us assume that the value in x is set to 10 for this run. In real-time, we may get the value for x as input from the user. The if statement checks whether the score is greater than 5. If this condition is true, the program prints a message indicating that the score is above the benchmark. Regardless of the condition, the program ends with a general message indicating completion.
public class IfExample1 {
public static void main(String[] args) {
int score = 10;
if (score > 5) {
System.out.println("score is greater than 5");
}
System.out.println("Program ended.");
}
}
score is greater than 5
Program ended.
Since x > 5 evaluates to true, the message inside the if block is printed. The final statement runs regardless of the condition.
public class IfExample2 {
public static void main(String[] args) {
int age = 15;
if (age >= 18) {
System.out.println("You can vote.");
}
System.out.println("Check completed.");
}
}
Check completed.
Here, the condition age >= 18 is false. So, the code inside the if block is ignored. Only the final line runs.
{ } even for single-line blocks: It improves readability and helps avoid logical bugs.= instead of ==: Assignment vs. comparison. if (a = 10) is invalid. Use == for comparison.if block.if condition: if (x > 0); { ... } causes logic errors. That semicolon ends the if prematurely.public class IfExample3 {
public static void main(String[] args) {
int marks = 45;
if (marks > 50)
System.out.println("You passed!");
System.out.println("This message is outside the if.");
}
}
This message is outside the if.
Only the first print statement is inside the if. The second line executes every time. To avoid such surprises, always use curly braces.
The Java if statement allows your programs to make decisions. Always test your conditions, keep your code readable, and avoid syntax pitfalls. As you progress, if will serve as a foundation for more advanced constructs like else, else if, and switch statements.
int num = 5;
if (num < 10) {
System.out.println("Less than 10");
}
System.out.println("Done");if statement must always evaluate to a boolean value.if statements in Java?int age = 15;
if (age >= 18) {
System.out.println("You can vote.");
}
System.out.println("Check completed.");int x = 10;
if (x > 5);
{
System.out.println("Greater than 5");
}int marks = 85;
if (marks > 50)
System.out.println("You passed!");
System.out.println("This message is outside the if.");