SlideShare a Scribd company logo
C PROGRAMMING
Flow Controls
By,
Mr. Shekharkumar
Loop statements
What is loop?
Loop is used to execute the block of code several times according to the
condition given in the loop. It means it executes the same code multiple
times so it saves code
Types of loops
for loop
while loop
do...while loop
Syntax of for loop
for (initialization Statement; test Expression; update Statement;
{
Code
}
How for loop works?
• The initialization statement is executed only once.
• Then, the test expression is evaluated. If the test expression is evaluated to false,
the for loop is terminated.
• However, if the test expression is evaluated to true, statements inside the body of
the for loop are executed, and the update expression is updated.
• Again the test expression is evaluated.
• This process goes on until the test expression is false. When the test expression is
false, the loop terminates.
Flowchart of for loop
Example of for loop
Print numbers from 1 to 10
#include <stdio.h>
void main()
{
int i;
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
}
Syntax of while loop
while (testexpression)
{
Code
}
How while loop works?
• The while loop evaluates the test expression inside the parentheses ().
• If test expression is true, statement inside the body of while loop are
executed .Then test expression is evaluated again.
• The process goes until test expression is evaluated false.
• If test expression is false, the loop terminates.
Flowchart of while loop
Example of while loop
Print numbers from 1 to 5
#include <stdio.h>
void main()
{
int i = 1;
while (i <= 5)
{
printf("%dn", i);
++i;
}
}
The syntax of do...while loop
do
{
Code
}
while (test expression)
How do...while loop works?
• The body of do...while loop is executed once. Only then, the test expression
is evaluated.
• If test expression is true, the body of the loop is executed again and test
expression is evaluated once more.
• This process goes on until test expression becomes false.
• If test expression is false, the loop ends.
Flowchart of do...while Loop
Example of do…while loop
Program to accept numbers until the user enters zero
#include <stdio.h>
void main() {
double number, sum = 0;
// the body of the loop is executed at least once
do {
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);
printf("Sum = %.2lf",sum);
}
Ad

More Related Content

Similar to 3. Flow Controls in C (Part II).pdf (20)

Introduction to Java Programming - Lecture 11.pptx
Introduction to Java Programming - Lecture 11.pptxIntroduction to Java Programming - Lecture 11.pptx
Introduction to Java Programming - Lecture 11.pptx
AbdulKhaleqHerawi1
 
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
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Priyom Majumder
 
Workbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdfWorkbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdf
DrDineshenScientist
 
Looping statements
Looping statementsLooping statements
Looping statements
AbhishekMondal42
 
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
 
cpu.pdf
cpu.pdfcpu.pdf
cpu.pdf
RAJCHATTERJEE24
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
Abhishek Choksi
 
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdfPROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
JavvajiVenkat
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loops
Manzoor ALam
 
C Programming: Looping Statements in C Pgm
C Programming: Looping Statements in C PgmC Programming: Looping Statements in C Pgm
C Programming: Looping Statements in C Pgm
Navya Francis
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
marvellous2
 
Python if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_StatementPython if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_Statement
AbhishekGupta692777
 
PYTHON.pptx
PYTHON.pptxPYTHON.pptx
PYTHON.pptx
studentsymposium
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
Likhil181
 
Going loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptxGoing loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptx
Amy Nightingale
 
Ch6 Loops
Ch6 LoopsCh6 Loops
Ch6 Loops
SzeChingChen
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
Suhail Akraam
 
M C6java6
M C6java6M C6java6
M C6java6
mbruggen
 
Introduction to Java Programming - Lecture 11.pptx
Introduction to Java Programming - Lecture 11.pptxIntroduction to Java Programming - Lecture 11.pptx
Introduction to Java Programming - Lecture 11.pptx
AbdulKhaleqHerawi1
 
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
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Priyom Majumder
 
Workbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdfWorkbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdf
DrDineshenScientist
 
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
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
Abhishek Choksi
 
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdfPROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
JavvajiVenkat
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loops
Manzoor ALam
 
C Programming: Looping Statements in C Pgm
C Programming: Looping Statements in C PgmC Programming: Looping Statements in C Pgm
C Programming: Looping Statements in C Pgm
Navya Francis
 
Python if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_StatementPython if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_Statement
AbhishekGupta692777
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
Likhil181
 
Going loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptxGoing loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptx
Amy Nightingale
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
Suhail Akraam
 

More from santosh147365 (8)

lightGen_presentatio you can see the new one in the given series of numbers n...
lightGen_presentatio you can see the new one in the given series of numbers n...lightGen_presentatio you can see the new one in the given series of numbers n...
lightGen_presentatio you can see the new one in the given series of numbers n...
santosh147365
 
VTUProj I think I will be going to temple ect-1.pptx
VTUProj I think I will be going to temple ect-1.pptxVTUProj I think I will be going to temple ect-1.pptx
VTUProj I think I will be going to temple ect-1.pptx
santosh147365
 
Chapter_12_Regisfnfnjfjfjfjfjjtration.pptx
Chapter_12_Regisfnfnjfjfjfjfjjtration.pptxChapter_12_Regisfnfnjfjfjfjfjjtration.pptx
Chapter_12_Regisfnfnjfjfjfjfjjtration.pptx
santosh147365
 
Data_Security_inrefef_CloudComputing.pptx
Data_Security_inrefef_CloudComputing.pptxData_Security_inrefef_CloudComputing.pptx
Data_Security_inrefef_CloudComputing.pptx
santosh147365
 
The-Power-of-Respect-in-Human-to-Human-Relationships.pptx
The-Power-of-Respect-in-Human-to-Human-Relationships.pptxThe-Power-of-Respect-in-Human-to-Human-Relationships.pptx
The-Power-of-Respect-in-Human-to-Human-Relationships.pptx
santosh147365
 
hanuppt.pptx
hanuppt.pptxhanuppt.pptx
hanuppt.pptx
santosh147365
 
5. Functions in C.pdf
5. Functions in C.pdf5. Functions in C.pdf
5. Functions in C.pdf
santosh147365
 
15EC81 - Robin Singla.pdf
15EC81 - Robin Singla.pdf15EC81 - Robin Singla.pdf
15EC81 - Robin Singla.pdf
santosh147365
 
lightGen_presentatio you can see the new one in the given series of numbers n...
lightGen_presentatio you can see the new one in the given series of numbers n...lightGen_presentatio you can see the new one in the given series of numbers n...
lightGen_presentatio you can see the new one in the given series of numbers n...
santosh147365
 
VTUProj I think I will be going to temple ect-1.pptx
VTUProj I think I will be going to temple ect-1.pptxVTUProj I think I will be going to temple ect-1.pptx
VTUProj I think I will be going to temple ect-1.pptx
santosh147365
 
Chapter_12_Regisfnfnjfjfjfjfjjtration.pptx
Chapter_12_Regisfnfnjfjfjfjfjjtration.pptxChapter_12_Regisfnfnjfjfjfjfjjtration.pptx
Chapter_12_Regisfnfnjfjfjfjfjjtration.pptx
santosh147365
 
Data_Security_inrefef_CloudComputing.pptx
Data_Security_inrefef_CloudComputing.pptxData_Security_inrefef_CloudComputing.pptx
Data_Security_inrefef_CloudComputing.pptx
santosh147365
 
The-Power-of-Respect-in-Human-to-Human-Relationships.pptx
The-Power-of-Respect-in-Human-to-Human-Relationships.pptxThe-Power-of-Respect-in-Human-to-Human-Relationships.pptx
The-Power-of-Respect-in-Human-to-Human-Relationships.pptx
santosh147365
 
5. Functions in C.pdf
5. Functions in C.pdf5. Functions in C.pdf
5. Functions in C.pdf
santosh147365
 
15EC81 - Robin Singla.pdf
15EC81 - Robin Singla.pdf15EC81 - Robin Singla.pdf
15EC81 - Robin Singla.pdf
santosh147365
 
Ad

Recently uploaded (20)

Digital Marketing Mock Project - Client Testimonial
Digital Marketing Mock Project - Client TestimonialDigital Marketing Mock Project - Client Testimonial
Digital Marketing Mock Project - Client Testimonial
Adeline Yeo
 
BCG’s Evolution of Travel: Rethinking Business Travel in a Post-Pandemic World
BCG’s Evolution of Travel: Rethinking Business Travel in a Post-Pandemic WorldBCG’s Evolution of Travel: Rethinking Business Travel in a Post-Pandemic World
BCG’s Evolution of Travel: Rethinking Business Travel in a Post-Pandemic World
INKPPT
 
McKinsey – Mobility Consumer Pulse 2024 | Global Trends in EVs, Shared Mobili...
McKinsey – Mobility Consumer Pulse 2024 | Global Trends in EVs, Shared Mobili...McKinsey – Mobility Consumer Pulse 2024 | Global Trends in EVs, Shared Mobili...
McKinsey – Mobility Consumer Pulse 2024 | Global Trends in EVs, Shared Mobili...
INKPPT
 
ppt nm epanet org irrigation system (1).pptx
ppt nm epanet org irrigation system (1).pptxppt nm epanet org irrigation system (1).pptx
ppt nm epanet org irrigation system (1).pptx
dondeepakff33
 
uTorrent Pro Crack Download for PC [Latest] 2025 Version
uTorrent Pro Crack Download for PC [Latest] 2025 VersionuTorrent Pro Crack Download for PC [Latest] 2025 Version
uTorrent Pro Crack Download for PC [Latest] 2025 Version
Web Designer
 
The Butterfly Effect in Design Entrepreneurship.pptx
The Butterfly Effect in Design Entrepreneurship.pptxThe Butterfly Effect in Design Entrepreneurship.pptx
The Butterfly Effect in Design Entrepreneurship.pptx
Prof. Hany El-Said
 
Learn the ABC with Bauhaus by Klara Francisco.pdf
Learn the ABC with Bauhaus by Klara Francisco.pdfLearn the ABC with Bauhaus by Klara Francisco.pdf
Learn the ABC with Bauhaus by Klara Francisco.pdf
KlaraJericaFrancisco
 
We Trust AI... Until We Don’t_ The UX of Comfort Zones by Dan Maccarone and P...
We Trust AI... Until We Don’t_ The UX of Comfort Zones by Dan Maccarone and P...We Trust AI... Until We Don’t_ The UX of Comfort Zones by Dan Maccarone and P...
We Trust AI... Until We Don’t_ The UX of Comfort Zones by Dan Maccarone and P...
UXPA Boston
 
Using AI to Streamline Personas and Journey Map Creation
Using AI to Streamline Personas and Journey Map CreationUsing AI to Streamline Personas and Journey Map Creation
Using AI to Streamline Personas and Journey Map Creation
Kyle Soucy
 
Elevating Urban Skylines: The Power of High-Rise Exterior Renderings by Yantr...
Elevating Urban Skylines: The Power of High-Rise Exterior Renderings by Yantr...Elevating Urban Skylines: The Power of High-Rise Exterior Renderings by Yantr...
Elevating Urban Skylines: The Power of High-Rise Exterior Renderings by Yantr...
Yantram Animation Studio Corporation
 
101 Marketing for Design Entrepreneurs.pptx
101 Marketing for Design Entrepreneurs.pptx101 Marketing for Design Entrepreneurs.pptx
101 Marketing for Design Entrepreneurs.pptx
Prof. Hany El-Said
 
lecture01_introImageprocessing andcv.ppt
lecture01_introImageprocessing andcv.pptlecture01_introImageprocessing andcv.ppt
lecture01_introImageprocessing andcv.ppt
shilpapatil4216
 
最新版加拿大莱斯桥学院毕业证(Lethbridge毕业证书)原版定制
最新版加拿大莱斯桥学院毕业证(Lethbridge毕业证书)原版定制最新版加拿大莱斯桥学院毕业证(Lethbridge毕业证书)原版定制
最新版加拿大莱斯桥学院毕业证(Lethbridge毕业证书)原版定制
Taqyea
 
Deloitte – State of AI in the Enterprise | Actionable AI Strategies & Insights
Deloitte – State of AI in the Enterprise | Actionable AI Strategies & InsightsDeloitte – State of AI in the Enterprise | Actionable AI Strategies & Insights
Deloitte – State of AI in the Enterprise | Actionable AI Strategies & Insights
INKPPT
 
"Dino World: The Ultimate Dinosaur Coloring Book for Kids"
"Dino World: The Ultimate Dinosaur Coloring Book for Kids""Dino World: The Ultimate Dinosaur Coloring Book for Kids"
"Dino World: The Ultimate Dinosaur Coloring Book for Kids"
Ijaz Ahmad
 
KPMG – Global Tech Report 2022 | Web3, Metaverse & Digital Transformation Trends
KPMG – Global Tech Report 2022 | Web3, Metaverse & Digital Transformation TrendsKPMG – Global Tech Report 2022 | Web3, Metaverse & Digital Transformation Trends
KPMG – Global Tech Report 2022 | Web3, Metaverse & Digital Transformation Trends
INKPPT
 
KPMG – Future of Supply Chain | ESG, Technology & Risk Strategies for 2030
KPMG – Future of Supply Chain | ESG, Technology & Risk Strategies for 2030KPMG – Future of Supply Chain | ESG, Technology & Risk Strategies for 2030
KPMG – Future of Supply Chain | ESG, Technology & Risk Strategies for 2030
INKPPT
 
CONTENT MARKETING.pdf vfhfhfbdvdfvdfregf
CONTENT MARKETING.pdf vfhfhfbdvdfvdfregfCONTENT MARKETING.pdf vfhfhfbdvdfvdfregf
CONTENT MARKETING.pdf vfhfhfbdvdfvdfregf
bjtjhj
 
SEERAT PPT[1][1].pptx project in sant ba
SEERAT PPT[1][1].pptx project in sant baSEERAT PPT[1][1].pptx project in sant ba
SEERAT PPT[1][1].pptx project in sant ba
RanvirSingh151
 
Furniture design for projects-vol-3-brochure.pdf
Furniture design for projects-vol-3-brochure.pdfFurniture design for projects-vol-3-brochure.pdf
Furniture design for projects-vol-3-brochure.pdf
AjayBhonge1
 
Digital Marketing Mock Project - Client Testimonial
Digital Marketing Mock Project - Client TestimonialDigital Marketing Mock Project - Client Testimonial
Digital Marketing Mock Project - Client Testimonial
Adeline Yeo
 
BCG’s Evolution of Travel: Rethinking Business Travel in a Post-Pandemic World
BCG’s Evolution of Travel: Rethinking Business Travel in a Post-Pandemic WorldBCG’s Evolution of Travel: Rethinking Business Travel in a Post-Pandemic World
BCG’s Evolution of Travel: Rethinking Business Travel in a Post-Pandemic World
INKPPT
 
McKinsey – Mobility Consumer Pulse 2024 | Global Trends in EVs, Shared Mobili...
McKinsey – Mobility Consumer Pulse 2024 | Global Trends in EVs, Shared Mobili...McKinsey – Mobility Consumer Pulse 2024 | Global Trends in EVs, Shared Mobili...
McKinsey – Mobility Consumer Pulse 2024 | Global Trends in EVs, Shared Mobili...
INKPPT
 
ppt nm epanet org irrigation system (1).pptx
ppt nm epanet org irrigation system (1).pptxppt nm epanet org irrigation system (1).pptx
ppt nm epanet org irrigation system (1).pptx
dondeepakff33
 
uTorrent Pro Crack Download for PC [Latest] 2025 Version
uTorrent Pro Crack Download for PC [Latest] 2025 VersionuTorrent Pro Crack Download for PC [Latest] 2025 Version
uTorrent Pro Crack Download for PC [Latest] 2025 Version
Web Designer
 
The Butterfly Effect in Design Entrepreneurship.pptx
The Butterfly Effect in Design Entrepreneurship.pptxThe Butterfly Effect in Design Entrepreneurship.pptx
The Butterfly Effect in Design Entrepreneurship.pptx
Prof. Hany El-Said
 
Learn the ABC with Bauhaus by Klara Francisco.pdf
Learn the ABC with Bauhaus by Klara Francisco.pdfLearn the ABC with Bauhaus by Klara Francisco.pdf
Learn the ABC with Bauhaus by Klara Francisco.pdf
KlaraJericaFrancisco
 
We Trust AI... Until We Don’t_ The UX of Comfort Zones by Dan Maccarone and P...
We Trust AI... Until We Don’t_ The UX of Comfort Zones by Dan Maccarone and P...We Trust AI... Until We Don’t_ The UX of Comfort Zones by Dan Maccarone and P...
We Trust AI... Until We Don’t_ The UX of Comfort Zones by Dan Maccarone and P...
UXPA Boston
 
Using AI to Streamline Personas and Journey Map Creation
Using AI to Streamline Personas and Journey Map CreationUsing AI to Streamline Personas and Journey Map Creation
Using AI to Streamline Personas and Journey Map Creation
Kyle Soucy
 
Elevating Urban Skylines: The Power of High-Rise Exterior Renderings by Yantr...
Elevating Urban Skylines: The Power of High-Rise Exterior Renderings by Yantr...Elevating Urban Skylines: The Power of High-Rise Exterior Renderings by Yantr...
Elevating Urban Skylines: The Power of High-Rise Exterior Renderings by Yantr...
Yantram Animation Studio Corporation
 
101 Marketing for Design Entrepreneurs.pptx
101 Marketing for Design Entrepreneurs.pptx101 Marketing for Design Entrepreneurs.pptx
101 Marketing for Design Entrepreneurs.pptx
Prof. Hany El-Said
 
lecture01_introImageprocessing andcv.ppt
lecture01_introImageprocessing andcv.pptlecture01_introImageprocessing andcv.ppt
lecture01_introImageprocessing andcv.ppt
shilpapatil4216
 
最新版加拿大莱斯桥学院毕业证(Lethbridge毕业证书)原版定制
最新版加拿大莱斯桥学院毕业证(Lethbridge毕业证书)原版定制最新版加拿大莱斯桥学院毕业证(Lethbridge毕业证书)原版定制
最新版加拿大莱斯桥学院毕业证(Lethbridge毕业证书)原版定制
Taqyea
 
Deloitte – State of AI in the Enterprise | Actionable AI Strategies & Insights
Deloitte – State of AI in the Enterprise | Actionable AI Strategies & InsightsDeloitte – State of AI in the Enterprise | Actionable AI Strategies & Insights
Deloitte – State of AI in the Enterprise | Actionable AI Strategies & Insights
INKPPT
 
"Dino World: The Ultimate Dinosaur Coloring Book for Kids"
"Dino World: The Ultimate Dinosaur Coloring Book for Kids""Dino World: The Ultimate Dinosaur Coloring Book for Kids"
"Dino World: The Ultimate Dinosaur Coloring Book for Kids"
Ijaz Ahmad
 
KPMG – Global Tech Report 2022 | Web3, Metaverse & Digital Transformation Trends
KPMG – Global Tech Report 2022 | Web3, Metaverse & Digital Transformation TrendsKPMG – Global Tech Report 2022 | Web3, Metaverse & Digital Transformation Trends
KPMG – Global Tech Report 2022 | Web3, Metaverse & Digital Transformation Trends
INKPPT
 
KPMG – Future of Supply Chain | ESG, Technology & Risk Strategies for 2030
KPMG – Future of Supply Chain | ESG, Technology & Risk Strategies for 2030KPMG – Future of Supply Chain | ESG, Technology & Risk Strategies for 2030
KPMG – Future of Supply Chain | ESG, Technology & Risk Strategies for 2030
INKPPT
 
CONTENT MARKETING.pdf vfhfhfbdvdfvdfregf
CONTENT MARKETING.pdf vfhfhfbdvdfvdfregfCONTENT MARKETING.pdf vfhfhfbdvdfvdfregf
CONTENT MARKETING.pdf vfhfhfbdvdfvdfregf
bjtjhj
 
SEERAT PPT[1][1].pptx project in sant ba
SEERAT PPT[1][1].pptx project in sant baSEERAT PPT[1][1].pptx project in sant ba
SEERAT PPT[1][1].pptx project in sant ba
RanvirSingh151
 
Furniture design for projects-vol-3-brochure.pdf
Furniture design for projects-vol-3-brochure.pdfFurniture design for projects-vol-3-brochure.pdf
Furniture design for projects-vol-3-brochure.pdf
AjayBhonge1
 
Ad

3. Flow Controls in C (Part II).pdf

  • 2. Loop statements What is loop? Loop is used to execute the block of code several times according to the condition given in the loop. It means it executes the same code multiple times so it saves code
  • 3. Types of loops for loop while loop do...while loop
  • 4. Syntax of for loop for (initialization Statement; test Expression; update Statement; { Code }
  • 5. How for loop works? • The initialization statement is executed only once. • Then, the test expression is evaluated. If the test expression is evaluated to false, the for loop is terminated. • However, if the test expression is evaluated to true, statements inside the body of the for loop are executed, and the update expression is updated. • Again the test expression is evaluated. • This process goes on until the test expression is false. When the test expression is false, the loop terminates.
  • 7. Example of for loop Print numbers from 1 to 10 #include <stdio.h> void main() { int i; for (i = 1; i < 11; ++i) { printf("%d ", i); } }
  • 8. Syntax of while loop while (testexpression) { Code }
  • 9. How while loop works? • The while loop evaluates the test expression inside the parentheses (). • If test expression is true, statement inside the body of while loop are executed .Then test expression is evaluated again. • The process goes until test expression is evaluated false. • If test expression is false, the loop terminates.
  • 11. Example of while loop Print numbers from 1 to 5 #include <stdio.h> void main() { int i = 1; while (i <= 5) { printf("%dn", i); ++i; } }
  • 12. The syntax of do...while loop do { Code } while (test expression)
  • 13. How do...while loop works? • The body of do...while loop is executed once. Only then, the test expression is evaluated. • If test expression is true, the body of the loop is executed again and test expression is evaluated once more. • This process goes on until test expression becomes false. • If test expression is false, the loop ends.
  • 15. Example of do…while loop Program to accept numbers until the user enters zero #include <stdio.h> void main() { double number, sum = 0; // the body of the loop is executed at least once do { printf("Enter a number: "); scanf("%lf", &number); sum += number; } while(number != 0.0); printf("Sum = %.2lf",sum); }
  翻译: