Control Flow Statements
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
EquationSimple.java [email protected]
1. Decision-making statements
if-else
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
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
if (<boolean expression>)
<statement>
else
<statement>
[email protected]
1. Decision-making statements
if-else-if
[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
[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
[email protected]
2. Looping statements
while
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
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)
post-block statement
If the condition is true, the associated block
of code
ExamplesFor.java
[email protected]
2. Looping statements
for
[email protected]
2. Looping statements
for
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;
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
42