Sei sulla pagina 1di 31

CT038-3-2

Object Oriented Development with


Java
Decision Making

Topic & Structure of the lesson

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

Key terms you must be able to use


If you have mastered this topic, you
should be able to use the following terms
correctly in your assignments and exams:

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

Java has decision making capabilities and


supports the following statements known s
control or decision making statements.
if statement
switch statement

CT038-3-2 OODJ

Decision Making

The if statement may be implemented in


different forms depending on the
complexity of conditions to be tested.
Simple if statement
ifelse statement
Nested if.else statement

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

The statement block may be a single


statement or a group of statements.
If the expression is true, the statement
block will be executed.
Otherwise the statement block will be
skipped and the execution will jump to
statement x.

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

Nested if.else Statements


if (test condition 1)
if (test condition 2)
{
Statement 1;
else
{
statement 2;
}
}
else
{
statement 3;
}
Statement x;
CT038-3-2 OODJ

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;

default: default block; break;


}
statement x

CT038-3-2 OODJ

Decision Making

The expression is an integer or


characters.
The break statement signals the end of a
particular case and causes and exit from
switch statement.

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

Break & Continue Statement


When the break statement is
encountered inside a loop, the loop is
exited and the program continues with
next statement after the loop.
The continue causes the loop to be
continued with the next iteration after
skipping any statements in between.

CT038-3-2 OODJ

Decision Making

while (..)
{
.
.
if (condition)
break;
....
..
}
..
CT038-3-2 OODJ

Decision Making

Exit from loop

do
{
.
.
if (condition)
break;
....
..
} while (..)
..
CT038-3-2 OODJ

Decision Making

Exit from loop

for (..)
{
.
.
if (condition)
break;
....
..
}
..
CT038-3-2 OODJ

Decision Making

Exit from loop

while (test condition)


{
.
.
if ()
continue;
....
..
}

CT038-3-2 OODJ

Decision Making

do
{
.
if (.)
continue;
....
..
} while (test condition)

CT038-3-2 OODJ

Decision Making

for (initialization;test condition;increment)


{
.
.
if (.)
continue;
....
..
}
..
CT038-3-2 OODJ

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

Potrebbero piacerti anche