Iteration Logic (Repetitive Flow)
The Iteration logic employs a loop which involves a repeat statement
followed by a module known as the body of a loop.
The two types of these structures are:
 Repeat-For Structure
This structure has the form:
 Repeat for i = A to N by I:
 [Module]
 [End of loop]
Here, A is the initial value, N is the end value and I is the increment.
The loop ends when A>B. K increases or decreases according to
the positive and negative value of I respectively.
Repeat-For Flow
 Repeat-While Structure
It also uses a condition to control the loop. This structure has the
form:
 Repeat while condition:
 [Module]
 [End of Loop]
Repeat While Flow
For loop is a repetition control structure which allows us to write a loop
that is executed a specific number of times. The loop enables us to
perform n number of steps together in one line.
For(init,condition,update)
For(i=1;1<10;i++) i=0 i=1,i=2 9<10,10<10
Syntax:
for (initialization expr; test expr; update expr)
{
// body of the loop
// statements we want to execute
}
The various parts of the For loop are:
1. Initialization Expression: In this expression we have to initialize
the loop counter to some value.
Example:
int i=1;
2. Condition: In this expression we have to test the condition. If the
condition evaluates to true then we will execute the body of the loop
and go to update expression. Otherwise, we will exit from the for
loop.
Example:
i <= 10
3. Update Expression: After executing the loop body, this expression
increments/decrements the loop variable by some value.
Example:
i++;
How does a For loop executes?
1. Control falls into the for loop. Initialization is done
2. The flow jumps to Condition
3. Condition is tested.
a. If Condition yields true, the flow goes into the Body
b. If Condition yields false, the flow goes outside the loop
4. The statements inside the body of the loop get executed.
5. The flow goes to the Updation
6. Updation takes place and the flow goes to Step 3 again
7. The for loop has ended and the flow has gone outside.
Flow chart for loop (For Control Flow):
Example 1: This program will try to print “Hello World” 5 times. The
program will execute in the following manner:
// C program to illustrate for loop
#include <stdio.h>
int main()
{
int i = 0;
// Writing a for loop
// to print Hello World 5 times
for (i = 1; i <= 5; i++) {
printf("Hello Worldn");
}
return 0;
}
Output:
Hello World
Hello World
Hello World
Hello World
Hello World
Dry-Running Example 1:
1. Program starts.
2. i is initialized with value 1.
3. Condition is checked. 1 <= 5 yields true.
3.a) "Hello World" gets printed 1st time.
3.b) Updation is done. Now i = 2.
4. Condition is checked. 2 <= 5 yields true.
4.a) "Hello World" gets printed 2nd time.
4.b) Updation is done. Now i = 3.
5. Condition is checked. 3 <= 5 yields true.
5.a) "Hello World" gets printed 3rd time
5.b) Updation is done. Now i = 4.
6. Condition is checked. 4 <= 5 yields true.
6.a) "Hello World" gets printed 4th time
6.b) Updation is done. Now i = 5.
7. Condition is checked. 5 <= 5 yields true.
7.a) "Hello World" gets printed 5th time
7.b) Updation is done. Now i = 6.
8. Condition is checked. 6 <= 5 yields false.
9. Flow goes outside the loop to return 0
Example2
// C program to illustrate for loop
#include <stdio.h>
int main()
{
int i = 0;
// Writing a for loop
// to print odd numbers upto N
for (i = 1; i <= 10; i += 2) {
printf("%dn", i);
}
return 0;
}
Loops in C: come into use when we need to repeatedly execute a block of
statements.
During the study of ‘for’ loop in C or C++, we have seen that the number
of iterations is known beforehand, i.e. the number of times the loop body is
needed to be executed is known to us. The while loop in C/C++ is used in
situations where we do not know the exact number of iterations of loop
beforehand. The loop execution is terminated on the basis of the test condition.
Syntax:
while (test_expression)
{
// statements
update_expression;
}
The various parts of the While loop are:
1. Test Expression: In this expression we have to test the condition. If the
condition evaluates to true then we will execute the body of the loop and go
to update expression. Otherwise, we will exit from the while loop.
Example:
i <= 10
2. Update Expression: After executing the loop body, this expression
increments/decrements the loop variable by some value.
Example:
i++;
How does a While loop executes?
1. Control falls into the while loop.
2. The flow jumps to Condition
3. Condition is tested.
a. If Condition yields true, the flow goes into the Body.
b. If Condition yields false, the flow goes outside the loop
4. The statements inside the body of the loop get executed.
5. Updation takes place.
6. Control flows back to Step 2.
7. The do-while loop has ended and the flow has gone outside.
Flowchart while loop (for Control Flow):
Example 1: This program will try to print “Hello World” 5 times.
// C program to illustrate while loop
#include <stdio.h>
int main()
{
// initialization expression
int i = 1;
// test expression
while (i < 6) {
printf("Hello Worldn");
// update expression
i++;
}
return 0;
}
Output:
Hello World
Hello World
Hello World
Hello World
Hello World
Dry-Running Example 1: The program will execute in the following manner.
1. Program starts.
2. i is initialized with value 1.
3. Condition is checked. 1 < 6 yields true.
3.a) "Hello World" gets printed 1st time.
3.b) Updation is done. Now i = 2.
4. Condition is checked. 2 < 6 yields true.
4.a) "Hello World" gets printed 2nd time.
4.b) Updation is done. Now i = 3.
5. Condition is checked. 3 < 6 yields true.
5.a) "Hello World" gets printed 3rd time
5.b) Updation is done. Now i = 4.
6. Condition is checked. 4 < 6 yields true.
6.a) "Hello World" gets printed 4th time
6.b) Updation is done. Now i = 5.
7. Condition is checked. 5 < 6 yields true.
7.a) "Hello World" gets printed 5th time
7.b) Updation is done. Now i = 6.
8. Condition is checked. 6 < 6 yields false.
9. Flow goes outside the loop to return 0.
Example 2:
// C program to illustrate while loop
#include <stdio.h>
int main()
{
// initialization expression
int i = 1;
// test expression
while (i > -5) {
printf("%dn", i);
// update expression
i--;
}
return 0;
}
Output:
1
0
-1
-2
-3
-4
Loops in C come into use when we need to repeatedly execute a block of
statements.
Like while the do-while loop execution is also terminated on the basis of
a test condition. The main difference between a do-while loop and while loop is
in the do-while loop the condition is tested at the end of the loop body, i.e do-
while loop is exit controlled whereas the other two loops are entry controlled
loops.
Note: In do-while loop the loop body will execute at least once irrespective of
test condition.
Syntax:
do
{
// loop body
update_expression;
}
while (test_expression);
Note: Notice the semi – colon(“;”) in the end of loop.
The various parts of the do-while loop are:
1. Test Expression: In this expression we have to test the condition. If the
condition evaluates to true then we will execute the body of the loop and go
to update expression. Otherwise, we will exit from the while loop.
Example:
i <= 10
2. Update Expression: After executing the loop body, this expression
increments/decrements the loop variable by some value.
Example:
i++;
How does a do-While loop executes?
1. Control falls into the do-while loop.
2. The statements inside the body of the loop get executed.
3. Updation takes place.
4. The flow jumps to Condition
5. Condition is tested.
a. If Condition yields true, goto Step 6.
b. If Condition yields false, the flow goes outside the loop
6. Flow goes back to Step 2.
Flow Diagram do-while loop:
Example 1: This program will try to print “Hello World” depending on few
conditions.
// C program to illustrate do-while loop
#include <stdio.h>
int main()
{
// Initialization expression
int i = 2;
do {
// loop body
printf("Hello Worldn");
// Update expression
i++;
}
// Test expression
while (i < 1);
return 0;
}
Output:
Hello World
Dry-Running Example 1:
1. Program starts.
2. i is intialised to 2.
3. Execution enters the loop
3.a) "Hello World" gets printed 1st time.
3.b) Updation is done. Now i = 2.
4. Condition is checked. 2 < 2 yields false.
5. The flow goes outside the loop.
Example 2:
C // C program to illustrate do-while loop
#include <stdio.h>
int main()
{
// Initialization expression
int i = 1;
do {
// Loop body
printf("%dn", i);
// Update expression
i++;
}
// Test expression
while (i <= 5);
return 0;
}
Output:
1
2
3
4
5
For loop While loop
Initialization may be either in loop
statement or outside the loop. Initialization is always outside the loop.
Once the statement(s) is executed
then after increment is done.
Increment can be done before or after the
execution of the statement(s).
It is normally used when the number
of iterations is known.
It is normally used when the number of
iterations is unknown.
Condition is a relational expression.
Condition may be expression or non-zero
value.
It is used when initialization and
increment is simple. It is used for complex initialization.
For is entry controlled loop. While is also entry controlled loop.
for ( init ; condition ; iteration )
{
statement(s);
}
while ( condition )
{
statement(s);
}
while do-while
Condition is checked first then
statement(s) is executed.
Statement(s) is executed atleast once,
thereafter condition is checked.
It might occur statement(s) is executed
zero times, If condition is false.
At least once the statement(s) is
executed.
No semicolon at the end of while.
while(condition)
Semicolon at the end of while.
while(condition);
If there is a single statement, brackets
are not required. Brackets are always required.
Variable in condition is initialized
before the execution of loop.
variable may be initialized before or
within the loop.
while loop is entry controlled loop. do-while loop is exit controlled loop.
while(condition)
{
statement(s);
}
do
{
statement(s);
}
while(condition);

