SlideShare une entreprise Scribd logo
1  sur  10
Télécharger pour lire hors ligne
Do-While
Loop
Said Msihullah Hashimi, M.Sc. NTM
INTRODUCTION
▪ A do-while loop is similar to a while loop, except
the fact that it is guaranteed to execute at least one
time.
▪ Unlike for and while loops, do-while loop in C
programming checks its condition at the bottom
of the loop .
2
Do-While Loop
Syntax
“
4
○ The syntax of a do-while loop in C.
do {
statement(s);
} while ( condition );
Flow
Diagram
5
WORKING CODE
6
• #include <stdio.h>
• int main () {
• /* local variable definition */
• int a = 10;
• /* do loop execution */
• do {
• printf("value of a: %dn", a);
• a = a + 1;
• } while ( a < 20 );
• return 0;
• }
Linear Search
code in C
7
Output
8
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
do {
printf("value of a: %dn", a);
First statement always gets
printed before checking the
condition even if condition is
wrong.
a = a + 1;
The value is incremented by 1
each time.
while ( a < 20 );
return 0;
}
After checking the condition,
other statements gets printed
based on condition value we
have given. (a < 20)
Comparison of
while loop and
do-while loop
// Example Using While-Loop
#include <stdio.h>
int main() {
int i=0;
while(i==1) {
printf("while vs do-while");
}
printf("Out of loop"); }
Output:
Out of loop
// Same example using do-while loop
#include <stdio.h>
int main() {
int i=0;
do {
printf("while vs do-whilen");
}while(i==1);
printf("Out of loop"); }
Output:
while vs do-while
Out of loop
THE END
10

Contenu connexe

Tendances

Program for pyramid
Program for pyramidProgram for pyramid
Program for pyramidnayakq
 
Android Developer Days: Increasing performance of big arrays processing on An...
Android Developer Days: Increasing performance of big arrays processing on An...Android Developer Days: Increasing performance of big arrays processing on An...
Android Developer Days: Increasing performance of big arrays processing on An...Stanfy
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++Bharat Kalia
 
Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)Syed Umair
 
Assignement of programming & problem solving(3)a.z
Assignement of programming & problem solving(3)a.zAssignement of programming & problem solving(3)a.z
Assignement of programming & problem solving(3)a.zSyed Umair
 
please sir i want to comments of every code what i do in eachline . in this w...
please sir i want to comments of every code what i do in eachline . in this w...please sir i want to comments of every code what i do in eachline . in this w...
please sir i want to comments of every code what i do in eachline . in this w...hwbloom27
 
Pert2 management
Pert2 managementPert2 management
Pert2 managementAhmed Gamal
 
verilog interview
verilog interview verilog interview
verilog interview Maitrik Shah
 
Scalability comparison: Traditional fork-join-based parallelism vs. Goroutine...
Scalability comparison: Traditional fork-join-based parallelism vs. Goroutine...Scalability comparison: Traditional fork-join-based parallelism vs. Goroutine...
Scalability comparison: Traditional fork-join-based parallelism vs. Goroutine...Artjom Simon
 

Tendances (20)

Program for pyramid
Program for pyramidProgram for pyramid
Program for pyramid
 
Roslyn: el futuro de C#
Roslyn: el futuro de C#Roslyn: el futuro de C#
Roslyn: el futuro de C#
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Open GL Tutorial10
Open GL Tutorial10Open GL Tutorial10
Open GL Tutorial10
 
1
11
1
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Android Developer Days: Increasing performance of big arrays processing on An...
Android Developer Days: Increasing performance of big arrays processing on An...Android Developer Days: Increasing performance of big arrays processing on An...
Android Developer Days: Increasing performance of big arrays processing on An...
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
MFC Cosinus
MFC CosinusMFC Cosinus
MFC Cosinus
 
Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)
 
Assignement of programming & problem solving(3)a.z
Assignement of programming & problem solving(3)a.zAssignement of programming & problem solving(3)a.z
Assignement of programming & problem solving(3)a.z
 
please sir i want to comments of every code what i do in eachline . in this w...
please sir i want to comments of every code what i do in eachline . in this w...please sir i want to comments of every code what i do in eachline . in this w...
please sir i want to comments of every code what i do in eachline . in this w...
 
Pert2 management
Pert2 managementPert2 management
Pert2 management
 
week-12x
week-12xweek-12x
week-12x
 
C coroutine
C coroutineC coroutine
C coroutine
 
C++ assignment
C++ assignmentC++ assignment
C++ assignment
 
verilog interview
verilog interview verilog interview
verilog interview
 
Scalability comparison: Traditional fork-join-based parallelism vs. Goroutine...
Scalability comparison: Traditional fork-join-based parallelism vs. Goroutine...Scalability comparison: Traditional fork-join-based parallelism vs. Goroutine...
Scalability comparison: Traditional fork-join-based parallelism vs. Goroutine...
 
Permute
PermutePermute
Permute
 

Similaire à Do while loop (20)

12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
Loops in c
Loops in cLoops in c
Loops in c
 
Looping statements
Looping statementsLooping statements
Looping statements
 
