You are on page 1of 6

Name:Nurul Hidayah Binti Jamahiri(112100522) Wan Syuhaidah Binti Mohd Haidir(112100660


Tajuk:Gelung/Loop & Gelung Bersarang/Nested Loop

Iteration structures (loops)
Loops have as purpose to repeat a statement a certain number of times or while a condition is fulfilled. The for loop is a looping statement that enables you to repeat a set of instructions based on the condition. It is used when the number of loops is known before the first loop

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

num1 <= 2. When working with nested loops. for(num2 = 0. In order to keep the trace manageable. num2 <= 3. num2++) { for(num1 = 0.adjacent column is incremented by one. } } 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 . Let's take a look at a trace of two nested loops. the outer loop changes only after the inner loop is completely finished(or is interrupted. num1++) { cout<< num2<< " " << num1<< endl. the number of iterations have been shortened. 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.).

2 3 end loop 3 0 1 2 3 end loop 4 end loop Remember. 7. --n. 1. 3. 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. 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. while (n>0) { cout << n << ". 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. in the memory. } cout << "FIRE!\n". ". For example. . cin >> n. FIRE! When the program starts the user is prompted to insert a starting number for the countdown. 4. int main () { int n. 6. Then the while loop begins. return 0. } Enter the starting number > 8 8. 2. cout << "Enter the starting number > ". for loops will register a value one beyond (or the step beyond) the requested ending value in order to disengage the loop. 5.

End of block. --n. ". (prints the value of n on the screen and decreases n by 1) 4. Continue the program right after the block: print FIRE! and end program. therefore we must provide within the block some method to force the condition to become false at some point. int main () { unsigned long n. we must always consider that it has to end at some point. The do-while loop Its format is: do statement while (condition). granting at least one execution of statement even if condition is never fulfilled. The while condition is checked (n>0). except that condition in the do-while loop is evaluated after the execution of statement instead of before. that decreases the value of the variable that is being evaluated in the condition (n) by one . User assigns a value to n 2. 1 2 3 4 5 6 7 8 9 10 // number echoer #include <iostream> using namespace std. For example. Of course this is such a simple action for our computer that the whole countdown is performed instantly without any practical delay between numbers.this will eventually make the condition (n>0) to become false after a certain number of loop iterations: to be more specific. 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 . otherwise the loop will continue looping forever. Execute statement: cout << n << ". Return automatically to step 2 5. do { cout << "Enter number (0 to end): ". When creating a while-loop. 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. In this case we have included --n. when n becomes0. that is where our while-loop and our countdown end. Its functionality is exactly the same as the while loop. the following example program echoes any number you enter until you enter 0.The whole process of the previous program can be interpreted according to the following script (beginning in main): 1.

4. otherwise the loop ends and statement is skipped (not executed). 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. 2. increase) statement. finally.11 cin >> n. n>0. like in the previous case. 3. 10. 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. It works in the following way: 1. n--) { cout << n << ". int main () { for (int n=10. 9. The for loop Its format is: for (initialization. and its main function is to repeat statement while condition remains true. 8. where the user input within the block is what is used to determine if the loop has to end. 4. 14 return 0. Generally it is an initial value setting for a counter variable. As usual. 13 } while (n != 0). 1. it can be either a single statement or a block enclosed in braces { }. 7. But in addition. FIRE! . ". If it is true the loop continues. 12 cout << "You entered: " << n << "\n". condition. condition is checked. 3. 2. initialization is executed. the for loop provides specific locations to contain an initialization statement and an increase statement. In fact if you never enter the value 0 in theprevious example you can be prompted for more numbers forever. whatever is specified in the increase field is executed and the loop gets back to step 2. 5. This is executed only once. } cout << "FIRE!\n". like the while loop. So this loop is specially designed to perform a repetitive action with a counter which is initialized and increased on each iteration. 6. statement is executed.

They can remain empty.n<10. suppose that we wanted to initialize more than one variable in our loop: 1 for ( n=0. but in all cases the semicolon signs between them must be written. using the comma operator (. The comma operator (.n++) if we wanted to include an increase field but no initialization (maybe because the variable was already initialized before). Because n is increased by one and idecreased by one.) if we wanted to specify no initialization and no increase.) 2{ 3 // whatever here. n++.. when both n and i will be equal to 50.. 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. Optionally. 11 } The initialization and increase fields are optional. For example. .10 return 0. like ininitialization. For example we could write: for (. i=100 . and i with 100. it serves to separate more than one expression where only one is generally expected. or for (. i-.) is an expression separator. n!=i . the loop's condition will become false after the 50th loop. for example.) we can specify more than one expression in any of the fields included in a for loop. the condition is n!=i (that n is not equal to i).n<10.