0% found this document useful (0 votes)
34 views

Control Flow Statements

The document discusses different types of control flow statements in programming, including decision-making statements like if-else and switch statements, looping statements like while, do-while and for loops, and branching statements like break, continue and System.exit. It provides examples of using each statement type and explains their functionality.

Uploaded by

Jose Rey CId
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Control Flow Statements

The document discusses different types of control flow statements in programming, including decision-making statements like if-else and switch statements, looping statements like while, do-while and for loops, and branching statements like break, continue and System.exit. It provides examples of using each statement type and explains their functionality.

Uploaded by

Jose Rey CId
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Lesson 3

Control flow statements

Programming
Grade in Computer Engineering
Outline

1. Decision-making statements
if-else
switch
2. Looping statements
while
do-while
for
3. Branching statements
break
continue
System.exit
[email protected]
Outline

1. Decision-making statements
if-else
switch
2. Looping statements
while
do-while
for
3. Branching statements
break
continue
System.exit
[email protected]
1. Decision-making statements
Control flow instructions

Java program instructions are executed from


top to bottom, in the order that they appear,
starting from the first sentence inside the main
method
Control flow instructions break up this sequence
Blocks of code are executed or not depending
on some conditions
Conditions are boolean expressions –relational
and logical expressions
[email protected]
1. Decision-making statements
Second grade equation revisited

EquationSimple.java [email protected]
1. Decision-making statements
if-else

The most basic control flow


instruction

If the condition is true, the


block of code associated to the
if part is executed

If the condition is false, the


block of code associated to the
else part is executed

if (<boolean expression>) {
if-else statements can be <statement(s)>

nested } else {
<statement(s)>
}
[email protected]
1. Decision-making statements
Second grade equation revisited

EquationBetter.java

[email protected]
1. Decision-making statements
Second grade equation revisited

[email protected]
1. Decision-making statements

Start

Read
a, b, c

a == 0 ?
True False

Calculate
b == 0 ? discriminant d
True False

Calculate x as in
a 1st grade d >= 0 ?
False True
equation

Calculate x1, x2 Calculate x1, x2


Print
Print x as imaginary with the
error solutions formula

v
Print Print
x1, x2 x1, x2

End
1. Decision-making statements
Second grade equation revisited

EquationBest.java

Exception management
try-catch is used to detect if there was an
InputMismatchException error when
reading from the keyboard
If this error happens, the catch block is
executed; otherwise, the catch block is not
executed

[email protected]
1. Decision-making statements
Second grade equation revisited

Nested conditions
Nested if-else allow the programmer to define
multiple execution branches depending on
variable values

[email protected]
1. Decision-making statements
Second grade equation revisited

[email protected]
1. Decision-making statements
if-else

The else part is optional If there is only one instruction


inside the block, the braces
can be removed
if (<boolean expression>)
<statement>

if (<boolean expression>)
<statement>
else
<statement>

Do not forget the ;

if (<boolean expression>) { if else instructions can be


<statement(s)>
nested
}

[email protected]
1. Decision-making statements
if-else-if

Nested if-else instructions can


be arranged to implement
mutually exclusive execution
branches
Only one of the blocks is
executed
If none of the conditions is true,
the final else block is executed

if (<boolean expression 1>) {


<statement(s)>
} else if (<boolean expression 2>) {
<statement(s)>
} else if (<boolean expression 3>) {

} else {
<statement(s)>
}

[email protected]
1. Decision-making statements
if-else-if

ExamplesIfElseIf.java

[email protected]
Outline

1. Decision-making statements
if-else
switch
2. Looping statements
while
do-while
for
3. Branching statements
break
continue
System.exit
[email protected]
1. Decision-making statements
switch

Allows for multiple execution paths, depending on the value of the


switch variable
The switch variable must be integer, character, string or enumerated
value
If the switch variable is equal to the value of a case, the sentences
following the case are executed until a break is found
If a case block does not have a break, the execution continues in the
next case, even if it is false!
Braces are not required to delimit each case
If no case is true, the default block (if defined –since it is optional)
is executed
Deciding between switch and if-else-if is based on the type of
the switch variable and code readability

[email protected]
1. Decision-making statements
switch

switch (<variable>) {
case <value 1>:
<sentence(s)>
[break;]
case <value 2>:
<sentence(s)>
[break;]
default:
<sentence(s)>
}

[email protected]
1. Decision-making statements
switch

BasicConditionals.java

[email protected]
1. Decision-making statements
switch + if-else

BasicConditionals.java

[email protected]
Outline

1. Decision-making statements
if-else
switch
2. Looping statements
while
do-while
for
3. Branching statements
break
continue
System.exit
[email protected]
2. Looping statements
General

Loops repeat sequentially the instructions in a


block of code while a condition holds
When the block of code associated to a loop
instruction is finished, the condition is tested
If the condition holds, the block is executed again
If the condition does not hold, the execution
continues with the instructions below the block

[email protected]
2. Looping statements
while

Continually executes a block


