2 ControlStatements If Switch
2 ControlStatements If Switch
CONTROL STATEMENTS
ROSALYN P. REYES
Control Statements |
Control Flow
Java provides three types of control flow statements.
If Statements
the "if" statement is used to evaluate a condition
4 types of if-statements
1. Simple if statement
2. if-else statement
3. if-else-if ladder
4. Nested if-statement
Decision Making Statements
1. Simple if Statements
It is the most basic statement among all control flow statements in Java. It
evaluates a Boolean expression and enables the program to enter a block of
code if the expression evaluates to true.
Decision Making Statements
1. Simple if Statements
It is the most basic statement among all control flow statements in Java. It
evaluates a Boolean expression and enables the program to enter a block of
code if the expression evaluates to true.
Decision Making Statements
1. Simple if Statements
Decision Making Statements
2. if-else Statements
The if-else statement is an extension to the if-statement, which uses another
block of code, i.e., else block. The else block is executed if the condition of
the if-block is evaluated as false.
Decision Making Statements
2. if-else Statements
Decision Making Statements
2. if-else Statements
Decision Making Statements
2. if-else Statements
Decision Making Statements
2. if-else Statements
Decision Making Statements
3. if-else-if ladder:
The if-else-if statement contains the
if-statement followed by multiple
else-if statements.
Decision Making Statements
3. if-else-if ladder:
Decision Making Statements
4. Nested if statements:
In nested if-statements, the if statement can contain a if or if-else statement
inside another if or else-if statement.
Decision Making Statements
4. Nested if statements:
In nested if-statements, the if statement can contain a if or if-else statement
inside another if or else-if statement.
Decision Making Statements
4. Nested if statements:
Decision Making Statements
4. Nested if statements:
In nested if-statements, the if statement can contain a if or if-else statement
inside another if or else-if statement.
switch
CASE
Decision Making Statements
2. Switch Statements
Switch statements are similar to if-else-if statements. The
switch statement contains multiple blocks of code called
cases and a single case is executed based on the variable
which is being switched. The switch statement is easier to
use instead of if-else-if statements. It also enhances the
readability of the program.
Decision Making Statements
Switch Statements
Points to be noted about switch statement:
•The case variables can be int, short, byte, char, or enumeration.
•Cases cannot be duplicate
•Default statement is executed when any of the case doesn't match
the value of expression. It is optional.
•Break statement terminates the switch block when the condition is
satisfied. It is optional, if not used, next case is executed.
•While using switch statements, we must notice that the case
expression will be of the same type as the variable. However, it will
also be a constant value.
Decision Making Statements
Switch Statements
Decision Making Statements
Switch Statements
Decision Making Statements
Switch Statements