Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Java Control Statements – Part I

APT 3040 – FALL 2020


Prof. Audrey Mbogho
USIU-Africa
Control Structures

Normally, statements in a program are executed one
after the other in the order in which they’re written.

This process is called sequential execution.

Various Java statements, enable you to specify that
the next statement to execute is not necessarily the
next one in sequence.

This is called transfer of control.

These notes are taken from Java How to Program by


Paul & Harvey Deitel (10th or 11th Edition)

2
Control Structures

All programs could be written in terms of only
three control structures
– the sequence structure,
– the selection structure
– the repetition structure.

3
Sequence Structure

The sequence structure is built into Java.

Unless directed otherwise, the computer executes
Java statements one after the other in the order in
which they’re written—that is, in sequence.

Java lets you have as many actions as you want
in a sequence structure.

As we’ll soon see, anywhere a single action may
be placed, we may place several actions in
sequence.

4
Sequence Structure Activity
Diagram

5
Activity Diagram

A UML activity diagram models the workflow (also
called the activity) of a portion of a software
system.

Activity diagrams are composed of symbols, such
as action state symbols (rectangles with their left
and right sides replaced with outward arcs),
diamonds and small circles.

These symbols are connected by transition
arrows, which represent the flow of the activity—
that is, the order in which the actions should occur.

6
Activity Diagram

The solid circle at the top of the activity diagram
represents the initial state.

The solid circle surrounded by a hollow circle at the
bottom of the diagram represents the final state.

The rectangles with a folded upper right corner are
UML notes (like comments in Java)—explanatory
remarks that describe the purpose of symbols in
the diagram.

A dotted line connects each note with the element it
describes.

7
Selection Statements

Java has three types of selection statements.

The if statement either performs (selects) an
action, if a condition is true, or skips it, if the
condition is false.

The if ... else statement performs an action if a
condition is true and performs a different action if
the condition is false.

The switch statement performs one of many
different actions, depending on the value of an
expression.

8
Repetition Statements

Java provides three repetition statements (also called iteration
statements or looping statements) that enable programs to
perform statements repeatedly as long as a condition (called
the loop-continuation condition) remains true.

The repetition statements are the while, do ... while, for and
enhanced for statements.

The while and for statements perform the action (or group of
actions) in their bodies zero or more times—if the loop-
continuation condition is initially false, the action (or group of
actions) will not execute.

The do ... while statement performs the action (or group of
actions) in its body one or more times.

9
Conditions and Relational Operators

A condition is an expression that can be true or false

Conditions in control statements can be formed by using
the relational operators shown in the table below.

== Equals to
!= Does not equal to
< Is less than
> Is greater than
<= Is less than or equal to
>= Is greater than or equal to

10
Single Selection with the if
Statement

Programs use selection statements to choose
among alternative courses of action.

For example, suppose that the passing grade
on an exam is 60.

We can write

11
Activity Diagram for the if Statement

12
Activity diagram for the if Statement

The diamond, or decision symbol, indicates that a
decision is to be made.

The workflow continues along a path determined
by the symbol’s associated guard conditions,
which can be true or false.

Each transition arrow emerging from a decision
symbol has a guard condition (specified in square
brackets next to the arrow).

If a guard condition is true, the workflow enters the
action state to which the transition arrow points.

13
Exercise: BMI Calculator

Write a program to calculate a person’s BMI
given their height and weight. The formula is
shown below. The program should report the
status of the user (underweight, normal,
overweight, or obese).

14
Double Selection with if-else

The if single-selection statement performs an
indicated action only when the condition is true.

Otherwise, the action is skipped.

The if ... else double-selection statement allows
you to specify an action to perform when the
condition is true and another action when the
condition is false.

15
Double Selection with if-else

For example, it makes sense to take one action
if the student has passed and a different action
if not.

16
UML Activity Diagram for if-else

17
Nested if-else

A program can test multiple cases by nesting
if ... else statements inside others.

For example, we can write a nested if ... else
statement that prints A for exam grades greater
than or equal to 90, B for grades 80 to 89, C for
grades 70 to 79, D for grades 60 to 69 and F for
all other grades.

18
Nested if-else

19
Exersise: BMI Calculator

Repeat the BMI Calculator exercise but this
time, use a nested if-else statement in place of
the if statement used earlier. This is a better
solution.

20
Blocks

To include several statements in the body of an
if (or the body of an else for an if ... else
statement), enclose the statements in braces.

Statements contained in a pair of braces (such
as the body of a method) form a block.

A block can be placed anywhere that a single
statement can be placed.

21
Blocks

22
Logic Errors

Syntax errors (e.g., when one brace in a block is
left out of the program) are caught by the compiler.

A logic error (e.g., when both braces in a block are
left out of the program) has its effect at execution
time.

A fatal logic error causes a program to fail and
terminate prematurely. This is also called a run-
time error.

A nonfatal logic error allows a program to continue
executing but causes it to produce incorrect results.

23
The Conditional Operator

Java provides the conditional operator ( ?: )
that can be used in place of an if ... else
statement.

This can make your code shorter and clearer.

The conditional operator is Java’s only ternary
operator (i.e., an operator that takes three
operands).

Together, the operands and the ?: symbol form
a conditional expression.

24
The Conditional Operator


The first operand is a Boolean expression (i.e.,
evaluates to true or false).

The second operand is the value of the conditional
expression if the Boolean expression is true

The third operand is the value of the conditional
expression if the Boolean expression evaluates to
false.

25

You might also like