Академический Документы
Профессиональный Документы
Культура Документы
CT038-3-2 OODJ
Simple if statement
if..else statement
Nested if.else Statements
switch Statement
while Statement
do Statement
for Statement
break & continue Statement
Decision Making
Learning outcomes
At the end of this lecture you should be
able to:
Identify and construct branching control
structures.
Identify and construct looping control
structures.
CT038-3-2 OODJ
Decision Making
CT038-3-2 OODJ
Decision Making
Introduction
A Java program is a set of statements,
which are normally executed sequentially
in the order in which they appear.
However, we can have situations where
we may have to change the order of the
execution of statements based on certain
conditions, or repeat a group of
statements until certain conditions are
met.
CT038-3-2 OODJ
Decision Making
CT038-3-2 OODJ
Decision Making
CT038-3-2 OODJ
Decision Making
Simple if statement
The general form of a simple if statement:
if (test expression)
{
statement block;
}
statement x;
CT038-3-2 OODJ
Decision Making
CT038-3-2 OODJ
Decision Making
Entry
Test
Expression
?
True
Statement block
False
Statement x
Next statement
CT038-3-2 OODJ
Decision Making
Example
..
if (category == sports)
{
marks = marks + bonus_marks;
}
System.out.println(marks);
CT038-3-2 OODJ
Decision Making
if..else statement
The general form of a simple if statement:
if (test expression)
{
true block statement(s);
}
else
{
false block statement(s);
}
statement x;
CT038-3-2 OODJ
Decision Making
Example
if (code == 1)
boy = boy + 1;
else
girl = girl + 1;
CT038-3-2 OODJ
Decision Making
Decision Making
Example:
if (employee = full_time)
{
if (salary > 3000)
bonus = 0.15 * salary;
else
bonus = 0.10 * salary;
}
else
{
bonus = 0.05 * salary;
}
salary = salary + bonus;
CT038-3-2 OODJ
Decision Making
switch Statement
The switch statement tests the value of a
given variable (expression) against a list of
case values.
When a match is found, a block of
statements associated with case is
executed.
CT038-3-2 OODJ
Decision Making
switch (expression)
{
case value-1:block-1; break;
case value-2:block-2; break;
CT038-3-2 OODJ
Decision Making
CT038-3-2 OODJ
Decision Making
Example:
switch (subject_id)
{
case A : System.out.println(English);
case S : System.out.println(Science);
case C : System.out.println(History);
default : System.out.println(Sorry these
are the only subjects listed);
}
CT038-3-2 OODJ
Decision Making
while Statement
General form:
Initialization;
while (test condition)
{
Body of the loop
}
CT038-3-2 OODJ
Decision Making
Example:
..
sum=0;
n=1;
while (n <=10)
{
sum = sum + n*n;
n=n+1;
}
System.out.println(sum =+sum);
CT038-3-2 OODJ
Decision Making
do Statement
Execute the body of the loop first before
the test is performed.
General form:
Initialization;
do
{
Body of the loop
}
while (test condition)
CT038-3-2 OODJ
Decision Making
for Statement
General form:
for(initialization; test condition; increment)
{
Body of the loop
}
Example:
sum=0
for (n=1; n<=10; n=n+1)
{
sum = sum + n*n;
}
CT038-3-2 OODJ
Decision Making
CT038-3-2 OODJ
Decision Making
while (..)
{
.
.
if (condition)
break;
....
..
}
..
CT038-3-2 OODJ
Decision Making
do
{
.
.
if (condition)
break;
....
..
} while (..)
..
CT038-3-2 OODJ
Decision Making
for (..)
{
.
.
if (condition)
break;
....
..
}
..
CT038-3-2 OODJ
Decision Making
CT038-3-2 OODJ
Decision Making
do
{
.
if (.)
continue;
....
..
} while (test condition)
CT038-3-2 OODJ
Decision Making
Decision Making
Summary
Selection statements supported by Java:
if statement
switch statement
The loop constructs are:
while statement
do statement
for statement
CT038-3-2 OODJ
Decision Making