Chapter 6: Decision making and Branching
6.1 Introduction
When a program breaks the sequential flow and jumps to another part of the code, it is
called branching.
When the branching is based on a particular condition, it is known as conditional
branching.
If branching takes place without any decision, it is known as unconditional branching.
Java language possesses such decision-making capabilities and supports the following
statements known as control or decision-making statements.
1. if statement
2. switch statement
3. conditional operator statement
6.2 Decision Making with IF Statement
is a two-way decision statement
The general form is:
if (test condition)
Allows the computer to evaluate the expression first and then depending on whether
the value of the expression is true or false, it transfers the control to a particular statement.
Test False
Condi
tion
True
6.3 Simple IF Statement
The general form of a simple if statement is
if ( test expression)
{
Statement- block;
}
Statement-x;
Statement-block may be a single statement or a group of statements.
If the test expression is true, the statement-block will be executed
Otherwise the statement-block will be skipped and the execution will jump to the
statement-x.
, Test True
Condi
tion
False
6.4 The IF… ELSE Statement
True False
Test
Condi
tion
The general form is:
6.1 Introduction
When a program breaks the sequential flow and jumps to another part of the code, it is
called branching.
When the branching is based on a particular condition, it is known as conditional
branching.
If branching takes place without any decision, it is known as unconditional branching.
Java language possesses such decision-making capabilities and supports the following
statements known as control or decision-making statements.
1. if statement
2. switch statement
3. conditional operator statement
6.2 Decision Making with IF Statement
is a two-way decision statement
The general form is:
if (test condition)
Allows the computer to evaluate the expression first and then depending on whether
the value of the expression is true or false, it transfers the control to a particular statement.
Test False
Condi
tion
True
6.3 Simple IF Statement
The general form of a simple if statement is
if ( test expression)
{
Statement- block;
}
Statement-x;
Statement-block may be a single statement or a group of statements.
If the test expression is true, the statement-block will be executed
Otherwise the statement-block will be skipped and the execution will jump to the
statement-x.
, Test True
Condi
tion
False
6.4 The IF… ELSE Statement
True False
Test
Condi
tion
The general form is: