Advance Java Programming
06 – Java Control Flow – Iteration & Branching
Ghalib University
Computer Science Department
Asrar Ahmad Ehsan
Fall 2017
Agenda
• Iterations
• While loop
• Do-while loop
• For loop
• Foreach loop
• Branching
Iterations
• Looping mechanisms in Java are similar to those in other high-level languages
like C/C++, C#.
• The four Java loop statements are:
1) while statement
2) do-while statement
3) for statement.
4) foreach statement
• The same terminology is used with Java as with other languages.
• The code that is repeated in a loop is called the body of the loop.
• Each repetition of the loop body is called an iteration of the loop.
While Loop
• The while loop is Java’s most fundamental looping statement.
• It repeats a statement or block while its controlling expression is true.
• The while statement works by first evaluating the expression, which must result in a
boolean or Boolean value.
• If the value is false, the interpreter skips the statement associated with the loop and moves to
the next statement in the program.
• If it is true, however, the statement that forms the body of the loop is executed, and the
expression is reevaluated.
• This cycle continues while the expression remains true (i.e., until it evaluates to
false), at which point the while statement ends, and the interpreter moves on to the
next statement.
• You can create an infinite loop with the syntax while(true).
While Loop
While Loop Example
Code Output
Do-While Loop
A do loop is much like a while loop, except that the loop expression is tested at
the bottom of the loop rather than at the top.
This means that the body of the loop is always executed at least once.
Do-While Loop
Do-While Loop Example
Code Output
while VS. do-while
Notice a couple of differences between the do loop and the more ordinary
while loop.
1. In while-do-loops, the condition is checked before the code is executed the first
time.
2. In do-while loops, the condition is checked afterwards ,Because of that, the code
of a do-while-loop is executed at least once.
3. do-while loop unlike the while loop is terminated with a semicolon.
4. The do loop is much less commonly used than its while cousin because, in
practice, it is unusual to encounter a situation where you are sure you always
want a loop to execute at least once.
For loop
Most loops follow more convenient approach than the while and do loops, which
have
a) a counter, or state variable of some kind, that is initialized before the loop starts
b) tested to determine whether to execute the loop body
c) and then incremented or updated somehow at the end of the loop body before the test
expression is evaluated again.
The for statement in java follows the above construct which is often more
convenient than the while and do loops.
The initialization, test, and update steps are the three crucial manipulations of a
loop variable, and the for statement makes these three steps an explicit part of
the loop
For loop syntax
This for loop is basically equivalent to the following while loop:
For loop
The following for loop prints the numbers 0 to 9, just as the previous while and do
loops have done:
The for loop supports some additional syntax that makes it even more convenient to
use. Because many loops use their loop variables only within the loop, the for loop
allows the initialize expression to be a full variable declaration, so that the variable is
scoped to the body of the loop and is not visible outside of it. For example:
For loop
Furthermore, the for loop syntax does not restrict you to writing loops that use only
a single variable. Both the initialize and update expressions of a for loop can use a
comma to separate multiple initializations and update expressions. For example:
The initialize, test, and update expressions of a for loop are all optional;
Only the semicolons that separate the expressions are required.
If the test expression is omitted, it is assumed to be true. Thus, you can write an
infinite loop as for(;;).
For loop example
Code Output
Foreach loop
Java’s for loop works well for primitive types, but it is needlessly hard for handling
collections of objects. Instead, an alternative syntax known as a foreach loop is
used for handling collections of objects that need to be looped over.
The foreach loop uses the keyword for followed by an opening parenthesis,
a variable declaration (without initializer), a colon, an expression, a closing
parenthesis, and finally the statement (or block) that forms the body of the loop:
Foreach loop
Branching
Branching statements allows us to redirect the flow of program execution.
Java uses three different types of Branching:
1) break
2) continue
3) return
Break
The break keyword is used to stop the entire loop.
The break keyword must be used inside any loop or a switch statement.
The break keyword will stop the execution of the innermost loop and start
executing the next line of code after the block.
Code
Break
The break keyword will stop the execution of the innermost loop and start
executing the next line of code after the block.
What happens, if we have nested loops????
The break statement can also be followed by the name of a containing labeled
statement. When used in this form, break causes the Java interpreter to
immediately exit the named block.
Break
Continue
We use the continue statement to skip the current iteration of a for, while, or
do-while loop.
Code Output
Iteration 4 is escaped!!!
Return
You use return to exit from the current method.
Data type of the value returned by return must match the type of the
method’s declared return value.
Method declared void has no return value.
Code
Any Questions?