Name:Nurul Hidayah Binti Jamahiri (112100522) Wan Syuhaidah Binti Mohd Haidir (112100660)
Name:Nurul Hidayah Binti Jamahiri (112100522) Wan Syuhaidah Binti Mohd Haidir (112100660)
Nested Loop
We have seen the advantages of using various methods of iteration, or looping. Now let's take a look at what happens when we combine looping procedures.
The placing of one loop inside the body of another loop is called nesting. When you "nest" two loops, the outer loop takes control of the number of complete repetitions of the inner loop. While all types of loops may be nested, the most commonly nested loops are for loops. nested loops
Let's look at an example of nested loops at work. We have all seen web page counters that
resemble the one shown below .Your car's odometer works in a similar manner.
This counter (if it worked properly) and your car's odometer are little more than seven or eight nested for loops, each going from 0 to 9. The far-right number iterates the fastest, visibly moving from 0 to 9 as you drive your car or increasing by one as people visit a web site. A for loop which imitates the movement of the far-right number is shown below: for(num1 = 0; num1 <= 9; num1++) { cout << num1 << endl; } The far-right number, however, is not the only number that is moving. All of the other numbers are moving also, but at a much slower pace. For every 10 numbers that move in the column on the right, the
adjacent column is incremented by one. The two nested loops shown below may be used to imitate the movement of the two far-right numbers of a web counter or an odometer:
The number of digits in the web page counter or the odometer determine the number of nested loops needed to imitate the process.
When working with nested loops, the outer loop changes only after the inner loop is completely finished(or is interrupted.). Let's take a look at a trace of two nested loops. In order to keep the trace manageable, the number of iterations have been shortened. for(num2 = 0; num2 <= 3; num2++) { for(num1 = 0; num1 <= 2; num1++) { cout<< num2<< " " << num1<< endl; } } Memory int num2 0 int num1 0 1 2 3 end loop 1 0 1 2 3 end loop 2 0 1 Screen 0 0 0 1 1 1 2 2 2 3 3 3 0 1 2 0 1 2 0 1 2 0 1 2
2 3 end loop 3 0 1 2 3 end loop 4 end loop Remember, in the memory, for loops will register a value one beyond (or the step beyond) the requested ending value in order to disengage the loop.
The while loop Its format is: while (expression) statement and its functionality is simply to repeat statement while the condition set in expression is true. For example, we are going to make a program to countdown using a while-loop: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // custom countdown using while #include <iostream> using namespace std; int main () { int n; cout << "Enter the starting number > "; cin >> n; while (n>0) { cout << n << ", "; --n; } cout << "FIRE!\n"; return 0; } Enter the starting number > 8 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
When the program starts the user is prompted to insert a starting number for the countdown. Then the while loop begins, if the value entered by the user fulfills the condition n>0 (that n is greater than zero) the block that follows the condition will be executed and repeated while the condition (n>0) remains being true.
The whole process of the previous program can be interpreted according to the following script (beginning in main):
1. User assigns a value to n 2. The while condition is checked (n>0). At this point there are two possibilities: * condition is true: statement is executed (to step 3) * condition is false: ignore statement and continue after it (to step 5) 3. Execute statement: cout << n << ", "; --n; (prints the value of n on the screen and decreases n by 1) 4. End of block. Return automatically to step 2 5. Continue the program right after the block: print FIRE! and end program.
When creating a while-loop, we must always consider that it has to end at some point, therefore we must provide within the block some method to force the condition to become false at some point, otherwise the loop will continue looping forever. In this case we have included --n; that decreases the value of the variable that is being evaluated in the condition (n) by one - this will eventually make the condition (n>0) to become false after a certain number of loop iterations: to be more specific, when n becomes0, that is where our while-loop and our countdown end. Of course this is such a simple action for our computer that the whole countdown is performed instantly without any practical delay between numbers.
The do-while loop Its format is: do statement while (condition); Its functionality is exactly the same as the while loop, except that condition in the do-while loop is evaluated after the execution of statement instead of before, granting at least one execution of statement even if condition is never fulfilled. For example, the following example program echoes any number you enter until you enter 0. 1 2 3 4 5 6 7 8 9 10 // number echoer #include <iostream> using namespace std; int main () { unsigned long n; do { cout << "Enter number (0 to end): "; Enter number (0 to end): 12345 You entered: 12345 Enter number (0 to end): 160277 You entered: 160277 Enter number (0 to end): 0 You entered: 0
11 cin >> n; 12 cout << "You entered: " << n << "\n"; 13 } while (n != 0); 14 return 0; 15 }
The do-while loop is usually used when the condition that has to determine the end of the loop is determined within the loop statement itself, like in the previous case, where the user input within the block is what is used to determine if the loop has to end. In fact if you never enter the value 0 in theprevious example you can be prompted for more numbers forever.
The for loop Its format is: for (initialization; condition; increase) statement; and its main function is to repeat statement while condition remains true, like the while loop. But in addition, the for loop provides specific locations to contain an initialization statement and an increase statement. So this loop is specially designed to perform a repetitive action with a counter which is initialized and increased on each iteration. It works in the following way:
1. initialization is executed. Generally it is an initial value setting for a counter variable. This is executed only once. 2. condition is checked. If it is true the loop continues, otherwise the loop ends and statement is skipped (not executed). 3. statement is executed. As usual, it can be either a single statement or a block enclosed in braces { }. 4. finally, whatever is specified in the increase field is executed and the loop gets back to step 2.
Here is an example of countdown using a for loop: 1 2 3 4 5 6 7 8 9 // countdown using a for loop #include <iostream> using namespace std; int main () { for (int n=10; n>0; n--) { cout << n << ", "; } cout << "FIRE!\n"; 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
10 return 0; 11 }
The initialization and increase fields are optional. They can remain empty, but in all cases the semicolon signs between them must be written. For example we could write: for (;n<10;) if we wanted to specify no initialization and no increase; or for (;n<10;n++) if we wanted to include an increase field but no initialization (maybe because the variable was already initialized before). Optionally, using the comma operator (,) we can specify more than one expression in any of the fields included in a for loop, like ininitialization, for example. The comma operator (,) is an expression separator, it serves to separate more than one expression where only one is generally expected. For example, suppose that we wanted to initialize more than one variable in our loop: 1 for ( n=0, i=100 ; n!=i ; n++, i-- ) 2{ 3 // whatever here... 4}
This loop will execute for 50 times if neither n or i are modified within the loop:
n starts with a value of 0, and i with 100, the condition is n!=i (that n is not equal to i). Because n is increased by one and idecreased by one, the loop's condition will become false after the 50th loop, when both n and i will be equal to 50.