SlideShare a Scribd company logo
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

Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
Control statements
Control statementsControl statements
Control statements
Kanwalpreet Kaur
 
Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)
Muhammad Tahir Bashir
 
Loops in java script
Loops in java scriptLoops in java script
Loops in java script
Ravi Bhadauria
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
Anil Dutt
 
Loops in JavaScript
Loops in JavaScriptLoops in JavaScript
Loops in JavaScript
Florence Davis
 
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
Krishna Raj
 
C++loop statements
C++loop statementsC++loop statements
C++loop statements
Muhammad Uzair Rasheed
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
SHRIRANG PINJARKAR
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
Amrit Kaur
 
Java Programmin: Selections
Java Programmin: SelectionsJava Programmin: Selections
Java Programmin: Selections
Karwan Mustafa Kareem
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
bluejayjunior
 
Loop
LoopLoop
Iteration Statement in C++
Iteration Statement in C++Iteration Statement in C++
Iteration Statement in C++
Jaypee Institute of Information Technology
 
JavaScript Loop: Optimization of Weak Typing
JavaScript Loop: Optimization of Weak TypingJavaScript Loop: Optimization of Weak Typing
JavaScript Loop: Optimization of Weak Typing
Janlay Wu
 
Conditional Loops Python
Conditional Loops PythonConditional Loops Python
Conditional Loops Python
primeteacher32
 
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
eShikshak
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
ChaAstillas
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
yugandhar vadlamudi
 
Chap 6(decision making-looping)
Chap 6(decision making-looping)Chap 6(decision making-looping)

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 itretion.docx

loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
JavvajiVenkat
 
Loops In C++
Loops In C++Loops In C++
Loops In C++
Banasthali Vidyapith
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
JayBhavsar68
 
Loops in c++
Loops in c++Loops in c++
Loops in c++
Rebin Daho
 
Loops in c
Loops in cLoops in c
Loops in c
shubhampandav3
 
Loops in C.pptx
Loops in C.pptxLoops in C.pptx
Loops in C.pptx
nagalakshmig4
 
Decision Making and Looping
Decision Making and LoopingDecision Making and Looping
Decision Making and Looping
Munazza-Mah-Jabeen
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
sneha2494
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
Tanmay Modi
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
tanmaymodi4
 
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.pdf
KirubelWondwoson1
 
Control Statement IN C.pptx
Control Statement IN C.pptxControl Statement IN C.pptx
Control Statement IN C.pptx
sujatha629799
 
dizital pods session 5-loops.pptx
dizital pods session 5-loops.pptxdizital pods session 5-loops.pptx
dizital pods session 5-loops.pptx
VijayKumarLokanadam
 
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
Sowmya Jyothi
 
etlplooping-170320213203.pptx
etlplooping-170320213203.pptxetlplooping-170320213203.pptx
etlplooping-170320213203.pptx
ffyuyufyfufufufu
 
Loops and iteration.docx
Loops and iteration.docxLoops and iteration.docx
Loops and iteration.docx
NkurikiyimanaGodefre
 
presentation on powerpoint template.pptx
presentation on powerpoint template.pptxpresentation on powerpoint template.pptx
presentation on powerpoint template.pptx
farantouqeer8
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
Mahantesh Devoor
 
1660213363910.pdf
1660213363910.pdf1660213363910.pdf
1660213363910.pdf
CuentaTemporal4
 

Similar to itretion.docx (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
 
presentation on powerpoint template.pptx
presentation on powerpoint template.pptxpresentation on powerpoint template.pptx
presentation on powerpoint template.pptx
 
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
 

Recently uploaded

New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
NazakatAliKhoso2
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
nooriasukmaningtyas
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 

Recently uploaded (20)

New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 

itretion.docx

  • 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);