while loop in C.pptx
while loop in C.pptxwhile loop in C.pptx
while loop in C.pptx
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
Chp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptxChp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptx
 
CP Handout#5
CP Handout#5CP Handout#5
CP Handout#5
 
Rseminarp
RseminarpRseminarp
Rseminarp
 
Slicing of Object-Oriented Programs
Slicing of Object-Oriented ProgramsSlicing of Object-Oriented Programs
Slicing of Object-Oriented Programs
 
C++ programming
C++ programmingC++ programming
C++ programming
 
C++ programming
C++ programmingC++ programming
C++ programming
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
Nested and Infinite loops.pptx
Nested and Infinite loops.pptxNested and Infinite loops.pptx
Nested and Infinite loops.pptx
 
Session 3
Session 3Session 3
Session 3
 
C-PROGRAMING FOR LOOP PPT
C-PROGRAMING FOR LOOP PPTC-PROGRAMING FOR LOOP PPT
C-PROGRAMING FOR LOOP PPT
 
Discussing Fundamentals of C
Discussing Fundamentals of CDiscussing Fundamentals of C
Discussing Fundamentals of C
 
Nested loops
Nested loopsNested loops
Nested loops
 
C loops
C loopsC loops
C loops
 
Slicing of Object-Oriented Programs
Slicing of Object-Oriented ProgramsSlicing of Object-Oriented Programs
Slicing of Object-Oriented Programs
 

Plus de BU

Network Attached Storage Security
Network Attached Storage Security Network Attached Storage Security
Network Attached Storage Security BU
 
Variables
VariablesVariables
VariablesBU
 
Conditional operators
Conditional operatorsConditional operators
Conditional operatorsBU
 
Tarjumaye jadwali-quran-karim-pdf
Tarjumaye jadwali-quran-karim-pdfTarjumaye jadwali-quran-karim-pdf
Tarjumaye jadwali-quran-karim-pdfBU
 
Operating system concepts 5th edition by silberschatz & galvin
Operating system concepts 5th edition by silberschatz & galvinOperating system concepts 5th edition by silberschatz & galvin
Operating system concepts 5th edition by silberschatz & galvinBU
 
IP Routing
IP RoutingIP Routing
IP RoutingBU
 
Secure socket layer
Secure socket layerSecure socket layer
Secure socket layerBU
 
Handy back up installation
Handy back up installationHandy back up installation
Handy back up installationBU
 
Ip hiding using proxy
Ip hiding using proxyIp hiding using proxy
Ip hiding using proxyBU
 
Crash plane-backup-solution
Crash plane-backup-solutionCrash plane-backup-solution
Crash plane-backup-solutionBU
 

Plus de BU (10)

Network Attached Storage Security
Network Attached Storage Security Network Attached Storage Security
Network Attached Storage Security
 
Variables
VariablesVariables
Variables
 
Conditional operators
Conditional operatorsConditional operators
Conditional operators
 
Tarjumaye jadwali-quran-karim-pdf
Tarjumaye jadwali-quran-karim-pdfTarjumaye jadwali-quran-karim-pdf
Tarjumaye jadwali-quran-karim-pdf
 
Operating system concepts 5th edition by silberschatz & galvin
Operating system concepts 5th edition by silberschatz & galvinOperating system concepts 5th edition by silberschatz & galvin
Operating system concepts 5th edition by silberschatz & galvin
 
IP Routing
IP RoutingIP Routing
IP Routing
 
Secure socket layer
Secure socket layerSecure socket layer
Secure socket layer
 
Handy back up installation
Handy back up installationHandy back up installation
Handy back up installation
 
Ip hiding using proxy
Ip hiding using proxyIp hiding using proxy
Ip hiding using proxy
 
Crash plane-backup-solution
Crash plane-backup-solutionCrash plane-backup-solution
Crash plane-backup-solution
 

Dernier

TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 

Dernier (20)

TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 

Do while loop

  • 2. INTRODUCTION ▪ A do-while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time. ▪ Unlike for and while loops, do-while loop in C programming checks its condition at the bottom of the loop . 2
  • 4. “ 4 ○ The syntax of a do-while loop in C. do { statement(s); } while ( condition );
  • 7. • #include <stdio.h> • int main () { • /* local variable definition */ • int a = 10; • /* do loop execution */ • do { • printf("value of a: %dn", a); • a = a + 1; • } while ( a < 20 ); • return 0; • } Linear Search code in C 7
  • 8. Output 8 value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19 do { printf("value of a: %dn", a); First statement always gets printed before checking the condition even if condition is wrong. a = a + 1; The value is incremented by 1 each time. while ( a < 20 ); return 0; } After checking the condition, other statements gets printed based on condition value we have given. (a < 20)
  • 9. Comparison of while loop and do-while loop // Example Using While-Loop #include <stdio.h> int main() { int i=0; while(i==1) { printf("while vs do-while"); } printf("Out of loop"); } Output: Out of loop // Same example using do-while loop #include <stdio.h> int main() { int i=0; do { printf("while vs do-whilen"); }while(i==1); printf("Out of loop"); } Output: while vs do-while Out of loop