ITC02
Chapter 4
Conditional Statements in Java: Control Flow
with If, Else, and Switch-Case
01
Discussion
Agenda
02 Overview
If statements
If-Else statements
If-Else if statements
Nested If statements
Switch-case sttatements
CHAPTER 4
If
IF STATEMENT
The if statement is the simplest control flow statement in
Java. It allows you to execute a block of code only if a specific
condition is true.
SYNTAX
Explanation: If the condition is true, the block of code inside
the if statement will run. If the condition is false, the block will
be skipped.
EXAMPLE
If-Else
IF-ELSE STATEMENT
The if-else statement adds an alternative block of code that
will execute if the condition is false.
SYNTAX
Explanation: If the condition is true, the code inside the if
block will run. If it's false, the code inside the else block will
run.
EXAMPLE
If-Else If
IF-ELSE IF STATEMENT
This is used when you need to check multiple conditions. The if
statement checks the first condition, and the else if statements
check additional conditions. The else block is optional and
runs if none of the conditions are met.
SYNTAX
Explanation: The first condition (condition1) is checked. If it's
false, the next condition (condition2) is checked, and so on. If
none of the conditions are true, the else block will run.
EXAMPLE
Nested If
NESTED IF STATEMENT
A nested if statement is an if statement inside another if or
else block. This allows you to test more complex conditions.
SYNTAX
Explanation: The outer if condition is evaluated first. If it’s
true, the inner if condition is checked. The inner block of code
will run only if both conditions are true.
EXAMPLE
Switch-case
SWITCH-CASE STATEMENT
The switch-case statement is used to simplify the decision-
making process when there are multiple possible outcomes
based on the value of a single variable. It is an alternative to
using many if-else-if statements and is often cleaner and
more readable.
Explanation: The
switch statement
SYNTAX compares the value of
the variable against
the values in the case
statements. When a
match is found, the
corresponding block of
code runs. The break
statement prevents the
execution from "falling
through" to the next
case. If no cases match,
the default block
(optional) is executed.
EXAMPLE
SUMMARY
SUMMARY - DIFFERENCE
FEATURE IF IF-ELSE IF-ELSE-IF NESTED IF SWITCH-CASE
Matches a single
Evaluates a single Evaluates a condition Evaluates multiple Evaluates multiple
Basic Structure variable against multiple
condition with an alternate block conditions in sequence conditions with nesting
values
When multiple When comparing a
When only one condition When there are two When conditions depend
Usage conditions need to be variable to discrete
needs to be checked outcomes (true/false) on each other
checked values
Single condition with an Multiple conditions Conditions inside other Value matching (integer,
Condition Type Single condition
alternative path evaluated one by one conditions string, enum)
Checking one variable
Binary decisions Multiple, ordered Complex, dependent
Best for Simple, single conditions against several constant
(true/false cases) condition checks conditions
values
Checks each condition Jumps directly to
Executes if the condition Executes the else block Checks outer condition
Execution Flow one by one until a match matching case,
is true if the condition is false first, then inner ones
is found bypassing others
SUMMARY - DIFFERENCE
IF: Best for checking a single condition.
IF-ELSE: Ideal for binary choices (either/or).
IF-ELSE-IF: Works well when there are multiple conditions to
evaluate in sequence.
NESTED IF: Useful when conditions are dependent on one another,
but can become complex.
SWITCH-CASE: Best suited for checking the value of a single
variable against multiple discrete values, offering cleaner and faster
execution for this purpose.
QUESTIONS
ITC02
Thanks for
attending!
ITC02 - PROGRAMMING 1
SEATWORK:
True or False (10 items)
1. The if statement in Java allows you to execute a block of code only when a specific
condition is false.
2. In a switch-case statement, each case must end with a break statement to prevent fall-
through to the next case.
3. A nested if statement is when one if statement is placed inside another if or else block.
4. The if-else statement can only handle two possible outcomes.
5. The if-else-if statement is used to evaluate a condition with more than two possible
outcomes.
6. The switch-case statement can handle conditions with logical operators like && and ||.
7. The default block in a switch-case statement is mandatory and always required.
8. You can use any data type with the switch-case statement in Java, including int, char, and
String.
9. A switch-case statement is generally faster and more efficient than using multiple if-else
statements when checking the value of a single variable.
10. In an if-else statement, if the if condition is true, the else block is still executed.
SEATWORK:
ANSWERS:
1. False
2. True
3. True
4. True
5. True
6. False
7. False
8. True
9. True
10. False