SlideShare a Scribd company logo
1 of 16
Abu Mohammad Musa
Md. Mahbub Morshed Chowdhury
S.M.Zahidul Islam
while (test expression)
{
statement to be executed.
}
The while loop checks whether the test expression is true or not. If it is true, code/s
inside the body of while loop is executed, that is, code/s inside the braces { } are
executed. Then again the test expression is checked whether test expression is true
or not. This process continues until the test expression becomes false
Example
#include<stdio.h>
int main ()
{
char ch;
printf("Enter any character: n");
while (ch!='q' && ch!='Q')
{
scanf("%c",&ch);
}
printf("nFound the Q");
return 0;
}
Write a C program to find the factorial of a number, where the number is entered by user. (Hints: factorial of n = 1*2*3*...*n
#include <stdio.h>
int main()
{
int number,factorial;
printf("Enter a number.n");
scanf("%d",&number);
factorial=1;
while (number>0) /* while loop continues util test
condition number>0 is true */
{
factorial=factorial*number;
--number;
}
printf("Factorial=%d",factorial);
return 0;
}
Output
Enter a number.
5
Factorial=120
Example
#include<stdio.h>
int main ()
{
int x,a,b;
x=10;
a=5;
b=15;
printf("--xt--at--bn");
printf("-------------------n");
while(--a,--b,--x)
{
printf("%dt%dt%dn",x,a,b);
}
return 0;
}
Output
do...while loop
In C, do...while loop is very similar to while loop. Only difference between these two loops is that, in while
loops, test expression is checked at first but, in do...while loop code is executed at first then the condition is
checked. So, the code are executed at least once in do...while loops.
Do
{
some code/s;
}
while (test expression);
Example
#include<stdio.h>
int main ()
{
int a,b;
char ch;
printf("Do you want to:n");
printf("Add, Subtraction, Multiply or
Divide?n");
do
{
printf("nEnter first letter: n");
scanf("%c",&ch);
}
while(ch!='A' && ch!='S' && ch!='M'
&& ch!='D');
printf("n");
printf("Enter the first number: n");
scanf("%d",&a);
printf("Enter second number: n");
scanf("%d",&b);
if(ch=='A')
printf("n%d",a+b);
else if(ch=='B')
printf("n%d",a-b);
else if(ch=='M')
printf("n%d",a*b);
else if(ch=='D')
printf("n%d",a/b);
return 0;
}
Output
Example 2
#include<stdio.h>
int main()
{
int i=10;
do
{
printf("%dn",i);
i=i-1;
if(i==6)
{
break;
}
}
while(i>0);
return 0;
}
Output
At first codes inside body of do is executed. Then, the test expression is checked. If it is true, code/s inside body of do
are executed again and the process continues until test expression becomes false(zero).
Notice, there is semicolon in the end of while (); in do...while loop.
Write a C program to add all the numbers entered by a user until user enters 0.
#include <stdio.h>
int main(){
int sum=0,num;
do
{
printf("Enter a numbern");
scanf("%d",&num);
sum+=num;
}
while(num!=0);
printf("sum=%d",sum);
return 0;
}
Output
Enter a number
3
Enter a number
-2
Enter a number
0
sum=1
In this C program, user is asked a number and it is added with sum. Then, only the test condition in the do...while loop is
checked. If the test condition is true,i.e, num is not equal to 0, the body of do...while loop is again executed until num
equals to zero.
Similarity between for and do while loop
Syntax of for loop
do .. While loop does the same thing as for loop does
But the for loop is compact and easy to use.
#include<stdio.h>
int main()
{
int i;
for(i=1;i<=5;i++)
{
printf("%d",i);
}
return 0;
}
This program shows the same output but they are
written in for loop and do while loop
Output
12345
Output
12345
#include<stdio.h>
int main()
{
int i=1;
do
{
printf("%d",i);
i++;
}
while (i<=5);
return 0;
}
C loops explained with examples

More Related Content

What's hot

Loop control structure
Loop control structureLoop control structure
Loop control structureKaran Pandey
 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
 
C programs
C programsC programs
C programsMinu S
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robinAbdullah Al Naser
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in cSaranya saran
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branchingSaranya saran
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For LoopSukrit Gupta
 
Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1Dr. Loganathan R
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...DR B.Surendiran .
 
Bcsl 033 data and file structures lab s1-3
Bcsl 033 data and file structures lab s1-3Bcsl 033 data and file structures lab s1-3
Bcsl 033 data and file structures lab s1-3Dr. Loganathan R
 
Bcsl 033 data and file structures lab s2-3
Bcsl 033 data and file structures lab s2-3Bcsl 033 data and file structures lab s2-3
Bcsl 033 data and file structures lab s2-3Dr. Loganathan R
 
C Programming Language Part 4
C Programming Language Part 4C Programming Language Part 4
C Programming Language Part 4Rumman Ansari
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04hassaanciit
 

What's hot (20)

C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
Progr3
Progr3Progr3
Progr3
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
Loop control structure
Loop control structureLoop control structure
Loop control structure
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
C programs
C programsC programs
C programs
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
 
Prim
PrimPrim
Prim
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Najmul
Najmul  Najmul
Najmul
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For Loop
 
Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 
Bcsl 033 data and file structures lab s1-3
Bcsl 033 data and file structures lab s1-3Bcsl 033 data and file structures lab s1-3
Bcsl 033 data and file structures lab s1-3
 
C Programming
C ProgrammingC Programming
C Programming
 
Bcsl 033 data and file structures lab s2-3
Bcsl 033 data and file structures lab s2-3Bcsl 033 data and file structures lab s2-3
Bcsl 033 data and file structures lab s2-3
 
C Programming Language Part 4
C Programming Language Part 4C Programming Language Part 4
C Programming Language Part 4
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04
 
week-6x
week-6xweek-6x
week-6x
 

Similar to C loops explained with examples

Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02Wingston
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02Suhail Akraam
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfAshutoshprasad27
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxAshutoshprasad27
 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements loopingMomenMostafa
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkarsandeep kumbhkar
 
C basics
C basicsC basics
C basicsMSc CST
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C LanguageRAJWANT KAUR
 
Loop's definition and practical code in C programming
Loop's definition and  practical code in C programming Loop's definition and  practical code in C programming
Loop's definition and practical code in C programming DharmaKumariBhandari
 
Best C Programming Solution
Best C Programming SolutionBest C Programming Solution
Best C Programming Solutionyogini sharma
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programsPrasadu Peddi
 

Similar to C loops explained with examples (20)

Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
C faq pdf
C faq pdfC faq pdf
C faq pdf
 
C programs
C programsC programs
C programs
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
Programming egs
Programming egs Programming egs
Programming egs
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
 
C programms
C programmsC programms
C programms
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
C lab
C labC lab
C lab
 
C file
C fileC file
C file
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
C basics
C basicsC basics
C basics
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
Loop's definition and practical code in C programming
Loop's definition and  practical code in C programming Loop's definition and  practical code in C programming
Loop's definition and practical code in C programming
 
C Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossainC Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossain
 
Best C Programming Solution
Best C Programming SolutionBest C Programming Solution
Best C Programming Solution
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programs
 

More from S.M.Zahidul Islam sumon (14)

Bioinfo
Bioinfo Bioinfo
Bioinfo
 
Infinity
InfinityInfinity
Infinity
 
Discrete math-presentation
Discrete math-presentationDiscrete math-presentation
Discrete math-presentation
 
Drone
DroneDrone
Drone
 
Os
OsOs
Os
 
Physics ii
Physics iiPhysics ii
Physics ii
 
Block chain
Block chainBlock chain
Block chain
 
Image processing
Image processingImage processing
Image processing
 
Data mining(1)
Data mining(1)Data mining(1)
Data mining(1)
 
Lcd bresenham
Lcd bresenhamLcd bresenham
Lcd bresenham
 
Simulation pst
Simulation pstSimulation pst
Simulation pst
 
Software engineering
Software engineeringSoftware engineering
Software engineering
 
Technology in education
Technology in educationTechnology in education
Technology in education
 
Zah architecture 123
Zah architecture 123Zah architecture 123
Zah architecture 123
 

Recently uploaded

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 

Recently uploaded (20)

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 

C loops explained with examples

  • 1.
  • 2.
  • 3. Abu Mohammad Musa Md. Mahbub Morshed Chowdhury S.M.Zahidul Islam
  • 4. while (test expression) { statement to be executed. } The while loop checks whether the test expression is true or not. If it is true, code/s inside the body of while loop is executed, that is, code/s inside the braces { } are executed. Then again the test expression is checked whether test expression is true or not. This process continues until the test expression becomes false
  • 5. Example #include<stdio.h> int main () { char ch; printf("Enter any character: n"); while (ch!='q' && ch!='Q') { scanf("%c",&ch); } printf("nFound the Q"); return 0; }
  • 6.
  • 7. Write a C program to find the factorial of a number, where the number is entered by user. (Hints: factorial of n = 1*2*3*...*n #include <stdio.h> int main() { int number,factorial; printf("Enter a number.n"); scanf("%d",&number); factorial=1; while (number>0) /* while loop continues util test condition number>0 is true */ { factorial=factorial*number; --number; } printf("Factorial=%d",factorial); return 0; } Output Enter a number. 5 Factorial=120
  • 8. Example #include<stdio.h> int main () { int x,a,b; x=10; a=5; b=15; printf("--xt--at--bn"); printf("-------------------n"); while(--a,--b,--x) { printf("%dt%dt%dn",x,a,b); } return 0; } Output
  • 9. do...while loop In C, do...while loop is very similar to while loop. Only difference between these two loops is that, in while loops, test expression is checked at first but, in do...while loop code is executed at first then the condition is checked. So, the code are executed at least once in do...while loops. Do { some code/s; } while (test expression);
  • 10. Example #include<stdio.h> int main () { int a,b; char ch; printf("Do you want to:n"); printf("Add, Subtraction, Multiply or Divide?n"); do { printf("nEnter first letter: n"); scanf("%c",&ch); } while(ch!='A' && ch!='S' && ch!='M' && ch!='D'); printf("n"); printf("Enter the first number: n"); scanf("%d",&a); printf("Enter second number: n"); scanf("%d",&b); if(ch=='A') printf("n%d",a+b); else if(ch=='B') printf("n%d",a-b); else if(ch=='M') printf("n%d",a*b); else if(ch=='D') printf("n%d",a/b); return 0; } Output
  • 11. Example 2 #include<stdio.h> int main() { int i=10; do { printf("%dn",i); i=i-1; if(i==6) { break; } } while(i>0); return 0; } Output
  • 12. At first codes inside body of do is executed. Then, the test expression is checked. If it is true, code/s inside body of do are executed again and the process continues until test expression becomes false(zero). Notice, there is semicolon in the end of while (); in do...while loop.
  • 13. Write a C program to add all the numbers entered by a user until user enters 0. #include <stdio.h> int main(){ int sum=0,num; do { printf("Enter a numbern"); scanf("%d",&num); sum+=num; } while(num!=0); printf("sum=%d",sum); return 0; } Output Enter a number 3 Enter a number -2 Enter a number 0 sum=1 In this C program, user is asked a number and it is added with sum. Then, only the test condition in the do...while loop is checked. If the test condition is true,i.e, num is not equal to 0, the body of do...while loop is again executed until num equals to zero.
  • 14. Similarity between for and do while loop Syntax of for loop do .. While loop does the same thing as for loop does But the for loop is compact and easy to use.
  • 15. #include<stdio.h> int main() { int i; for(i=1;i<=5;i++) { printf("%d",i); } return 0; } This program shows the same output but they are written in for loop and do while loop Output 12345 Output 12345 #include<stdio.h> int main() { int i=1; do { printf("%d",i); i++; } while (i<=5); return 0; }