⬅ Previous Topic
Java Conditional StatementsNext Topic ⮕
Java if-else Statement⬅ Previous Topic
Java Conditional StatementsNext Topic ⮕
Java if-else StatementThe 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 sboolean 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.
public class IfExample1 {
public static void main(String[] args) {
int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
}
System.out.println("Program ended.");
}
}
The number is positive.
Program ended.
Since number > 0
evaluates to true
, the message inside the if
block is printed. The last line runs regardless of the condition.
number
is assigned 10
.number > 0
becomes true
.if
block.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 = 85;
if (marks > 50)
System.out.println("You passed!");
System.out.println("This message is outside the if.");
}
}
You passed!
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.");
⬅ Previous Topic
Java Conditional StatementsNext Topic ⮕
Java if-else StatementYou 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.