SlideShare a Scribd company logo
ITERATIVE/
REPETITIVE
STATEMENTS
OVERVIEW
OVERVIEW
 We often need to do repetitive calculations in order to solve
complex problems
 Example: calculate the trajectory of a cannon ball
 Humans are typically very bad at this
 Fortunately computers are very good at this
 To perform repetitive calculations in a program we need
iterative statements that let us execute the same block of
code multiple times
 Loops cause a section of program to be repeated certain
number of times.
 As long as condition remains true, the repetition continues,
when the condition becomes false, the loop ends and the
control passes to the statement following the loop.
2
OVERVIEW
 C++ has three kinds of iterative statements
 The for loop
 The while loop
 The do-while loop
 Lesson objectives:
 Learn the syntax and semantics of these iterative statements
 Study example programs showing their use
 Complete lab tasks on iterative statements
 Complete programming project using iterative statements
3
ITERATIVE
STATEMENTS
PART 1
FOR LOOP
FOR LOOP
 The C++ for loop provides a compact syntax for iteration
 For loops are typically used for counting loops, but they can
be used for any looping task
 Allows you to specify the following all on one line
 Initialization statement
 Logical expression/condition for continuing loop
 Statement to be executed after loop
 The for loop executes a section of code a fixed number of
times.
 “for loop” is used normally when we know, before entering
the loop, that how many times we want to execute the code
5
OPERATION OF FOR
LOOP
FOR LOOP
 The C++ syntax of the for loop is:
for ( initialization; condition ; increment/decrement)
{
// block of statements to be repeated
}
7
FOR LOOP
// For loop example
for ( int Num = 0; Num < 10; Num = Num+1 )
{
cout << Num << "cubed=" << Num*Num*Num << endl;
}
 The initialization statement is executed first
8
FOR LOOP
// For loop example
for ( int Num = 0; Num < 10; Num = Num+1 )
{
cout << Num << "cubed=" << Num*Num*Num << endl;
}
 Then the logical expression is evaluated
9
FOR LOOP
// For loop example
for ( int Num = 0; Num < 10; Num = Num+1 )
{
cout << Num << "cubed=" << Num*Num*Num << endl;
}
 If logical expression is true the block of code is executed
 If it is false, the program skips over the block of code
10
FOR LOOP
// For loop example
for ( int Num = 0; Num < 10; Num = Num+1 )
{
cout << Num << "cubed=" << Num*Num*Num << endl;
}
 Next the increment statement is executed
11
FOR LOOP
// For loop example
for ( int Num = 0; Num < 10; Num = Num+1 )
{
cout << Num << "cubed=" << Num*Num*Num << endl;
}
 Then the logical expression is evaluated again
 If it is true, we execute the block of code, the increment
statement and the logical expression again
 If it is false, the for loop ends
12
EXAMPLE OF A
SIMPLE FOR LOOP
int main()
{
for(int i=1; i<=6; i++)
{
/* This statement would be executed
* repeatedly until the condition
* i<=6 returns false.
*/
cout<<"Value of variable i is: "<<i<<endl;
}
}
13
FOR LOOP
14
int main()
{
// Program to find sum of first 10 numbers
int sum=0;
cout<<"AscendingtDescending"<<endl;
for(int a=1;a<=10;a++)
{
cout<<a<<"tt"<<(11-a)<<endl;
sum += a;
}
cout<<"Sum is "<<sum;
}
FOR LOOP EXAMPLE
// Input varying loop
int main()
{
int Num = 0;
for (cin >> Num; Num >= 0; cin >> Num)
{
cout << sqrt(Num) << " squared=" << Num << endl;
}
}
15
FACTORIAL !
int main()
{
int num, fact=1;
cout<<"Enter a number : ";
cin>>num;
for(int i=num; i>0 ; i--)
{
fact *=i;
}
cout<<"Factorial of "<<num<<" is "<<fact;
}
INFINITE FOR LOOP
 A loop becomes infinite loop if a condition never
becomes false.
 You can make an endless loop by leaving the
conditional expression empty or all three expressions
int main()
{
for(int i=1; ; i++)
{
cout<<“I is "<<i<<endl;
}
}
FOR LOOP
// Infinite loop example
for ( ; true ; )
cout << "Hello Worldn";
 Notice that the initialization statement is empty
 The increment statement is also empty
 We still need to have the semicolons in the for line