of statements while a
particular condition is true
When the program reaches
the while statement for the
first time,
If the condition is true, the
block of code associated to
the while is executed
If the condition is false, the
program continues by the
sentence below the block
After finishing the while while(<boolean expression>) {

block, the condition is tested }


<sentence(s)>

again
[email protected]
2. Looping statements
while

ExamplesWhile.java
[email protected]
2. Looping statements
while

ExamplesWhile.java
[email protected]
Outline

1. Decision-making statements
if-else
switch
2. Looping statements
while
do-while
for
3. Branching statements
break
continue
System.exit
[email protected]
2. Looping statements
do-while

Executes a block of statements;


repeat the execution if the
condition is true
First, the block of code is statements true
executed
If the condition is true, the block condition
of code associated to the do
while is executed again false

After finishing the do while


block, the condition is tested
again
do{
If the condition is false, the <sentence(s)>
execution continues by the first } while(<boolean expression>);
instruction below the block
[email protected]
2. Looping statements
do-while

ExamplesDoWhile.java
[email protected]
2. Looping statements
do-while

ExamplesDoWhile.java
[email protected]
2. Looping statements
while and do-while

ExamplesWhileDoWhile.java
[email protected]
Outline

1. Decision-making statements
if-else
switch
2. Looping statements
while
do-while
for
3. Branching statements
break
continue
System.exit
[email protected]
2. Looping statements
for
Executes a block of statements; repeat the
execution if the condition is true (similar to
while)

Additionally, performs more operations pre-block statement

Pre-block statement (optional)


Usually, a variable declaration
condition
After-block statement (optional)
Usually, a variable increment/decrement true

The first time, the pre-block statement is statements


executed false

post-block statement
If the condition is true, the associated block
of code

After finishing the block, the after-block


statement is executed

If the condition is true, the block is


executed again for ([pre-block]; <expression>; [post-block]){
<statement(s)>
If the condition is false, the execution }
continues by the next instruction below the
for [email protected]
2. Looping statements
for

pre-block and post-block statements


The pre-block statement is usually a variable definition,
whereas the post-block statement is usually a
modification of the pre-block variable

for / while equivalence


for loops can be implemented with while
loops, and vice versa

ExamplesFor.java
[email protected]
2. Looping statements
for

Loop instructions can be nested –with the


following observations:
The inner loop must be included inside the outer
loop
For each value of the counter of the outer
instruction, the counter of the inner instruction
takes all its values
Outer loop: i = {0, …, n-1}
Inner loop: j = {0, …, m-1}
n x m pairs (i, j) inside the loop

[email protected]
2. Looping statements
for

for and arrays


for loops are frequently used to traverse all
the elements of an array: initialization,
operations with elements, etc.

ExamplesFor.java
[email protected]
Outline

1. Decision-making statements
if-else
switch
2. Looping statements
while
do-while
for
3. Branching statements
break
continue
System.exit
[email protected]
3. Branching statements
break, continue, System.exit
break terminates the execution of the loop
After the break, the execution continues in the statement just below
the loop
break;

continue jumps to the next iteration of the loop


After the continue, the execution continues just before the evaluation
of the condition of the loop
continue;

System.exit(-1)terminates the execution of the program


System.exit is used to finish the program when a wrong condition
due to the parameters or the input values is detected
System.exit(-1);
[email protected]
3. Branching statements
break, continue, System.exit

ExamplesBranching.java
[email protected]
Outline

1. Decision-making statements
if-else
switch
2. Looping statements
while
do-while
for
3. Branching statements
break
continue
System.exit
[email protected]
Summary
Control flow statements
Conditional instructions
if-else
A block of code is executed depending on a condition
switch
A block of code is executed depending on the value of a single variable
Cases have a special behavior

Loop instructions
while
A block of code is repeated depending on a condition
do-while
A block of code is repeated depending on a condition
The block is executed at least once
for
A block of code is repeated depending on a condition
Additional statements are executed the first time the for is reached (pre-statement) and each time the for block
is finished (post-statement)

Branching instructions
break
The loop is finished; execution continues below the block
continue
The loop is restarted; the condition is evaluated again
System.exit
The program is finished
[email protected]
Additional lectures
Control flow statements

Recommended lectures
The JavaTM Tutorials. Oracle, Control flow statements [link]
H. M. Deitel, P. J. Deitel. Java: How to Program. Prentice Hall,
2007 (7th Edition), Chapters 4 [link], 5 [link],
K. Sierra, B. Bates. Head First Java. O'Reilly Media, 2005 (2nd
Edition), Chapter 5 [link]
B. Eckel. Thinking in Java. Prentice Hall, 2002 (3rd Edition),
Chapter 3 [link]
I. Horton. Beginning Java 2, JDK 5 Edition. Wrox, 2004 (5th
Edition), Chapter 3 [link]

[email protected]
Programming – Grado en Ingeniería Informática
Authors
Of this version:
Juan Gómez Romero

Based on the work by:


Ángel García Olaya
Manuel Pereira González
Silvia de Castro García
Gustavo Fernández-Baillo Cañas

42

You might also like