SlideShare a Scribd company logo
1 of 14
Control Structure continued….
• Repetition structures
• C has three types:
• While
• do/while
• for
while
The while Repetition Structure
while
while (condition) action;
• Example in C:
int product=2;
while (product<=1000)
product=2*product;
product<=1000 product=2*product
Y
N
while
Counter-Controlled Repetition
#include <stdio.h>
void main() {
int counter, grade, total, average;
total=0;
counter=0;
while (counter < 10) {
printf("Enter grade:");
scanf("%d", &grade);
total+=grade;
counter++;
}
average=total/10;
printf("Class average is %dn", average);
}
(initialization)
(execute loop)
(termination)
Counter-Controlled Repetition
• Result
Enter grade:60
Enter grade:70
Enter grade:65
Enter grade:76
Enter grade:73
Enter grade:56
Enter grade:78
Enter grade:64
Enter grade:89
Enter grade:76
Class average is 70
Nested Control Structures
#include <stdio.h>
void main() {
int passed=0, failed=0, counter=0, result;
while (counter<10) {
printf("Enter result (1=pass, 2=fail):");
scanf("%d", &result);
if (result==1)
passed++;
else
failed++;
counter++;
}
Nested Control Structures
printf("Passed %dn", passed);
printf("Failed %dn", failed);
if (passed>8)
printf("Excellent Classn");
}
Enter result (1=pass, 2=fail):1
Enter result (1=pass, 2=fail):1
Enter result (1=pass, 2=fail):1
Enter result (1=pass, 2=fail):2
Enter result (1=pass, 2=fail):1
Enter result (1=pass, 2=fail):1
Enter result (1=pass, 2=fail):1
Enter result (1=pass, 2=fail):1
Enter result (1=pass, 2=fail):1
Enter result (1=pass, 2=fail):1
Passed 9
Failed 1
Excellent Class
do-while The do-while Repetition Structure
do while
do
action;
while (condition);
condition
action
Y
N
do-while The do-while Repetition Structure
• 1+2+…+100
#include <stdio.h>
void main() {
int s, i;
s=0;
i=1;
do {
s = s + i;
i++;
} while (i<=100);
printf("1+2+...+100=%dn", s);
}
The for Repetition Structure
for
for (expression1; expression2; expression3)
action;
expression2 action
Y
N
expression3
expression1
The for Repetition Structure
for
#include <stdio.h>
void main() {
int counter;
for (counter = 1; counter <= 10; counter++)
printf("%d ", counter);
}
1 2 3 4 5 6 7 8 9 10
The break Statement
break
#include <stdio.h>
void main() {
int x;
for (x = 1; x <= 10; x++) {
if (x == 5)
break;
printf("%d ", x);
}
printf("nBroke out of loop at x == %dn", x);
}
1 2 3 4
Broke out of loop at x == 5
break
The break Statement
Example: break in switch and for structure
for (i=1;i<=3;i++)
{ switch(i)
{ case 1: printf(“*n”);
break;
case 2: printf(“**n”);
break;
case 3: printf(“***n”);
break;
}
} *
**
***
for (i=1;i<=3;i++)
{ if (i==1) {printf(“*n”);
break;}
if (i==2) {printf(“**n”);
break;}
if (i==3) {printf(“***n”);
break;}
}
*
The continue Statement
continue
#include <stdio.h>
void main() {
int x;
x = 1;
while (x <= 10) {
if (x == 5)
continue;
printf("%d ", x);
x++;
}
}
1 2 3 4
#include <stdio.h>
void main() {
int x;
for (x = 1; x <= 10; x++) {
if (x == 5)
continue;
printf("%d ", x);
}
}
1 2 3 4 6 7 8 9 10
The goto Statement
goto
#include <stdio.h>
void main() {
int s=0, i=1;
Loop:
s = s + i;
i++;
if (i<=100) goto Loop;
printf("1+2+...+100=%dn", s);
}
1+2+…+100=5050
Label

More Related Content

What's hot

What's hot (17)

C Programming Language Part 4
C Programming Language Part 4C Programming Language Part 4
C Programming Language Part 4
 
week-17x
week-17xweek-17x
week-17x
 
Program to illustrate Switch, Goto and Exit statements.
Program to illustrate Switch, Goto and  Exit statements.Program to illustrate Switch, Goto and  Exit statements.
Program to illustrate Switch, Goto and Exit statements.
 
rtrtrNew text document
rtrtrNew text documentrtrtrNew text document
rtrtrNew text document
 
3 flow
3 flow3 flow
3 flow
 
