Faizan Riaz Presentation
Faizan Riaz Presentation
Presented By:
► Imran Khan
► Mohammad Fu’ad Mustafa
► Abdul Rehman
► Faizan Riaz
Overview
► Looping
Introduction
► While loops
Syntax
Examples
► Infinite Loops
► Examples using while loops
► do.. while loops
Examples
► For loops
Definition
Examples
Readability
Nesting
► Break – Continue
► Exiting
Introduction
true
► A while statement has the
following syntax: statement
while (condition)
statement;
test;
or
test; loop; test;
or
test; loop; test; loop, test;
and so on. The last test on each line must have delivered the
result FALSE; earlier tests must have delivered the result TRUE.
► The body of a while loop must eventually make the condition false
► If not, it is an infinite loop, which will execute until the user interrupts
the program
► This is a common type of logical error -- always double check that
your loops will terminate normally
int counter = 1
while (counter>0) {
System.out.println("Counter:" + counter);
counter++;
}
Examples of "while" loops
To add together the sequence 1 + 1/2 + 1/4 + 1/8 + ...
until the terms we are adding together are smaller than 0.00001
// We need float variables
float Term = 1.0f, Total = 0.0f;
int Counter = 0;
final float DELTA = 0.00001f;
while ( Term > DELTA ) {
// Add the next term to the total
Total += Term;
// Halve the term
Term /= 2.0; // or *= 0.5
// Count them
Counter++;
} // end while Term > 0.00001 loop
System.out.println( "Total " + Total );
System.out.println( "Number" + Counter );
“do” loops
► The "while" loops above performed the test first, and then executed the loop.
Sometimes you may wish to test at the end of the loop, after the execution of the
statements in the body of the loop (and hence to execute the loop body always at
least once). In JAVA we use what is referred to as a "do" loop, written as follows:
int Number = 1;
do
{ ....;
Number++;
}
while ( Number <= 10 );
► In this case the value of "Number" would be 1 the first time round the loop, and
10 the last time. The condition (exactly as in a "while" loop) is still contained in
round brackets, and is still TRUE to continue with another execution of the loop
body, and FALSE to leave the loop. Remember that there is a semicolon after the
condition, terminating the whole statement. We have one more semicolon overall
than the equivalent "while" loop.
“do” loops
Syntax:
[initialization]
do {
[statements]
[iteration]
} while ( boolean-
expression )
“do” loops
► The pattern of execution in this case can be summarized as follows.
loop; test
loop; test; loop; test
loop; test; loop; test; loop; test
► The code in the above programming example could also be written
int Number = 1;
do {
....;
} while ( ++Number <= 10 );
► This is the form of combined "increment and test" that most C++
programmers that write JAVA would use. The "++" must, of course, be in
front of the "Number" in this case.
► It is generally safer to test at the start of a loop;
► "while" loops are generally safer and more common than "do" loops.
Examples of “do” loops
► Toread in positive numbers until a zero is encountered, and print the
biggest one.
► The loop executes with "month" and "year" taking the pairs of values
[1900,0], [1900,1], [1900,2], ..., [1900,11], [1901,0], [1901,1], ...,
[1901,11], ..., [1999,11] in turn in that order.
The "break" statement
► In any of the above loops, the special statement "break" causes
the loop to be abandoned, and execution continues following the
closing curly brace.
while ( i > 0 ) {
....;
if ( j == .... ) {
break; // abandon the loop
} ....;
} // end of the loop body
System.out.println( "continues here ...");
► The program continues after the end of the loop.
► Within a nested loop, "break" causes the inner most loop to be
abandoned.
The "continue" statement
► Wikipedia.com
► Google.com
► Scrab.com
Thanks for Paying
Attention