Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 4: Loops and Iteration Graham Northup

Similar presentations


Presentation on theme: "Lab 4: Loops and Iteration Graham Northup"— Presentation transcript:

1 Lab 4: Loops and Iteration Graham Northup

2 Objectives To understand and use while and do/while loops.
To become familiar with debugging and, particularly, gdb—the GNU Debugger.

3 cout << "Factor cannot be negative!;"
Review: Control Flow PC if(b < 0) { cout << "Factor cannot be negative!\n"; return 1; } c = a * b; PC (start) PC b < 0 True False cout << "Factor cannot be negative!;" return 1; (end)

4 Review: Control Flow if(foobar == 7) {
cout << "Your foobar is a 7? Mine too!\n"; } else { cout << "Aww, my foobar is a 7.\n"; } (start) foobar == 7 False True cout << "Your foobar is a 7? Mine too!\n"; cout << "Aww, my foobar is a 7.\n"; (end)

5 Iteration Selection using the if/else and switch/case statements are essential to making functional, effective programs. However, they are not the only tool; in particular, many programs don’t have the size of the data they will be dealing with specified in advance. In such cases, the ability to repeat a process becomes important. This feature is known as iteration, and we will be studying two of the forms it takes in C.

6 While Loop #include <iostream> using namespace std;
int main() { int i; cout << "Counting to ten...\n"; i = 1; while(i < 10) { cout << i << "...\n"; i = i + 1; } cout << "10!\n"; return 0; The first form is the while loop, which is quite a bit like the if statement... Like the if statement, it has a condition; also like it, if the condition is true, the body is executed; otherwise, it is skipped. PC Unlike the if statement, after the body is done executing, the program counter returns to the top of the while loop, where the condition is checked again. This continues to happen “while” the condition is true. PC This is legal! PC

7 While Loop (start) As a flowchart... i = 1; True i < 10 False
cout << i << "...\n"; i = i + 1; cout << "10!\n"; (end)

8 While Loop (start) i ? i = 1; i < 10 True False
cout << i << "...\n"; i = i + 1; cout << "10!\n"; (end)

9 While Loop (start) i 1 i = 1; i < 10 True False
cout << i << "...\n"; i = i + 1; cout << "10!\n"; (end)

10 While Loop (start) i 2 i = 1; i was 1 here i < 10 True False
cout << i << "...\n"; i = i + 1; cout << "10!\n"; (end)

11 While Loop (start) i 3 i = 1; ...and so forth... i was 2 here
True False cout << i << "...\n"; i = i + 1; cout << "10!\n"; (end)

12 While Loop (start) i 10 i = 1; i was 9 here i < 10 True False
cout << i << "...\n"; i = i + 1; cout << "10!\n"; (end)

13 While Loop (start) i 10 i = 1; i was 9 here i < 10 True False
cout << i << "...\n"; i = i + 1; cout << "10!\n"; (end)

14 Do/While Loops #include <iostream> using namespace std;
int main() { int ergs, ergnum; do { cout << "Print out this many ergs: "; cin >> ergs; if(ergs <= 0) { cout << "Must be at least one erg!\n"; } } while(ergs <= 0); ergnum = 0; while(ergnum < ergs) { cout << "erg"; ergnum = ergnum + 1; cout << "\n"; return 0; The second form of iteration we’re covering this week is the do/while loop, which is the same as the while loop, but instead of testing the condition at the beginning, tests it instead at the end. This is reflected in the source order; the condition comes after the block, and the block always executes at least once. Note also the semicolon after the condition—perhaps the only time you’ll ever see it there.

15 Do/While Loops #include <iostream> using namespace std;
int main() { int ergs, ergnum; do { cout << "Print out this many ergs: "; cin >> ergs; if(ergs <= 0) { cout << "Must be at least one erg!\n"; } } while(ergs <= 0); ergnum = 0; while(ergnum < ergs) { cout << "erg"; ergnum = ergnum + 1; cout << "\n"; return 0; This little construct is rather popular, and is used relatively frequently to validate user input. Each iteration of the loop asks the user to enter a “correct” value; the redundant if on the inside is just there to belittle them for doing it wrong :)

16 Do/While Loops (start) cout << "Print out this many ergs: ";
cin >> ergs; if statement ergs <= 0 True False cout << "Must be at least one erg!\n"; ergs <= 0 True Iteration path False (end)

17 Activity: Factorial The factorial function, commonly denoted n!, is a function of natural numbers (unsigned integers) whose value is the product of the sequence 1, 2, …, n. #include <iostream> using namespace std; int main() { unsigned number, i, product; cout << "Calculate the factorial of? "; cin >> number; product = 1; while(i <= number) { product = product * i; i = i + 1; } cout << "The factorial of " << number << \ " is " << product << "\n"; return 0;

18 Uhm... Oops.

19 Debugging. Until now, we’ve been dealing with programs that are fairly trivial, and mistakes that usually involve missing semicolons or braces. Unfortunately, good syntax can only go so far; sometimes our logic is fundamentally flawed...or sometimes we just forget to set a variable. In cases like these, the programmers have a friend: a debugger.

20 Command Line Revisited
g++ -o myprogname -g myprogname.cpp Output executable/object name Input filename(s) Option: debug info gdb ./myprogname “GNU Debugger”

21 Starting the Debugger

22 Running in the Debugger

23 Setting Breakpoints We’d like to find out what’s happening around here, starting with line 10...

24 Setting Breakpoints Asking the debugger to stop at this line
The debugger stops at line 10, shows us what source code is there, and asks us what to do. The program is still running, but paused! The line it’s showing is the one it’s about to run if we ask it to continue.

25 Observing State Just about all C expressions are allowed! You can access any variables by name as usual.

26 Continuing Conditionally
Just executed PC

27 Continuing Conditionally
Uhh... Just executed PC

28 Continuing Unconditionally

29 Exiting the Debugger There are pretty much three things you need to remember to get out of a program at your terminal: exit quit :wq

30 Fixing Bugs #include <iostream> using namespace std;
int main() { unsigned number, i, product; cout << "Calculate the factorial of? "; cin >> number; product = 1; while(i <= number) { product = product * i; i = i + 1; } cout << "The factorial of " << number << \ " is " << product << "\n"; return 0;

31 Real Activity: Factorials
#include <iostream> using namespace std; int main() { unsigned number, i, product; cout << "Calculate the factorial of? "; cin >> number; product = 1; i = 1; while(i <= number) { product = product * i; i = i + 1; } cout << "The factorial of " << number << \ " is " << product << "\n"; return 0;

32 Closing Matter Questions? Tasks (including extra credit)


Download ppt "Lab 4: Loops and Iteration Graham Northup"

Similar presentations


Ads by Google