Implementing string
Implementing stringImplementing string
Implementing string
 
Python Conditionals and Functions
Python Conditionals and FunctionsPython Conditionals and Functions
Python Conditionals and Functions
 
Container adapters
Container adaptersContainer adapters
Container adapters
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Final ds record
Final ds recordFinal ds record
Final ds record
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloading
 
C questions
C questionsC questions
C questions
 
Pegomock, a mocking framework for Go
Pegomock, a mocking framework for GoPegomock, a mocking framework for Go
Pegomock, a mocking framework for Go
 
Blocks+gcd入門
Blocks+gcd入門Blocks+gcd入門
Blocks+gcd入門
 
Oopsprc1e
Oopsprc1eOopsprc1e
Oopsprc1e
 
New text document
New text documentNew text document
New text document
 
C program to implement linked list using array abstract data type
C program to implement linked list using array abstract data typeC program to implement linked list using array abstract data type
C program to implement linked list using array abstract data type
 

Similar to Session05 iteration structure

COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops Hemantha Kulathilake
 
1 introduction to c program
1 introduction to c program1 introduction to c program
1 introduction to c programNishmaNJ
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثةشرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثةجامعة القدس المفتوحة
 
6_A1944859510_21789_2_2018_06. Branching Statements.ppt
6_A1944859510_21789_2_2018_06. Branching Statements.ppt6_A1944859510_21789_2_2018_06. Branching Statements.ppt
6_A1944859510_21789_2_2018_06. Branching Statements.pptRithwikRanjan
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop kapil078
 
C# Control Statements, For loop, Do While.ppt
C# Control Statements, For loop, Do While.pptC# Control Statements, For loop, Do While.ppt
C# Control Statements, For loop, Do While.pptRiannel Tecson
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++msharshitha03s
 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings imtiazalijoono
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements loopingMomenMostafa
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02Wingston
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin cVikash Dhal
 
Some stuff about C++ and development
Some stuff about C++ and developmentSome stuff about C++ and development
Some stuff about C++ and developmentJon Jagger
 

Similar to Session05 iteration structure (20)

COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
 
1 introduction to c program
1 introduction to c program1 introduction to c program
1 introduction to c program
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثةشرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
 
6_A1944859510_21789_2_2018_06. Branching Statements.ppt
6_A1944859510_21789_2_2018_06. Branching Statements.ppt6_A1944859510_21789_2_2018_06. Branching Statements.ppt
6_A1944859510_21789_2_2018_06. Branching Statements.ppt
 
Looping
LoopingLooping
Looping
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
C# Control Statements, For loop, Do While.ppt
C# Control Statements, For loop, Do While.pptC# Control Statements, For loop, Do While.ppt
C# Control Statements, For loop, Do While.ppt
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
UNIT 2 LOOP CONTROL.pptx
UNIT 2 LOOP CONTROL.pptxUNIT 2 LOOP CONTROL.pptx
UNIT 2 LOOP CONTROL.pptx
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Unix Programs
Unix ProgramsUnix Programs
Unix Programs
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin c
 
C language
C languageC language
C language
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab programs
C lab programsC lab programs
C lab programs
 
Some stuff about C++ and development
Some stuff about C++ and developmentSome stuff about C++ and development
Some stuff about C++ and development
 

More from HarithaRanasinghe (20)

Session12 pointers
Session12 pointersSession12 pointers
Session12 pointers
 
Session11 single dimarrays
Session11 single dimarraysSession11 single dimarrays
Session11 single dimarrays
 
Session09 multi dimarrays
Session09 multi dimarraysSession09 multi dimarrays
Session09 multi dimarrays
 
Session07 recursion
Session07 recursionSession07 recursion
Session07 recursion
 
Session06 functions
Session06 functionsSession06 functions
Session06 functions
 
Session04 selection structure_b
Session04 selection structure_bSession04 selection structure_b
Session04 selection structure_b
 
Session04 selection structure_a
Session04 selection structure_aSession04 selection structure_a
Session04 selection structure_a
 
Session03 operators
Session03 operatorsSession03 operators
Session03 operators
 
Session02 c intro
Session02 c introSession02 c intro
Session02 c intro
 
Session01 basics programming
Session01 basics programmingSession01 basics programming
Session01 basics programming
 
Program flow charts
Program flow chartsProgram flow charts
Program flow charts
 
Sad -sample_paper
Sad  -sample_paperSad  -sample_paper
Sad -sample_paper
 
Sad sample paper - mcq answers
Sad   sample paper - mcq answersSad   sample paper - mcq answers
Sad sample paper - mcq answers
 
