SlideShare a Scribd company logo
1 of 15
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);

More Related Content

What's hot (20)

Loops c++
Loops c++Loops c++
Loops c++
 
Control statements
Control statementsControl statements
Control statements
 
Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)
 
Loops in java script
Loops in java scriptLoops in java script
Loops in java script
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Loops in JavaScript
Loops in JavaScriptLoops in JavaScript
Loops in JavaScript
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
 
C++loop statements
C++loop statementsC++loop statements
C++loop statements
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Java Programmin: Selections
Java Programmin: SelectionsJava Programmin: Selections
Java Programmin: Selections
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
 
Loop
LoopLoop
Loop
 
Iteration Statement in C++
Iteration Statement in C++Iteration Statement in C++
Iteration Statement in C++
 
JavaScript Loop: Optimization of Weak Typing
JavaScript Loop: Optimization of Weak TypingJavaScript Loop: Optimization of Weak Typing
JavaScript Loop: Optimization of Weak Typing
 
Conditional Loops Python
Conditional Loops PythonConditional Loops Python
Conditional Loops Python
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executions
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
Chap 6(decision making-looping)
Chap 6(decision making-looping)Chap 6(decision making-looping)
Chap 6(decision making-looping)
 

Similar to Iteration Logic Explained

loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docxJavvajiVenkat
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while LoopJayBhavsar68
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c languagesneha2494
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languagetanmaymodi4
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languageTanmay Modi
 
What is loops? What is For loop?
What is loops? What is For loop?What is loops? What is For loop?
What is loops? What is For loop?AnuragSrivastava272
 
Chapter 3 - Flow of Control Part II.pdf
Chapter 3  - Flow of Control Part II.pdfChapter 3  - Flow of Control Part II.pdf
Chapter 3 - Flow of Control Part II.pdfKirubelWondwoson1
 
Control Statement IN C.pptx
Control Statement IN C.pptxControl Statement IN C.pptx
Control Statement IN C.pptxsujatha629799
 
dizital pods session 5-loops.pptx
dizital pods session 5-loops.pptxdizital pods session 5-loops.pptx
dizital pods session 5-loops.pptxVijayKumarLokanadam
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in CSowmya Jyothi
 
etlplooping-170320213203.pptx
etlplooping-170320213203.pptxetlplooping-170320213203.pptx
etlplooping-170320213203.pptxffyuyufyfufufufu
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming LanguageMahantesh Devoor
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, LoopingMURALIDHAR R
 

Similar to Iteration Logic Explained (20)

loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
Loops In C++
Loops In C++Loops In C++
Loops In C++
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
Loops in c++
Loops in c++Loops in c++
Loops in c++
 
Loops in c
Loops in cLoops in c
Loops in c
 
Loops in C.pptx
Loops in C.pptxLoops in C.pptx
Loops in C.pptx
 
Decision Making and Looping
Decision Making and LoopingDecision Making and Looping
Decision Making and Looping
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
What is loops? What is For loop?
What is loops? What is For loop?What is loops? What is For loop?
What is loops? What is For loop?
 
Chapter 3 - Flow of Control Part II.pdf
Chapter 3  - Flow of Control Part II.pdfChapter 3  - Flow of Control Part II.pdf
Chapter 3 - Flow of Control Part II.pdf
 
Control Statement IN C.pptx
Control Statement IN C.pptxControl Statement IN C.pptx
Control Statement IN C.pptx
 
dizital pods session 5-loops.pptx
dizital pods session 5-loops.pptxdizital pods session 5-loops.pptx
dizital pods session 5-loops.pptx
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
 
etlplooping-170320213203.pptx
etlplooping-170320213203.pptxetlplooping-170320213203.pptx
etlplooping-170320213203.pptx
 
Loops and iteration.docx
Loops and iteration.docxLoops and iteration.docx
Loops and iteration.docx
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
1660213363910.pdf
1660213363910.pdf1660213363910.pdf
1660213363910.pdf
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 

Recently uploaded

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 

Recently uploaded (20)

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 

Iteration Logic Explained

  • 1. 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:
  • 2.  [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
  • 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 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
  • 6. 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.
  • 7. 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++;
  • 8. 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()
  • 9. { // 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.
  • 10. 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-
  • 11. 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
  • 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 { // 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);
  • 14. 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); }
  • 15. 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);