Lab 4: Loops and Iteration Graham Northup

Slides:



Advertisements
Similar presentations
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Advertisements

Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
CS0004: Introduction to Programming Repetition – Do Loops.
Computer Science 1620 Loops.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Chapter 5: Control Structures II (Repetition)
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 C++ Control Statements ~ Selection and Iteration.
Chapter 4 Program Control Statements
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
CPS120 Introduction to Computer Science Iteration (Looping)
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Week 6 - Monday.  What did we talk about last time?  while loop examples  Lab 5.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Computer Programming -1-
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
CS161 Introduction to Computer Science
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Repetition Structures (Loops)
Programming Fundamentals
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Arrays, For loop While loop Do while loop
Iteration with While You can say that again.
For & do/while Loops.
We’re moving on to more recap from other programming languages
Repetition Control Structure
Computing Fundamentals
Control Structures Part 1
Let’s all Repeat Together
2.6 The if/else Selection Structure
Repetition Statements (Loops) - 2
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Chapter 4 Repetition Structures
Presentation transcript:

Lab 4: Loops and Iteration Graham Northup

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

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)

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)

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.

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

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

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

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

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)

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)

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)

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)

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.

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 :)

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)

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;

Uhm... Oops.

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.

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

Starting the Debugger

Running in the Debugger

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

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.

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

Continuing Conditionally Just executed PC

Continuing Conditionally Uhh... Just executed PC

Continuing Unconditionally

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

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;

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;

Closing Matter Questions? Tasks (including extra credit)