ConditionalIf Lecture (1)
ConditionalIf Lecture (1)
1. Simple if Statement
2. if-else Statement
3. if-else-if Ladder
4. Nested if Statement
5. Ternary Operator
1. Simple if Statement
The simple if statement checks a condition and executes the block of code inside the if statement if
the condition is true.
Syntax:
if (condition) {
// Code to be executed if condition is true
}
Example:
int age = 18;
if (age >= 18)
{
System.out.println("You are eligible to vote.");
}
Use Case:
Checking whether a user meets a specific condition, such as age eligibility.
Validating inputs before processing further.
2. if-else Statement
The if-else statement provides an alternative path of execution when the if condition is false.
Syntax:
if (condition)
{
// Code to be executed if condition is true
}
else
{
// Code to be executed if condition is false
}
Example:
int number = 10;
if (number % 2 == 0)
{
System.out.println("The number is even.");
}
else
{
System.out.println("The number is odd.");
}
Use Case:
Providing a fallback action if the primary condition is not met.
Handling success and failure scenarios.
3. if-else-if Ladder
The if-else-if ladder allows multiple conditions to be checked sequentially. As soon as one condition is
true, the corresponding block of code is executed, and the remaining conditions are skipped.
Syntax:
if (condition1)
{
// Code to be executed if condition1 is true
}
else if (condition2)
{
// Code to be executed if condition2 is true
}
else
{
// Code to be executed if none of the conditions are true
}
Example:
int marks = 85;
if (marks >= 90)
{
System.out.println("Grade: A");
}
else if (marks >= 80)
{
System.out.println("Grade: B");
}
else if (marks >= 70)
{
System.out.println("Grade: C");
}
else
{
System.out.println("Grade: F");
}
Use Case:
Grading systems based on marks.
Handling multiple conditions in a structured way.
4. Nested if Statement
A nested if statement is an if statement inside another if statement. It allows more complex
conditions to be checked.
Syntax:
if (condition1)
{
if (condition2)
{
// Code to be executed if both conditions are true
}
}
Example:
int age = 20;
int income = 50000;
if (age > 18)
{
if (income > 40000)
{
System.out.println("You are eligible for a loan.");
}
}
Use Case:
Handling dependent conditions.
Validating multiple criteria before proceeding.
5. Ternary Operator
The ternary operator is a shorthand for the if-else statement. It uses the ? and : symbols to
perform conditional checks in a single line.
Syntax:
variable = (condition) ? valueIfTrue : valueIfFalse;
Example:
int number = 5;
String result = (number % 2 == 0) ? "Even" : "Odd";
System.out.println("The number is " + result);
Use Case:
Simplifying if-else statements when assigning values to variables.
Writing concise and readable code.
Incorrect: if (x = 5)
Correct: if (x == 5)
2. Unreachable Code: Ensuring that all possible conditions are covered to avoid unreachable
code.
3. Missing Braces: Although optional for single-line statements, always use braces {} for clarity.
Conclusion
Conditional statements are crucial for making decisions in Java programs. Understanding the different
types of if statements and their appropriate use cases is essential for writing dynamic and responsive
code. By mastering conditional statements, developers can create more flexible, efficient, and
maintainable applications.