SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Counting and Looping
Unit 3 Lecture for
Intro to Computer Programming
Introduction
• Counting in programming can
be a very useful tool for,
among other things,
mathematical calculations.
• The primary function for
counting in c++ is the for loop.
• Loops are used when a
process needs to be repeated
either a certain number of
times, or arbitrarily until a
certain set of conditions
become true.
• The primary looping function
(aside from for loops) we will
look at is the while loop.
For Loops
• A for loop is designed to count
a given number from one point
until it reaches another, every
instance running the
commands within the loop
• The following code is an
example of a for loop designed
to add consecutive integers,
starting at 1 up to a user-
inputted number:
int total = 0;
int how_high;
cout << “how high would you
like to sum?”
cin >> how_high;
for (int j = 1; j <= how_high; j+
+)
{
total = total + j;
}
cout << “total = “ << total;
For Loops
• We’re going to break this down
one step at a time, just as a
computer would execute it, to
see how this loop works.
• The very first line initializes an
integer variable “total” to equal
0.
int total = 0;
int how_high;
cout << “how high would you
like to sum?”
cin >> how_high;
for (int j = 1; j <= how_high; j+
+)
{
total = total + j;
}
cout << “total = “ << total;
total = 0
For Loops
• The second line creates an
integer “how_high” and does
not initiate it.
int total = 0;
int how_high;
cout << “how high would you
like to sum?”
cin >> how_high;
for (int j = 1; j <= how_high; j+
+)
{
total = total + j;
}
cout << “total = “ << total;
total = 0
how_high
For Loops
• Letting the user input how high
to count… for this example,
let’s say they enter 4.
int total = 0;
int how_high;
cout << “how high would you
like to sum?”
cin >> how_high;
for (int j = 1; j <= how_high; j+
+)
{
total = total + j;
}
cout << “total = “ << total;
total = 0
how_high = 4
For Loops
• The for loop begins. Integer j is
initialized to = 1 (which is <
how_high) and the loop starts.
int total = 0;
int how_high;
cout << “how high would you
like to sum?”
cin >> how_high;
for (int j = 1; j <= how_high; j+
+)
{
total = total + j;
}
cout << “total = “ << total;
total = 0
how_high = 4
j = 1
For Loops
• total is increased by j.int total = 0;
int how_high;
cout << “how high would you
like to sum?”
cin >> how_high;
for (int j = 1; j <= how_high; j+
+)
{
total = total + j;
}
cout << “total = “ << total;
total = 1
how_high = 4
j = 1
For Loops
• Then the first iteration of the
loop finishes and it returns to
the for command…
int total = 0;
int how_high;
cout << “how high would you
like to sum?”
cin >> how_high;
for (int j = 1; j <= how_high; j+
+)
{
total = total + j;
}
cout << “total = “ << total;
total = 1
how_high = 4
j = 1
For Loops
• j++ is shorthand for “j = j + 1”
so the value for j bumps up to
2, which is still < how_high.
int total = 0;
int how_high;
cout << “how high would you
like to sum?”
cin >> how_high;
for (int j = 1; j <= how_high; j+
+)
{
total = total + j;
}
cout << “total = “ << total;
total = 1
how_high = 4
j = 2
For Loops
• total is increased by j.int total = 0;
int how_high;
cout << “how high would you
like to sum?”
cin >> how_high;
for (int j = 1; j <= how_high; j+
+)
{
total = total + j;
}
cout << “total = “ << total;
total = 3
how_high = 4
j = 2
For Loops
• j increases 1.
• j < how_high…
int total = 0;
int how_high;
cout << “how high would you
like to sum?”
cin >> how_high;
for (int j = 1; j <= how_high; j+
+)
{
total = total + j;
}
cout << “total = “ << total;
total = 3
how_high = 4
j = 3
For Loops
• total is increased by j.int total = 0;
int how_high;
cout << “how high would you
like to sum?”
cin >> how_high;
for (int j = 1; j <= how_high; j+
+)
{
total = total + j;
}
cout << “total = “ << total;
total = 6
how_high = 4
j = 3
For Loops
• j increases 1.
• j < how_high…
int total = 0;
int how_high;
cout << “how high would you
like to sum?”
cin >> how_high;
for (int j = 1; j <= how_high; j+
+)
{
total = total + j;
}
cout << “total = “ << total;
total = 6
how_high = 4
j = 4
For Loops
• total is increased by j.int total = 0;
int how_high;
cout << “how high would you
like to sum?”
cin >> how_high;
for (int j = 1; j <= how_high; j+
+)
{
total = total + j;
}
cout << “total = “ << total;
total = 10
how_high = 4
j = 4
For Loops
• The next value of j is >
how_high, so the for loop ends
and continues on in the
program, outputting “total = 10”
int total = 0;
int how_high;
cout << “how high would you
like to sum?”
cin >> how_high;
for (int j = 1; j <= how_high; j+
+)
{
total = total + j;
}
cout << “total = “ << total;
total = 10
how_high = 4
j = 5
While Loops
• While loops can be much more
powerful than a for loop, and
can even be made to do the
exact same thing, but
sometimes it’s a bit more
wordy…
• The following code is does the
same thing as above, only
using a while loop:
(we won’t go through the step by step of
this… if you would like to see it, it is quite
easy to write a program to show it to you.)
int total = 0;
int how_high;
cout << “how high would you
like to sum?”
cin >> how_high;
j = 1;
do
{
total = total + j;
j = j + 1;
} while (j <= how_high)
cout << “total = “ << total;