// Another example of infinite for loop
int main()
{
for(int i=1; i>=1; i++){
cout<<"Value of variable i is: "<<i<<endl;
}
18
AUTO INCREMENT
AND DECREMENT
 The auto increment operator “++” can be used to quickly
add one to an integer variable
 Instead of using i = i+1 we can use i++
 The auto decrement operator “--” can be used to quickly
subtract one from an integer variable
 Instead of using j = j-1 we can use j--
 These operators are used quite frequently in for loops,
especially counting loops to save typing
19
AUTO INCREMENT
AND DECREMENT
 The auto increment and decrement operations can also be
placed before the variable to add or subtract one
 There is a very subtle difference between ++i and i++
 “cout << ++i” will add one to i, and then print i
 “cout << i++” will print i, and then add one to i
20
AUTO INCREMENT
AND DECREMENT
 It is also possible to combine arithmetic operators with the
assignment operator to save typing (and improve speed)
 We can replace a = a + b with a += b
 Similarly c = c - d can be written as c -= d
 The operators *=, /=, %= are also supported
 This results in shorter and often faster code
21
AUTO INCREMENT
AND DECREMENT
// Example using compact operatorsint main()
{
int sum = 0;
int product = 1;
for (int count = 1; count < 10; count++)
{
sum += count;
product *= count;
}
cout<<"Sum = "<<sum<<endl;
cout<<"Product = "<<product<<endl;
}
22
SUMMARY
 In this section we have introduced the syntax and use of
the C++ for loop
 We also described how you can convert a for loop into a
while loop and vice versa
 Finally, we described the C++ auto increment and auto
decrement operators and related operations that combine
an arithmetic operation with assignment
23

More Related Content

PPTX
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
 
PPTX
Lec7 - Loops updated.pptx
NaumanRasheed11
 
PPT
Chapter 5 Looping
GhulamHussain142878
 
PPTX
operating system introduction to programming
farhannisar578
 
PPTX
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Krishna Raj
 
PPT
Counting and looping
dcorliss08
 
PPTX
Cs1123 6 loops
TAlha MAlik
 
PPTX
Lecture 5
Mohammed Khan
 
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
 
Lec7 - Loops updated.pptx
NaumanRasheed11
 
Chapter 5 Looping
GhulamHussain142878
 
operating system introduction to programming
farhannisar578
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Krishna Raj
 
Counting and looping
dcorliss08
 
Cs1123 6 loops
TAlha MAlik
 
Lecture 5
Mohammed Khan
 

Similar to 12-Lec - Repetition For Loop.pptx (20)

PPTX
LOOPS IN PROGRAMMING - Muqaddas Bin Tahir .pptx
ROOTs International , MIUC
 
PPT
03b loops
Manzoor ALam
 
PPT
Loops
abdulmanan366
 
PDF
Chapter 3
Amrit Kaur
 
PPTX
Chp4_C++_Control Structures-Part2_Iteration.pptx
ssuser10ed71
 
PPT
C++ control loops
pratikborsadiya
 
PPTX
Loops c++
Shivani Singh
 
PPT
Lecture 2
Mohammed Saleh
 
PPTX
Introduction to C++.pptx learn c++ and basic concepts of OOP
UbaidKhan930128
 
PPT
Control Statement.ppt
sanjay
 
PDF
Programming Fundamentals presentation slide
mibrahim020205
 
PPTX
Introduction to Loops using C++. Exploring While&Do-While Loop
MunawarAhmad22
 
PPTX
slidesgo-understanding-the-for-loop-in-c-principles-and-applications-20241116...
muhammadammarqaiser
 
PPT
FP 201 - Unit 3 Part 2
rohassanie
 
PPT
C++ CH3-P2 using c++ in all other parts.ppt
MutacalimMohamed
 
PPTX
Overview of c++ language
samt7
 
DOCX
C++ Loops General Discussion of Loops A loop is a.docx
humphrieskalyn
 
PPT
Lecture#5 Operators in C++
NUST Stuff
 
PPT
Ch4
aamirsahito
 
PPTX
Lecture 4
Mohammed Khan
 
LOOPS IN PROGRAMMING - Muqaddas Bin Tahir .pptx
ROOTs International , MIUC
 
03b loops
Manzoor ALam
 
Chapter 3
Amrit Kaur
 
Chp4_C++_Control Structures-Part2_Iteration.pptx
ssuser10ed71
 
C++ control loops
pratikborsadiya
 
Loops c++
Shivani Singh
 
Lecture 2
Mohammed Saleh
 
Introduction to C++.pptx learn c++ and basic concepts of OOP
UbaidKhan930128
 
Control Statement.ppt
sanjay
 
Programming Fundamentals presentation slide
mibrahim020205
 
Introduction to Loops using C++. Exploring While&Do-While Loop
MunawarAhmad22
 
slidesgo-understanding-the-for-loop-in-c-principles-and-applications-20241116...
muhammadammarqaiser
 
FP 201 - Unit 3 Part 2
rohassanie
 
C++ CH3-P2 using c++ in all other parts.ppt
MutacalimMohamed
 
Overview of c++ language
samt7
 
C++ Loops General Discussion of Loops A loop is a.docx
humphrieskalyn
 
Lecture#5 Operators in C++
NUST Stuff
 
Lecture 4
Mohammed Khan
 

More from AqeelAbbas94 (20)

PPT
trees_introduction.ppt ug yg i gg yg gj
AqeelAbbas94
 
PPT
ch6_2_v1.ppt yh jkkj jhjh h jhyj hg hghh
AqeelAbbas94
 
PPT
lecture_5 (2).ppt hjhrrgjbgrmgrhbgrgghjd
AqeelAbbas94
 
PPT
1-Lec - Introduction vhvv,vbvv,v (2).ppt
AqeelAbbas94
 
PPT
2-Lec - History of OOP and Java (1) .ppt
AqeelAbbas94
 
PPTX
Lecture-32-33.pptx
AqeelAbbas94
 
PPTX
10-Lec - Nested IF Statement.pptx
AqeelAbbas94
 
PPTX
blue.pptx
AqeelAbbas94
 
PPTX
use_case+use_case description.pptx
AqeelAbbas94
 
PPT
555e81217b39f1c1262b33d0.ppt
AqeelAbbas94
 
PPTX
hexagon.pptx
AqeelAbbas94
 
PPT
Lecture1.ppt
AqeelAbbas94
 
PDF
wepik-exploring-the-robust-features-of-samsung-health-app-enhancing-well-bein...
AqeelAbbas94
 
PPT
lecture56.ppt
AqeelAbbas94
 
PPT
04.ppt
AqeelAbbas94
 
PPT
19-Lec - Multidimensional Arrays.ppt
AqeelAbbas94
 
PPT
SelectionSort.ppt
AqeelAbbas94
 
PPT
Lecture2 (1).ppt
AqeelAbbas94
 
PPT
ch06.ppt
AqeelAbbas94
 
PPTX
Your Title goes Here.pptx
AqeelAbbas94
 
trees_introduction.ppt ug yg i gg yg gj
AqeelAbbas94
 
ch6_2_v1.ppt yh jkkj jhjh h jhyj hg hghh
AqeelAbbas94
 
lecture_5 (2).ppt hjhrrgjbgrmgrhbgrgghjd
AqeelAbbas94
 
1-Lec - Introduction vhvv,vbvv,v (2).ppt
AqeelAbbas94
 
2-Lec - History of OOP and Java (1) .ppt
AqeelAbbas94
 
Lecture-32-33.pptx
AqeelAbbas94
 
10-Lec - Nested IF Statement.pptx
AqeelAbbas94
 
blue.pptx
AqeelAbbas94
 
use_case+use_case description.pptx
AqeelAbbas94
 
555e81217b39f1c1262b33d0.ppt
AqeelAbbas94
 
hexagon.pptx
AqeelAbbas94
 
Lecture1.ppt
AqeelAbbas94
 
wepik-exploring-the-robust-features-of-samsung-health-app-enhancing-well-bein...
AqeelAbbas94
 
lecture56.ppt
AqeelAbbas94
 
04.ppt
AqeelAbbas94
 
19-Lec - Multidimensional Arrays.ppt
AqeelAbbas94
 
SelectionSort.ppt
AqeelAbbas94
 
Lecture2 (1).ppt
AqeelAbbas94
 
ch06.ppt
AqeelAbbas94
 
Your Title goes Here.pptx
AqeelAbbas94
 

Recently uploaded (20)

PDF
Hossain Kamyab on Mixing and Matching Furniture.pdf
Hossain Kamyab
 
PPTX
3. Introduction to Materials and springs.pptx
YESIMSMART
 
PPTX
VERTICAL TRANSPORTATION SYSTEMS.pptxhvcvcvvdh
jp0718076
 
PDF
PowerPoint Presentation -- Jennifer Kyte -- 9786400311489 -- ade9381d14f65b06...
Adeel452922
 
PPTX
Introduction-to-Graphic-Design-and-Adobe-Photoshop.pptx
abdullahedpk
 
PPTX
700315768-Linguistic-Connnnnnnnnntext.pptx
rossanthonytan1
 
PPTX
Residential_Interior_Design_No_Images.pptx
hasansarkeraidt
 
PPT
UNIT- 2 CARBON FOOT PRINT.ppt yvvuvvvvvvyvy
sriram270905
 
PDF
Line Sizing presentation about pipe sizes
anniebuzzfeed
 
PDF
Home_Decor_Presentation and idiea with decor
sp1357556
 
PPTX
Model PPT-1.pptx for research protocol or
drkalaivani
 
PDF
hees101.pdfyyyyyuywgbzhdtehwytjeyktweyga
pratap1004
 
PPT
Strengthening of an existing reinforced concrete structure.ppt
erdarshanpshah
 
PPTX
confluence of tradition in modernity- design approaches and design thinking
madhuvidya7
 
PPTX
The birth & Rise of python.pptx vaibhavd
vaibhavdobariyal79
 
PDF
Shayna Andrieze Yjasmin Goles - Your VA!
shaynagoles31
 
PDF
SS27 Environments & Design Trend Book Peclers Paris
Peclers Paris
 
PPTX
佛罗伦萨大学文凭办理|办理UNIFI毕业证学费单购买文凭在线制作
1cz3lou8
 
DOCX
Amplopxxxxxxxxxvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Lm Hardin 'Idin'
 
PPTX
Riverfront Development_nashikcity_landscape
aditikoshley2
 
Hossain Kamyab on Mixing and Matching Furniture.pdf
Hossain Kamyab
 
3. Introduction to Materials and springs.pptx
YESIMSMART
 
VERTICAL TRANSPORTATION SYSTEMS.pptxhvcvcvvdh
jp0718076
 
PowerPoint Presentation -- Jennifer Kyte -- 9786400311489 -- ade9381d14f65b06...
Adeel452922
 
Introduction-to-Graphic-Design-and-Adobe-Photoshop.pptx
abdullahedpk
 
700315768-Linguistic-Connnnnnnnnntext.pptx
rossanthonytan1
 
Residential_Interior_Design_No_Images.pptx
hasansarkeraidt
 
UNIT- 2 CARBON FOOT PRINT.ppt yvvuvvvvvvyvy
sriram270905
 
Line Sizing presentation about pipe sizes
anniebuzzfeed
 
Home_Decor_Presentation and idiea with decor
sp1357556
 
Model PPT-1.pptx for research protocol or
drkalaivani
 
hees101.pdfyyyyyuywgbzhdtehwytjeyktweyga
pratap1004
 
Strengthening of an existing reinforced concrete structure.ppt
erdarshanpshah
 
confluence of tradition in modernity- design approaches and design thinking
madhuvidya7
 
The birth & Rise of python.pptx vaibhavd
vaibhavdobariyal79
 
Shayna Andrieze Yjasmin Goles - Your VA!
shaynagoles31
 
SS27 Environments & Design Trend Book Peclers Paris
Peclers Paris
 
佛罗伦萨大学文凭办理|办理UNIFI毕业证学费单购买文凭在线制作
1cz3lou8
 
Amplopxxxxxxxxxvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Lm Hardin 'Idin'
 
Riverfront Development_nashikcity_landscape
aditikoshley2
 

12-Lec - Repetition For Loop.pptx

  • 2. OVERVIEW  We often need to do repetitive calculations in order to solve complex problems  Example: calculate the trajectory of a cannon ball  Humans are typically very bad at this  Fortunately computers are very good at this  To perform repetitive calculations in a program we need iterative statements that let us execute the same block of code multiple times  Loops cause a section of program to be repeated certain number of times.  As long as condition remains true, the repetition continues, when the condition becomes false, the loop ends and the control passes to the statement following the loop. 2
  • 3. OVERVIEW  C++ has three kinds of iterative statements  The for loop  The while loop  The do-while loop  Lesson objectives:  Learn the syntax and semantics of these iterative statements  Study example programs showing their use  Complete lab tasks on iterative statements  Complete programming project using iterative statements 3
  • 5. FOR LOOP  The C++ for loop provides a compact syntax for iteration  For loops are typically used for counting loops, but they can be used for any looping task  Allows you to specify the following all on one line  Initialization statement  Logical expression/condition for continuing loop  Statement to be executed after loop  The for loop executes a section of code a fixed number of times.  “for loop” is used normally when we know, before entering the loop, that how many times we want to execute the code 5
  • 7. FOR LOOP  The C++ syntax of the for loop is: for ( initialization; condition ; increment/decrement) { // block of statements to be repeated } 7
  • 8. FOR LOOP // For loop example for ( int Num = 0; Num < 10; Num = Num+1 ) { cout << Num << "cubed=" << Num*Num*Num << endl; }  The initialization statement is executed first 8
  • 9. FOR LOOP // For loop example for ( int Num = 0; Num < 10; Num = Num+1 ) { cout << Num << "cubed=" << Num*Num*Num << endl; }  Then the logical expression is evaluated 9
  • 10. FOR LOOP // For loop example for ( int Num = 0; Num < 10; Num = Num+1 ) { cout << Num << "cubed=" << Num*Num*Num << endl; }  If logical expression is true the block of code is executed  If it is false, the program skips over the block of code 10
  • 11. FOR LOOP // For loop example for ( int Num = 0; Num < 10; Num = Num+1 ) { cout << Num << "cubed=" << Num*Num*Num << endl; }  Next the increment statement is executed 11
  • 12. FOR LOOP // For loop example for ( int Num = 0; Num < 10; Num = Num+1 ) { cout << Num << "cubed=" << Num*Num*Num << endl; }  Then the logical expression is evaluated again  If it is true, we execute the block of code, the increment statement and the logical expression again  If it is false, the for loop ends 12
  • 13. EXAMPLE OF A SIMPLE FOR LOOP int main() { for(int i=1; i<=6; i++) { /* This statement would be executed * repeatedly until the condition * i<=6 returns false. */ cout<<"Value of variable i is: "<<i<<endl; } } 13
  • 14. FOR LOOP 14 int main() { // Program to find sum of first 10 numbers int sum=0; cout<<"AscendingtDescending"<<endl; for(int a=1;a<=10;a++) { cout<<a<<"tt"<<(11-a)<<endl; sum += a; } cout<<"Sum is "<<sum; }
  • 15. FOR LOOP EXAMPLE // Input varying loop int main() { int Num = 0; for (cin >> Num; Num >= 0; cin >> Num) { cout << sqrt(Num) << " squared=" << Num << endl; } } 15
  • 16. FACTORIAL ! int main() { int num, fact=1; cout<<"Enter a number : "; cin>>num; for(int i=num; i>0 ; i--) { fact *=i; } cout<<"Factorial of "<<num<<" is "<<fact; }
  • 17. INFINITE FOR LOOP  A loop becomes infinite loop if a condition never becomes false.  You can make an endless loop by leaving the conditional expression empty or all three expressions int main() { for(int i=1; ; i++) { cout<<“I is "<<i<<endl; } }
  • 18. FOR LOOP // Infinite loop example for ( ; true ; ) cout << "Hello Worldn";  Notice that the initialization statement is empty  The increment statement is also empty  We still need to have the semicolons in the for line // Another example of infinite for loop int main() { for(int i=1; i>=1; i++){ cout<<"Value of variable i is: "<<i<<endl; } 18
  • 19. AUTO INCREMENT AND DECREMENT  The auto increment operator “++” can be used to quickly add one to an integer variable  Instead of using i = i+1 we can use i++  The auto decrement operator “--” can be used to quickly subtract one from an integer variable  Instead of using j = j-1 we can use j--  These operators are used quite frequently in for loops, especially counting loops to save typing 19
  • 20. AUTO INCREMENT AND DECREMENT  The auto increment and decrement operations can also be placed before the variable to add or subtract one  There is a very subtle difference between ++i and i++  “cout << ++i” will add one to i, and then print i  “cout << i++” will print i, and then add one to i 20
  • 21. AUTO INCREMENT AND DECREMENT  It is also possible to combine arithmetic operators with the assignment operator to save typing (and improve speed)  We can replace a = a + b with a += b  Similarly c = c - d can be written as c -= d  The operators *=, /=, %= are also supported  This results in shorter and often faster code 21
  • 22. AUTO INCREMENT AND DECREMENT // Example using compact operatorsint main() { int sum = 0; int product = 1; for (int count = 1; count < 10; count++) { sum += count; product *= count; } cout<<"Sum = "<<sum<<endl; cout<<"Product = "<<product<<endl; } 22
  • 23. SUMMARY  In this section we have introduced the syntax and use of the C++ for loop  We also described how you can convert a for loop into a while loop and vice versa  Finally, we described the C++ auto increment and auto decrement operators and related operations that combine an arithmetic operation with assignment 23