If Statement
This is the simplest form of conditional statement and is fundamental to decision-making in Java programming.
if
statement allows you to execute a code block only if a specified condition is true.
Syntax
if (condition) {
// code to be executed if condition is true
}
condition: This is a boolean expression that evaluates to either true
or false
. If the condition evaluates to true
, then the block of code enclosed in curly braces {}
will be executed.
Simple if
This is the most used control statement in all programming languages. Let us see an example code snippet.
public class Main {
public static void main(String[] args) {
int number = 10;
// if statement
if (number > 5) {
System.out.println("The number is greater than 5.");
}
System.out.println("This statement is always executed.");
}
}
// Outputs:
// The number is greater than 5.
// This statement is always executed.
In this example:
- The condition
number > 5
is evaluated. - Since
number
is 10, which is greater than 5, the condition evaluates totrue
. - Therefore, the code inside the
if
block (System.out.println("The number is greater than 5.");
) is executed. - The subsequent
System.out.println("This statement is always executed.");
line is outside theif
block and is executed regardless of the condition.
Multiple Conditions
You can use multiple if
statements to check multiple conditions independently:
public class Main {
public static void main(String[] args) {
int number = 10;
if (number > 5) {
System.out.println("The number is greater than 5.");
}
if (number < 20) {
System.out.println("The number is less than 20.");
}
}
}
// Outputs:
// The number is greater than 5.
// The number is less than 20.
In this example:
- The first
if
statement checks ifnumber
is greater than 5. - The second
if
statement checks ifnumber
is less than 20. - Both conditions are true, so both messages are printed.
Nested if
Statements
if
statements can be nested inside other if
statements to handle more complex conditions:
public class Main {
public static void main(String[] args) {
int number = 10;
if (number > 5) {
if (number < 15) {
System.out.println("The number is between 5 and 15.");
}
}
}
}
// Outputs:
// The number is between 5 and 15.
In this example:
- The outer
if
statement checks ifnumber
is greater than 5. - If true, the inner
if
statement then checks ifnumber
is less than 15. - Since both conditions are true, "The number is between 5 and 15." is printed.
Logical Operators
You can also use logical operators to combine multiple conditions in a single if
statement:
public class Main {
public static void main(String[] args) {
int number = 10;
if (number > 5 && number < 15) {
System.out.println("The number is between 5 and 15.");
}
}
}
// Outputs:
// The number is between 5 and 15.
In this example:
- The condition
number > 5 && number < 15
uses the logical AND operator (&&
) to check if both conditions are true. - Since
number
is 10, both conditions are true, and the message is printed.
Summary
- The
if
condition allows you to execute code based on whether a condition is true. - It is a basic but powerful tool for decision-making in Java.
- Conditions are boolean expressions that evaluate to
true
orfalse
. - You can use multiple
if
statements, nestedif
statements, and logical operators to handle more complex decision-making.
Using the if
statement effectively enables you to control the flow of your Java programs based on dynamic conditions.