itretion.docx

  • 1.
    Iteration Logic (RepetitiveFlow) The Iteration logic employs a loop which involves a repeat statement followed by a module known as the body of a loop. The two types of these structures are:  Repeat-For Structure This structure has the form:  Repeat for i = A to N by I:  [Module]  [End of loop] Here, A is the initial value, N is the end value and I is the increment. The loop ends when A>B. K increases or decreases according to the positive and negative value of I respectively. Repeat-For Flow  Repeat-While Structure It also uses a condition to control the loop. This structure has the form:  Repeat while condition:
  • 2.
     [Module]  [Endof Loop] Repeat While Flow For loop is a repetition control structure which allows us to write a loop that is executed a specific number of times. The loop enables us to perform n number of steps together in one line. For(init,condition,update) For(i=1;1<10;i++) i=0 i=1,i=2 9<10,10<10
  • 3.
    Syntax: for (initialization expr;test expr; update expr) { // body of the loop // statements we want to execute } The various parts of the For loop are: 1. Initialization Expression: In this expression we have to initialize the loop counter to some value. Example: int i=1; 2. Condition: In this expression we have to test the condition. If the condition evaluates to true then we will execute the body of the loop and go to update expression. Otherwise, we will exit from the for loop. Example: i <= 10
  • 4.
    3. Update Expression:After executing the loop body, this expression increments/decrements the loop variable by some value. Example: i++; How does a For loop executes? 1. Control falls into the for loop. Initialization is done 2. The flow jumps to Condition 3. Condition is tested. a. If Condition yields true, the flow goes into the Body b. If Condition yields false, the flow goes outside the loop 4. The statements inside the body of the loop get executed. 5. The flow goes to the Updation 6. Updation takes place and the flow goes to Step 3 again 7. The for loop has ended and the flow has gone outside. Flow chart for loop (For Control Flow): Example 1: This program will try to print “Hello World” 5 times. The program will execute in the following manner:
  • 5.
    // C programto illustrate for loop #include <stdio.h> int main() { int i = 0; // Writing a for loop // to print Hello World 5 times for (i = 1; i <= 5; i++) { printf("Hello Worldn"); } return 0; } Output: Hello World Hello World Hello World Hello World Hello World Dry-Running Example 1: 1. Program starts. 2. i is initialized with value 1. 3. Condition is checked. 1 <= 5 yields true. 3.a) "Hello World" gets printed 1st time. 3.b) Updation is done. Now i = 2. 4. Condition is checked. 2 <= 5 yields true. 4.a) "Hello World" gets printed 2nd time. 4.b) Updation is done. Now i = 3. 5. Condition is checked. 3 <= 5 yields true. 5.a) "Hello World" gets printed 3rd time
  • 6.
    5.b) Updation isdone. Now i = 4. 6. Condition is checked. 4 <= 5 yields true. 6.a) "Hello World" gets printed 4th time 6.b) Updation is done. Now i = 5. 7. Condition is checked. 5 <= 5 yields true. 7.a) "Hello World" gets printed 5th time 7.b) Updation is done. Now i = 6. 8. Condition is checked. 6 <= 5 yields false. 9. Flow goes outside the loop to return 0 Example2 // C program to illustrate for loop #include <stdio.h> int main() { int i = 0; // Writing a for loop // to print odd numbers upto N for (i = 1; i <= 10; i += 2) { printf("%dn", i); } return 0; } Loops in C: come into use when we need to repeatedly execute a block of statements. During the study of ‘for’ loop in C or C++, we have seen that the number of iterations is known beforehand, i.e. the number of times the loop body is needed to be executed is known to us. The while loop in C/C++ is used in situations where we do not know the exact number of iterations of loop beforehand. The loop execution is terminated on the basis of the test condition.
  • 7.
    Syntax: while (test_expression) { // statements update_expression; } Thevarious parts of the While loop are: 1. Test Expression: In this expression we have to test the condition. If the condition evaluates to true then we will execute the body of the loop and go to update expression. Otherwise, we will exit from the while loop. Example: i <= 10 2. Update Expression: After executing the loop body, this expression increments/decrements the loop variable by some value. Example: i++;
  • 8.
    How does aWhile loop executes? 1. Control falls into the while loop. 2. The flow jumps to Condition 3. Condition is tested. a. If Condition yields true, the flow goes into the Body. b. If Condition yields false, the flow goes outside the loop 4. The statements inside the body of the loop get executed. 5. Updation takes place. 6. Control flows back to Step 2. 7. The do-while loop has ended and the flow has gone outside. Flowchart while loop (for Control Flow): Example 1: This program will try to print “Hello World” 5 times. // C program to illustrate while loop #include <stdio.h> int main()
  • 9.
    { // initialization expression inti = 1; // test expression while (i < 6) { printf("Hello Worldn"); // update expression i++; } return 0; } Output: Hello World Hello World Hello World Hello World Hello World Dry-Running Example 1: The program will execute in the following manner. 1. Program starts. 2. i is initialized with value 1. 3. Condition is checked. 1 < 6 yields true. 3.a) "Hello World" gets printed 1st time. 3.b) Updation is done. Now i = 2. 4. Condition is checked. 2 < 6 yields true. 4.a) "Hello World" gets printed 2nd time. 4.b) Updation is done. Now i = 3. 5. Condition is checked. 3 < 6 yields true. 5.a) "Hello World" gets printed 3rd time 5.b) Updation is done. Now i = 4. 6. Condition is checked. 4 < 6 yields true. 6.a) "Hello World" gets printed 4th time 6.b) Updation is done. Now i = 5.
  • 10.
    7. Condition ischecked. 5 < 6 yields true. 7.a) "Hello World" gets printed 5th time 7.b) Updation is done. Now i = 6. 8. Condition is checked. 6 < 6 yields false. 9. Flow goes outside the loop to return 0. Example 2: // C program to illustrate while loop #include <stdio.h> int main() { // initialization expression int i = 1; // test expression while (i > -5) { printf("%dn", i); // update expression i--; } return 0; } Output: 1 0 -1 -2 -3 -4 Loops in C come into use when we need to repeatedly execute a block of statements. Like while the do-while loop execution is also terminated on the basis of a test condition. The main difference between a do-while loop and while loop is in the do-while loop the condition is tested at the end of the loop body, i.e do-
  • 11.
    while loop isexit controlled whereas the other two loops are entry controlled loops. Note: In do-while loop the loop body will execute at least once irrespective of test condition. Syntax: do { // loop body update_expression; } while (test_expression); Note: Notice the semi – colon(“;”) in the end of loop. The various parts of the do-while loop are: 1. Test Expression: In this expression we have to test the condition. If the condition evaluates to true then we will execute the body of the loop and go to update expression. Otherwise, we will exit from the while loop. Example: i <= 10
  • 12.
    2. Update Expression:After executing the loop body, this expression increments/decrements the loop variable by some value. Example: i++; How does a do-While loop executes? 1. Control falls into the do-while loop. 2. The statements inside the body of the loop get executed. 3. Updation takes place. 4. The flow jumps to Condition 5. Condition is tested. a. If Condition yields true, goto Step 6. b. If Condition yields false, the flow goes outside the loop 6. Flow goes back to Step 2. Flow Diagram do-while loop: Example 1: This program will try to print “Hello World” depending on few conditions. // C program to illustrate do-while loop #include <stdio.h> int main() { // Initialization expression int i = 2;
  • 13.
    do { // loopbody printf("Hello Worldn"); // Update expression i++; } // Test expression while (i < 1); return 0; } Output: Hello World Dry-Running Example 1: 1. Program starts. 2. i is intialised to 2. 3. Execution enters the loop 3.a) "Hello World" gets printed 1st time. 3.b) Updation is done. Now i = 2. 4. Condition is checked. 2 < 2 yields false. 5. The flow goes outside the loop. Example 2: C // C program to illustrate do-while loop #include <stdio.h> int main() { // Initialization expression int i = 1; do { // Loop body printf("%dn", i); // Update expression i++; } // Test expression while (i <= 5);
  • 14.
    return 0; } Output: 1 2 3 4 5 For loopWhile loop Initialization may be either in loop statement or outside the loop. Initialization is always outside the loop. Once the statement(s) is executed then after increment is done. Increment can be done before or after the execution of the statement(s). It is normally used when the number of iterations is known. It is normally used when the number of iterations is unknown. Condition is a relational expression. Condition may be expression or non-zero value. It is used when initialization and increment is simple. It is used for complex initialization. For is entry controlled loop. While is also entry controlled loop. for ( init ; condition ; iteration ) { statement(s); } while ( condition ) { statement(s); }
  • 15.
    while do-while Condition ischecked first then statement(s) is executed. Statement(s) is executed atleast once, thereafter condition is checked. It might occur statement(s) is executed zero times, If condition is false. At least once the statement(s) is executed. No semicolon at the end of while. while(condition) Semicolon at the end of while. while(condition); If there is a single statement, brackets are not required. Brackets are always required. Variable in condition is initialized before the execution of loop. variable may be initialized before or within the loop. while loop is entry controlled loop. do-while loop is exit controlled loop. while(condition) { statement(s); } do { statement(s); } while(condition);