SlideShare a Scribd company logo
1 of 8
C++ Programming
Understanding programming languages
Mohammed Jehan
Lecturer
The for loop
A for loop is a repetition control structure that allows you to efficiently write a loop that executes a specific number
of times.
Syntax:
for ( init; condition; increment ) {
statement(s);
}
The init step is executed first, and does not repeat.
Next, the condition is evaluated, and the body of the loop is executed if the condition is true.
In the next step, the increment statement updates the loop control variable.
Then, the loop's body repeats itself, only stopping when the condition becomes false.
For example:
for (int x = 1; x < 10; x++) {
// some code
}
The for Loop
The example below uses a for loop to print numbers from 0 to 9.
for (int a = 0; a < 10; a++) {
cout << a << endl;
}
In the init step, we declared a variable a and set it to equal 0.
a < 10 is the condition.
After each iteration, the a++ increment statement is executed.
When a increments to 10, the condition evaluates to false, and the loop stops.
The for Loop
It's possible to change the increment statement.
for (int a = 0; a < 50; a+=10) {
cout << a << endl;
}
It's possible to change the increment statement.
for (int a = 10; a >= 0; a -= 3) {
cout << a << endl;
}
The do...while Loop
Unlike for and while loops, which test the loop condition at the top of the loop,
the do...while loop checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop. The one difference is that the
do...while loop is guaranteed to execute at least one time.
do {
statement(s);
} while (condition);
For example, you can take input from the user, then check it. If the input is
wrong, you can take it again.
The do...while Loop
Here is an example:
int a = 0;
do {
cout << a << endl;
a++;
} while(a < 5);
while vs. do...while
If the condition evaluated to false, the statements in the do would still run once:
int a = 42;
do {
cout << a << endl;
a++;
} while(a < 5);
while vs. do...while
As with other loops, if the condition in the loop never evaluates to false, the loop
will run forever.
For example:
int a = 42;
do {
cout << a << endl;
} while (a > 0);
This will print 42 to the screen forever.

More Related Content

What's hot

loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docxJavvajiVenkat
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in JavaJin Castor
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While LoopAbhishek Choksi
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loopsbsdeol28
 
Loops Basics
Loops BasicsLoops Basics
Loops BasicsMushiii
 
C++ control structure
C++ control structureC++ control structure
C++ control structurebluejayjunior
 
Loop (Computer programming and utilization)
Loop (Computer programming and utilization)Loop (Computer programming and utilization)
Loop (Computer programming and utilization)Digvijaysinh Gohil
 
Looping statement
Looping statementLooping statement
Looping statementilakkiya
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Deepak Singh
 
Conditional Loops Python
Conditional Loops PythonConditional Loops Python
Conditional Loops Pythonprimeteacher32
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements loopingMomenMostafa
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareGagan Deep
 

What's hot (20)

loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
Java Programming: Loops
Java Programming: LoopsJava Programming: Loops
Java Programming: Loops
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
Loop c++
Loop c++Loop c++
Loop c++
 
Looping in C
Looping in CLooping in C
Looping in C
 
ICP - Lecture 9
ICP - Lecture 9ICP - Lecture 9
ICP - Lecture 9
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
Loops Basics
Loops BasicsLoops Basics
Loops Basics
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
 
Loop (Computer programming and utilization)
Loop (Computer programming and utilization)Loop (Computer programming and utilization)
Loop (Computer programming and utilization)
 
Loops in c++
Loops in c++Loops in c++
Loops in c++
 
Looping statement
Looping statementLooping statement
Looping statement
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
 
Conditional Loops Python
Conditional Loops PythonConditional Loops Python
Conditional Loops Python
 
Java Programmin: Selections
Java Programmin: SelectionsJava Programmin: Selections
Java Programmin: Selections
 
14 - Exceptions
14 - Exceptions14 - Exceptions
14 - Exceptions
 
For Loop
For LoopFor Loop
For Loop
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
 

Similar to Lecture 5

Similar to Lecture 5 (20)

Programming Fundamentals lecture 8
Programming Fundamentals lecture 8Programming Fundamentals lecture 8
Programming Fundamentals lecture 8
 
Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Visula C# Programming Lecture 4
Visula C# Programming Lecture 4
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
 
Loops in C.net.pptx
Loops in C.net.pptxLoops in C.net.pptx
Loops in C.net.pptx
 
Chap 6(decision making-looping)
Chap 6(decision making-looping)Chap 6(decision making-looping)
Chap 6(decision making-looping)
 
5.pptx fundamental programing one branch
5.pptx fundamental programing one branch5.pptx fundamental programing one branch
5.pptx fundamental programing one branch
 
Introduction to programming in C++ : Loop Structure.pptx
Introduction to programming in C++ : Loop Structure.pptxIntroduction to programming in C++ : Loop Structure.pptx
Introduction to programming in C++ : Loop Structure.pptx
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdf
 
