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...
For Loops
• A for loop is designed to count
a given number from one point
until it reaches another, every
instance running...
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 w...
For Loops
• The second line creates an
integer “how_high” and does
not initiate it.
int total = 0;
int how_high;
cout << “...
For Loops
• Letting the user input how high
to count… for this example,
let’s say they enter 4.
int total = 0;
int how_hig...
For Loops
• The for loop begins. Integer j is
initialized to = 1 (which is <
how_high) and the loop starts.
int total = 0;...
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 Loops
• Then the first iteration of the
loop finishes and it returns to
the for command…
int total = 0;
int how_high;
...
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;
i...
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 Loops
• j increases 1.
• j < how_high…
int total = 0;
int how_high;
cout << “how high would you
like to sum?”
cin >> h...
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 Loops
• j increases 1.
• j < how_high…
int total = 0;
int how_high;
cout << “how high would you
like to sum?”
cin >> h...
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 Loops
• The next value of j is >
how_high, so the for loop ends
and continues on in the
program, outputting “total = 1...
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
...
Upcoming SlideShare
Loading in …5
×

Counting and looping

540 views
424 views

Published on

Published in: Business, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
540
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Counting and looping

  1. 1. Counting and Looping Unit 3 Lecture for Intro to Computer Programming
  2. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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;

×