Weitere ähnliche Inhalte

Ähnlich wie Counting and looping

Ähnlich wie Counting and looping (20)

DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
 
Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1
 
2 BytesC++ course_2014_c2_ flow of control
2 BytesC++ course_2014_c2_ flow of control 2 BytesC++ course_2014_c2_ flow of control
2 BytesC++ course_2014_c2_ flow of control
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
ICP - Lecture 9
ICP - Lecture 9ICP - Lecture 9
ICP - Lecture 9
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020
 
Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020
 
Fekra c++ Course #2
Fekra c++ Course #2Fekra c++ Course #2
Fekra c++ Course #2
 
Introduction to c part -1
Introduction to c   part -1Introduction to c   part -1
Introduction to c part -1
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Looping
LoopingLooping
Looping
 
Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1
 
12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
 
Ch4
Ch4Ch4
Ch4
 
Nested loops
Nested loopsNested loops
Nested loops
 

KĂźrzlich hochgeladen

BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRLBAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRLkapoorjyoti4444
 
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...amitlee9823
 
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 MonthsSEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 MonthsIndeedSEO
 
Katrina Personal Brand Project and portfolio 1
Katrina Personal Brand Project and portfolio 1Katrina Personal Brand Project and portfolio 1
Katrina Personal Brand Project and portfolio 1kcpayne
 
Falcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business GrowthFalcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business GrowthFalcon investment
 
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...amitlee9823
 
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...Sheetaleventcompany
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangaloreamitlee9823
 
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...Anamikakaur10
 
Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876
Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876
Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876dlhescort
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableSeo
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesMysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesDipal Arora
 
RSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors DataRSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors DataExhibitors Data
 
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756dollysharma2066
 
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...daisycvs
 
How to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League CityHow to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League CityEric T. Tung
 
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...lizamodels9
 
Malegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort Service
Malegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort ServiceMalegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort Service
Malegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort ServiceDamini Dixit
 

KĂźrzlich hochgeladen (20)

BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRLBAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
BAGALUR CALL GIRL IN 98274*61493 ❤CALL GIRLS IN ESCORT SERVICE❤CALL GIRL
 
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
 
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 MonthsSEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
 
Katrina Personal Brand Project and portfolio 1
Katrina Personal Brand Project and portfolio 1Katrina Personal Brand Project and portfolio 1
Katrina Personal Brand Project and portfolio 1
 
Falcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business GrowthFalcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business Growth
 
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
 
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
 
Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876
Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876
Cheap Rate Call Girls In Noida Sector 62 Metro 959961乂3876
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
 
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabiunwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesMysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
 
RSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors DataRSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors Data
 
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
 
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
 
How to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League CityHow to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League City
 
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
 
Malegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort Service
Malegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort ServiceMalegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort Service
Malegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort Service
 

