Conditional Statements
Summary of Conditional Statements
Statement | Purpose | Use Case Example |
---|---|---|
if | Executes code if a condition is true. | Check if a number is positive. |
if-else | Executes one block if true, another if false. | Check if a number is positive or negative. |
if-else-if ladder | Tests multiple conditions in sequence. | Determine if a number is positive, negative, or zero. |
Nested if | Adds further conditions within an if . | Check if a number is positive and even/odd. |
switch | Executes one block from many cases. | Perform actions based on days of the week. |
Ternary operator | Shortens simple if-else . | Assign a value based on whether a condition is true (e.g., positive or negative). |
1. if Statement
The if
statement is used to execute a block of code only if a specified condition is true.
Syntax
if (condition) {
// Code to execute if condition is true
}
Example
int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
}
2. if-else Statement
The if-else
statement is used to execute one block of code if the condition is true and another block if the condition is false.
Syntax
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Example
int number = -5;
if (number > 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is negative.");
}
3. if-else-if Ladder
The if-else-if
ladder is used when there are multiple conditions to check.
Syntax
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if none of the conditions are true
}
Example
int number = 0;
if (number > 0) {
System.out.println("The number is positive.");
} else if (number < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}
4. Nested if Statements
An if
statement can be used inside another if
statement to perform additional checks.
Syntax
if (condition1) {
if (condition2) {
// Code to execute if both conditions are true
}
}
Example
int number = 5;
if (number > 0) {
if (number % 2 == 0) {
System.out.println("The number is positive and even.");
} else {
System.out.println("The number is positive and odd.");
}
}
5. switch Statement
The switch
statement is used to execute one block of code from many options based on the value of a variable or expression.
Syntax
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
...
default:
// Code to execute if no case matches
}
Example
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}
Key Points
- Use the
break
statement to terminate a case. Withoutbreak
, the program executes all subsequent cases (fall-through). - The
default
block is optional but executes if no case matches.
Comparison of if-else
and switch
Feature | if-else | switch |
---|---|---|
Use Case | For complex conditions or ranges. | For discrete values. |
Data Types | Supports all data types. | Supports int , char , String , enum . |
Readability | Less readable for multiple conditions. | More readable for multiple discrete cases. |
6. Conditional (Ternary) Operator
The conditional operator (? :
) is a shorthand for if-else
.
Syntax
condition ? expression1 : expression2;
Example
int number = 5;
String result = (number > 0) ? "Positive" : "Negative";
System.out.println(result); // Output: Positive