 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Repeating Actions While and For Loops
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
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.
1 Three C++ Looping Statements Chapter 7 CSIS 10A.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
CS101 Computer Programming I Chapter 4 Extra Examples.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Control Structures RepetitionorIterationorLooping Part I.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
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.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Fundamental Programming Fundamental Programming More on Repetition.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Computer Programming -1-
REPETITION CONTROL STRUCTURE
CS161 Introduction to Computer Science
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Lecture 4B More Repetition Richard Gesick
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Control Structure Senior Lecturer
3 Control Statements:.
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Alternate Version of STARTING OUT WITH C++ 4th Edition
do/while Selection Structure
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types  Reading (for Mon): 2.3, 2.5, b  Exercises: p. 50 #15, 16  New files/handouts: WhileEx1.cpp, WhileEx2.cpp, NestedLoop.cpp

 Wednesday, 9/18/02, Slide #2 Repetition (Looping) Structures  Provides the ability to repeat a list of statements (an "action") more than once  Three looping structures in C++  while structure  for structure  do-while structure

 Wednesday, 9/18/02, Slide #3 Looping tasks With loops you can:  Repeat an action a specific number of times  Play a game 5 times  Repeat until some condition is met  Play game until user says quit  Play game until user loses 3 times  Accumulate a total  Add up a grocery bill and compute tax  Compute the average of a set of marks  Count how many occurrences of a letter there are in a given passage

 Wednesday, 9/18/02, Slide #4 The while structure  The syntax of the while structure is:  The statements of the loop (the “body” of the loop) repeat while the T-F condition remains true -- possibly forever!  How do we change the true/false value of the condition?? while (T-F condition) or while (T-F condition) single statement;{ list of statements }

 Wednesday, 9/18/02, Slide #5 Initialize, Update, Exit!  Initialize variables in loop condition before entering loop;  Update these variables inside the loop, so it can eventually terminate;  Check that the loop condition can be made false inside loop, so that loop will end. If you accidentally execute an infinite loop in VCPP, press Ctrl-Break to stop it. Statement to initialize loop condition variables while (condition) { Loop body contains statements to update loop condition variables }

 Wednesday, 9/18/02, Slide #6Examples  Write a program that inputs a number from the user, representing a restaurant bill, that adds 7% tax, then 15% tip to that, and prints the total. The process should repeat until the user enters a number <= 0.  Write a program that writes “TGIF” ten times, using only one output statement

 Wednesday, 9/18/02, Slide #7 First while-loop example  Write a program that inputs a number from the user, representing a restaurant bill, that adds 7% tax, then 15% tip to that, and prints the total. The process should repeat until the user enters a number <= 0.  See WhileEx1.cpp for full program float tax = 0.07; float tip = 0.15; float amount; cin >> amount; //while positive amount, compute tax and tip while (amount > 0) { float taxtotal = amount * (1 + tax); float total = taxtotal * (1 + tip); cout << total << endl ; cin >> amount; } Initialize loop condition objects before loop! Update loop condition objects inside loop!

 Wednesday, 9/18/02, Slide #8 Sentinels and Counters  The object amount in the last program is called a sentinel, because its value signals when to end the loop.  If we have a loop that should repeat a fixed number of times, we can use a loop counter to control the loop.  The loop counter starts at 0  It is increased by 1 each time the loop executes  The loop ends when the counter reaches the desired number of executions

 Wednesday, 9/18/02, Slide #9 Second While Loop Example  Write a program that writes “TGIF” ten times, using only one output statement  See WhileEx2.cpp for complete program.  Off-by-one errors: A common error is to write loops that execute one too many or one too few times – always check!! float numTimes = 10; float count = 0; //count = # loop executions while (count < numTimes) { cout << "TGIF" << endl; count = count + 1; }

 Wednesday, 9/18/02, Slide #10 Accumulating values  The statement count = count + 1; inside the loop, causes the object count to increase by 1 each time  We can accumulate other values too:  Write a program to input a list of positive numbers and compute their sum; 0 or negative number ends input  Modify to compute average of the numbers  Modify to count how many are over 50

 Wednesday, 9/18/02, Slide #11 “Nested Loops”: An example  Assume that the objects numRows and numCols have the values 3 and 5, resp. Trace the following code segment by hand, showing values in memory and on monitor.  See NestedLoop.cpp for complete program float row = 0; while (row < numRows) { float col = 0; while (col < numCols) { cout << '*'; col = col + 1; } cout << endl; row = row + 1; }

 Wednesday, 9/18/02, Slide #12 Nested Loops  If we want to repeat a process that uses a loop, then we put one loop inside another as part of its action.  For each iteration of the outer loop, the inner loop must be completely executed.  Example: Modify the previous program to ask the user if she/he would like to draw another rectangle.

 Wednesday, 9/18/02, Slide #13 Choosing the right data type: int, float, and double  In many of the previous programs, the float objects we used were always whole numbers.  C++ provides different data types for whole numbers and numbers with fractional parts:  int: An object of type int may only be a whole number  float: An object of type float may have a fractional part  double: Similar to float, but with larger number of decimal places, and larger magnitude permitted  Look at programs we did today. Which objects should be type int, and which should be float or double?

 Wednesday, 9/18/02, Slide #14 More on int, float, and double  C++ constants: If we use a numeric constant value in C++ that is a whole number, it is automatically given type int. If it has a fractional part, it is given type double.  Integer arithmetic: Any arithmetic operation involving only int type objects produces an int type object. For example, what is  5 / / 5