Counting and looping

  • 1. Counting and Looping Unit 3 Lecture for Intro to Computer Programming
  • 2. Introduction • Counting in programming can be a very useful tool for, among other things, mathematical calculations. • The primary function for counting in c++ is the for loop. • Loops are used when a process needs to be repeated either a certain number of times, or arbitrarily until a certain set of conditions become true. • The primary looping function (aside from for loops) we will look at is the while loop.
  • 3. For Loops • A for loop is designed to count a given number from one point until it reaches another, every instance running the commands within the loop • The following code is an example of a for loop designed to add consecutive integers, starting at 1 up to a user- inputted number: int total = 0; int how_high; cout << “how high would you like to sum?” cin >> how_high; for (int j = 1; j <= how_high; j+ +) { total = total + j; } cout << “total = “ << total;
  • 4. For Loops • We’re going to break this down one step at a time, just as a computer would execute it, to see how this loop works. • The very first line initializes an integer variable “total” to equal 0. int total = 0; int how_high; cout << “how high would you like to sum?” cin >> how_high; for (int j = 1; j <= how_high; j+ +) { total = total + j; } cout << “total = “ << total; total = 0
  • 5. For Loops • The second line creates an integer “how_high” and does not initiate it. int total = 0; int how_high; cout << “how high would you like to sum?” cin >> how_high; for (int j = 1; j <= how_high; j+ +) { total = total + j; } cout << “total = “ << total; total = 0 how_high
  • 6. For Loops • Letting the user input how high to count… for this example, let’s say they enter 4. int total = 0; int how_high; cout << “how high would you like to sum?” cin >> how_high; for (int j = 1; j <= how_high; j+ +) { total = total + j; } cout << “total = “ << total; total = 0 how_high = 4
  • 7. For Loops • The for loop begins. Integer j is initialized to = 1 (which is < how_high) and the loop starts. int total = 0; int how_high; cout << “how high would you like to sum?” cin >> how_high; for (int j = 1; j <= how_high; j+ +) { total = total + j; } cout << “total = “ << total; total = 0 how_high = 4 j = 1
  • 8. For Loops • total is increased by j.int total = 0; int how_high; cout << “how high would you like to sum?” cin >> how_high; for (int j = 1; j <= how_high; j+ +) { total = total + j; } cout << “total = “ << total; total = 1 how_high = 4 j = 1
  • 9. For Loops • Then the first iteration of the loop finishes and it returns to the for command… int total = 0; int how_high; cout << “how high would you like to sum?” cin >> how_high; for (int j = 1; j <= how_high; j+ +) { total = total + j; } cout << “total = “ << total; total = 1 how_high = 4 j = 1
  • 10. For Loops • j++ is shorthand for “j = j + 1” so the value for j bumps up to 2, which is still < how_high. int total = 0; int how_high; cout << “how high would you like to sum?” cin >> how_high; for (int j = 1; j <= how_high; j+ +) { total = total + j; } cout << “total = “ << total; total = 1 how_high = 4 j = 2
  • 11. For Loops • total is increased by j.int total = 0; int how_high; cout << “how high would you like to sum?” cin >> how_high; for (int j = 1; j <= how_high; j+ +) { total = total + j; } cout << “total = “ << total; total = 3 how_high = 4 j = 2
  • 12. For Loops • j increases 1. • j < how_high… int total = 0; int how_high; cout << “how high would you like to sum?” cin >> how_high; for (int j = 1; j <= how_high; j+ +) { total = total + j; } cout << “total = “ << total; total = 3 how_high = 4 j = 3
  • 13. For Loops • total is increased by j.int total = 0; int how_high; cout << “how high would you like to sum?” cin >> how_high; for (int j = 1; j <= how_high; j+ +) { total = total + j; } cout << “total = “ << total; total = 6 how_high = 4 j = 3
  • 14. For Loops • j increases 1. • j < how_high… int total = 0; int how_high; cout << “how high would you like to sum?” cin >> how_high; for (int j = 1; j <= how_high; j+ +) { total = total + j; } cout << “total = “ << total; total = 6 how_high = 4 j = 4
  • 15. For Loops • total is increased by j.int total = 0; int how_high; cout << “how high would you like to sum?” cin >> how_high; for (int j = 1; j <= how_high; j+ +) { total = total + j; } cout << “total = “ << total; total = 10 how_high = 4 j = 4
  • 16. For Loops • The next value of j is > how_high, so the for loop ends and continues on in the program, outputting “total = 10” int total = 0; int how_high; cout << “how high would you like to sum?” cin >> how_high; for (int j = 1; j <= how_high; j+ +) { total = total + j; } cout << “total = “ << total; total = 10 how_high = 4 j = 5
  • 17. While Loops • While loops can be much more powerful than a for loop, and can even be made to do the exact same thing, but sometimes it’s a bit more wordy… • The following code is does the same thing as above, only using a while loop: (we won’t go through the step by step of this… if you would like to see it, it is quite easy to write a program to show it to you.) int total = 0; int how_high; cout << “how high would you like to sum?” cin >> how_high; j = 1; do { total = total + j; j = j + 1; } while (j <= how_high) cout << “total = “ << total;