Recurrence (Looping) - Looping, is section that assigned to conduct activities to repeat a process in accordance with the desired.
The types of looping in C++, among others
- for, used in a known amount iteration count. (how many times the loop will be done).
- while, is function to repeat a statement during the condition is true.
- do-while, almost the same as while loops, but the condition of the do-while loop will be checked after the statement is executed. If the condition is true then the statement is executed again, but if the condition is false, the statement is not executed.
Looping Condition
Conditions used in the iteration has some rules so that there are no error in program. First, the loop condition variable must be of type integer or char. Variable of type float is not recommended though it could. If forced to use a condition variable of type float / fractions, one should check the logic of the conditions carefully.Especially for loop, the parameters can be removed one, two, or even all of them. But stopping factors should remain there. These factors can be placed in a looping statement.
Nested Looping
Nested looping, is a loop inside another loop. The deeper loop will be processed first until it finish, then the outer loop will be increased, then work again on a deeper iteration starting from the initial value onwards. Looping inside another loop is allowed in almost all programming languages.Nested looping example can be seen in the following programs:
#include <iostream>
using namespace std;
int main ()
{
int nested1, nested2;
int start1 = 1, start2;
cout << "input nested1 value : ";
cin >> nested1;
cout << "\ninput nested2 value : ";
cin >> nested2;
if ( ( nested1 > 0) && ( nested2 > 0) )
{
while ( start1 <= nested1 )
{
for ( start2=1; start2 <= nested2; start2++)
{
cout << "repetition to - " << start1 * start2 << endl;
}
start1++;
}
}
}
Source code example using While and For Nested looping
In the example above, there is a loop inside another loop. All forms of looping can be put on another looping form. As in the example, for loop was inside the while loop. Statement line 18, will run as many as nested1 x nested2 (according to user input).
Course of the example is as follows. First, the value for nested1 and nested2 variable inserted by the user are 2 and 3. Then if condition will check whether the value of each nested is in positive integer form. If true then the while loop will be executed. Inside the while loop condition will be checked whether the value start1 less than the value nested1 (2). Because the condition is true, statement inside the while loop is executed. Inside the while loop statement, there are loop again, then the looping condition is checked. Is start2 value less than nested2? Because the condition is true then for statement is executed. Which displays a message to the screen repetition to - ...
For statement will be executed until the value of start3 more than nested2 value. In other words the for loop statement, run 3 times. Meanwhile the while loop statement is executed as much as 2x. In conclusion total statement line 28 executed as much as 6x (2 x 3). Program output as follows:
input nested1 value : 2
input nested2 value : 3
repetition to - 1
repetition to - 2
repetition to - 3
repetition to - 4
repetition to - 5
repetition to - 6
Display of Running Program using While and For Nested looping
Using the FOR loop :
1. Task 1 : Creating an algorithm
- Step 1 : Variable Declaration
Explanation: Declaring a variable to be used as an index to display the row
|number 1, for example, i, of type integer.
i : integer - Step 2 : Initialitation
Explanation: Filling the variable i to the initial value, aims to create a prelooping.
i <- 1 - Step 3 : Process
Explanation: Contains all the parts of the loop that is performed repeatedly. This process is displays number 1 that repeated 6 times. In this step, iteration occurs in the loop, which is an increament condition so that the loop can continue to run.
for i <- 1 to 6 do
write ("1")
end for - Step 4 : Finalization
Explanation: Stoping condition of the loop is very important to prevent the loop that runs continuously.
2. Task 2 : Create the program
- Step 1 : Variable declaration
int i; - Step 2 : Initialization + Process + Finalization
for(i=1; i<=6; i++) {
cout<<”1”;
}
#include <iostream>
#include <conio.h>
using namespace std;
int main () {
int i;
for(i=1; i<=6; i++)
cout << "1";
getch( );
return 0;
}
3. Task 3 : Compilation and program execution
Display of Running Program using FOR looping
By using WHILE loop :
1. Task 1 : Creating an Algorithm
- Step 1 : Variable declaration
Explanation: Declaring a variable to be used as an index to display the row number 1, for example, i of type integer.
i : integer - Step 2 : Initialitation
Explanation: Filling the variable i to the initial value, aims to create a prelooping conditions.
i ← 1 ; - Step 3 : Process
Explanation: Contains all the parts of the loop that is performed repeatedly. This process is displays number 1 that repeated 6 times. In this step, iteration occurs in the loop, which is an increament condition so that the loop can continue to run.
while (i<=6) do {
cout<<"1".
} - Step 4 : Finalization
Explanation: Stoping condition of the loop is very important to prevent the loop that runs continuously.
i ← i+1. -> iterasi
end while;
- Step 1 : Variable declaration
int i=1; - Step 2 : Initialization + Process + Finalization
while ((i<=6) {
cout<<"1";
i=i+1.
}
#include <iostream>Source code example using While looping
#include <conio.h>
using namespace std;
int main () {
int i;
i=1
while (i<=6);{
cout << "1";
i=i+1;
}
getch( );
return 0;
}
3. Task 3 : Compilation and program execution
Display of Running Program using While looping
By using DO-WHILE loop :
1. Task 1 : Create an algorithm
- Step 1 : Variable declaration
Explanation: Declaring a variable to be used as an index to display the row number 1, for example, i of type integer.
i : integer; - Step 2 : Initialization
Explanation: Filling the variable i to the initial value, aims to create a prelooping conditions.
i ← 1 - Step 3 : Process
Explanation: Contains all the parts of the loop that is performed repeatedly. This process is displays number 1 that repeated 6 times. In this step, iteration occurs in the loop, which is an increament condition so that the loop can continue to run.
repeat {
write(“1”);
i ← i+1;
until (i=6) - Step 4 : Finalization
Explanation: Stoping condition of the loop is very important to prevent the loop that runs continuously.
- Step 1 : Variable declaration
int i=1; - Step 2 : Initialization + Process + Finalization
do {
cout<<"1";
i=i+1;
}
while (i<=6);
return 0;
}
#include <iostream>Source code example using Do-While looping
#include <conio.h>
using namespace std;
int main () {
int i;
i=1
do {
cout << "1";
i=i+1;
}
while (i<=6);
getch( );
return 0;
}
3. Task 3 : Compilation and program execution
Display of Running Program using While looping
Exercise
1. Create a program to display a sequence of numbers using for loop, while, do. . while1 2 3 4 5 6 7 8 9
2. Create a program using for loop, while loop and do-while loop to display rows of letters
A B C D E F . .
3. Create a program to display stars, within the limits of the amount of numbers that inputed by user, for example, the input limits is 4, as in the following figure
4. Create a program to display multiplication table, as an example:
* 1 2 3 4 5
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 9 12 15
4 4 8 12 16 20
5 5 10 15 20 25
EmoticonEmoticon