Paper
PaperPaper
Paper
 
Model questions
Model questionsModel questions
Model questions
 
Model paper algorithms and data structures
Model paper  algorithms and data structuresModel paper  algorithms and data structures
Model paper algorithms and data structures
 
Doc 20180208-wa0001
Doc 20180208-wa0001Doc 20180208-wa0001
Doc 20180208-wa0001
 
Doc 20180130-wa0006
Doc 20180130-wa0006Doc 20180130-wa0006
Doc 20180130-wa0006
 
Doc 20180130-wa0005
Doc 20180130-wa0005Doc 20180130-wa0005
Doc 20180130-wa0005
 
Doc 20180130-wa0004-1
Doc 20180130-wa0004-1Doc 20180130-wa0004-1
Doc 20180130-wa0004-1
 

Recently uploaded

Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 

Recently uploaded (20)

Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 

Session05 iteration structure

  • 1. Control Structure continued…. • Repetition structures • C has three types: • While • do/while • for
  • 2. while The while Repetition Structure while while (condition) action; • Example in C: int product=2; while (product<=1000) product=2*product; product<=1000 product=2*product Y N while
  • 3. Counter-Controlled Repetition #include <stdio.h> void main() { int counter, grade, total, average; total=0; counter=0; while (counter < 10) { printf("Enter grade:"); scanf("%d", &grade); total+=grade; counter++; } average=total/10; printf("Class average is %dn", average); } (initialization) (execute loop) (termination)
  • 4. Counter-Controlled Repetition • Result Enter grade:60 Enter grade:70 Enter grade:65 Enter grade:76 Enter grade:73 Enter grade:56 Enter grade:78 Enter grade:64 Enter grade:89 Enter grade:76 Class average is 70
  • 5. Nested Control Structures #include <stdio.h> void main() { int passed=0, failed=0, counter=0, result; while (counter<10) { printf("Enter result (1=pass, 2=fail):"); scanf("%d", &result); if (result==1) passed++; else failed++; counter++; }
  • 6. Nested Control Structures printf("Passed %dn", passed); printf("Failed %dn", failed); if (passed>8) printf("Excellent Classn"); } Enter result (1=pass, 2=fail):1 Enter result (1=pass, 2=fail):1 Enter result (1=pass, 2=fail):1 Enter result (1=pass, 2=fail):2 Enter result (1=pass, 2=fail):1 Enter result (1=pass, 2=fail):1 Enter result (1=pass, 2=fail):1 Enter result (1=pass, 2=fail):1 Enter result (1=pass, 2=fail):1 Enter result (1=pass, 2=fail):1 Passed 9 Failed 1 Excellent Class
  • 7. do-while The do-while Repetition Structure do while do action; while (condition); condition action Y N
  • 8. do-while The do-while Repetition Structure • 1+2+…+100 #include <stdio.h> void main() { int s, i; s=0; i=1; do { s = s + i; i++; } while (i<=100); printf("1+2+...+100=%dn", s); }
  • 9. The for Repetition Structure for for (expression1; expression2; expression3) action; expression2 action Y N expression3 expression1
  • 10. The for Repetition Structure for #include <stdio.h> void main() { int counter; for (counter = 1; counter <= 10; counter++) printf("%d ", counter); } 1 2 3 4 5 6 7 8 9 10
  • 11. The break Statement break #include <stdio.h> void main() { int x; for (x = 1; x <= 10; x++) { if (x == 5) break; printf("%d ", x); } printf("nBroke out of loop at x == %dn", x); } 1 2 3 4 Broke out of loop at x == 5
  • 12. break The break Statement Example: break in switch and for structure for (i=1;i<=3;i++) { switch(i) { case 1: printf(“*n”); break; case 2: printf(“**n”); break; case 3: printf(“***n”); break; } } * ** *** for (i=1;i<=3;i++) { if (i==1) {printf(“*n”); break;} if (i==2) {printf(“**n”); break;} if (i==3) {printf(“***n”); break;} } *
  • 13. The continue Statement continue #include <stdio.h> void main() { int x; x = 1; while (x <= 10) { if (x == 5) continue; printf("%d ", x); x++; } } 1 2 3 4 #include <stdio.h> void main() { int x; for (x = 1; x <= 10; x++) { if (x == 5) continue; printf("%d ", x); } } 1 2 3 4 6 7 8 9 10
  • 14. The goto Statement goto #include <stdio.h> void main() { int s=0, i=1; Loop: s = s + i; i++; if (i<=100) goto Loop; printf("1+2+...+100=%dn", s); } 1+2+…+100=5050 Label