loops in C ppt.pdf
loops in C ppt.pdfloops in C ppt.pdf
loops in C ppt.pdf
 
While loop
While loopWhile loop
While loop
 
15-Loops.ppt
15-Loops.ppt15-Loops.ppt
15-Loops.ppt
 
Loops and iteration.docx
Loops and iteration.docxLoops and iteration.docx
Loops and iteration.docx
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
 
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
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Programming loop
Programming loopProgramming loop
Programming loop
 
cpu.pdf
cpu.pdfcpu.pdf
cpu.pdf
 
LOOPS AND DECISIONS
LOOPS AND DECISIONSLOOPS AND DECISIONS
LOOPS AND DECISIONS
 
Control structures
Control structuresControl structures
Control structures
 

More from Mohammed Khan

Module 11 therapeutic intervention play yoga
Module 11 therapeutic  intervention  play yogaModule 11 therapeutic  intervention  play yoga
Module 11 therapeutic intervention play yogaMohammed Khan
 
Module 10 principles of learning, practice, reinforcement understanding spec...
Module 10 principles of learning, practice, reinforcement understanding  spec...Module 10 principles of learning, practice, reinforcement understanding  spec...
Module 10 principles of learning, practice, reinforcement understanding spec...Mohammed Khan
 
Module 3 special education
Module 3 special educationModule 3 special education
Module 3 special educationMohammed Khan
 

More from Mohammed Khan (17)

Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Module 11 therapeutic intervention play yoga
Module 11 therapeutic  intervention  play yogaModule 11 therapeutic  intervention  play yoga
Module 11 therapeutic intervention play yoga
 
Module 10 principles of learning, practice, reinforcement understanding spec...
Module 10 principles of learning, practice, reinforcement understanding  spec...Module 10 principles of learning, practice, reinforcement understanding  spec...
Module 10 principles of learning, practice, reinforcement understanding spec...
 
Module 9 speci ed
Module 9 speci edModule 9 speci ed
Module 9 speci ed
 
Module 8 spec ed
Module 8 spec edModule 8 spec ed
Module 8 spec ed
 
Module 7 special ed
Module 7  special edModule 7  special ed
Module 7 special ed
 
Module 6 spec ed
Module 6 spec edModule 6 spec ed
Module 6 spec ed
 
Module 5 special ed
Module 5 special edModule 5 special ed
Module 5 special ed
 
Module 4 spec needs
Module 4 spec needsModule 4 spec needs
Module 4 spec needs
 
Module 3 special education
Module 3 special educationModule 3 special education
Module 3 special education
 
Module 2 special ed
Module 2 special edModule 2 special ed
Module 2 special ed
 
Module 1 special ed
Module 1 special edModule 1 special ed
Module 1 special ed
 

Recently uploaded

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 

Recently uploaded (20)

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 

Lecture 5

  • 1. C++ Programming Understanding programming languages Mohammed Jehan Lecturer
  • 2. The for loop A for loop is a repetition control structure that allows you to efficiently write a loop that executes a specific number of times. Syntax: for ( init; condition; increment ) { statement(s); } The init step is executed first, and does not repeat. Next, the condition is evaluated, and the body of the loop is executed if the condition is true. In the next step, the increment statement updates the loop control variable. Then, the loop's body repeats itself, only stopping when the condition becomes false. For example: for (int x = 1; x < 10; x++) { // some code }
  • 3. The for Loop The example below uses a for loop to print numbers from 0 to 9. for (int a = 0; a < 10; a++) { cout << a << endl; } In the init step, we declared a variable a and set it to equal 0. a < 10 is the condition. After each iteration, the a++ increment statement is executed. When a increments to 10, the condition evaluates to false, and the loop stops.
  • 4. The for Loop It's possible to change the increment statement. for (int a = 0; a < 50; a+=10) { cout << a << endl; } It's possible to change the increment statement. for (int a = 10; a >= 0; a -= 3) { cout << a << endl; }
  • 5. The do...while Loop Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop checks its condition at the bottom of the loop. A do...while loop is similar to a while loop. The one difference is that the do...while loop is guaranteed to execute at least one time. do { statement(s); } while (condition); For example, you can take input from the user, then check it. If the input is wrong, you can take it again.
  • 6. The do...while Loop Here is an example: int a = 0; do { cout << a << endl; a++; } while(a < 5);
  • 7. while vs. do...while If the condition evaluated to false, the statements in the do would still run once: int a = 42; do { cout << a << endl; a++; } while(a < 5);
  • 8. while vs. do...while As with other loops, if the condition in the loop never evaluates to false, the loop will run forever. For example: int a = 42; do { cout << a << endl; } while (a > 0); This will print 42 to the screen forever.