Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.

Slides:



Advertisements
Similar presentations
Chapter 04 (Part III) Control Statements: Part I.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Computer Science 1620 Loops.
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.
Starting Out with C++, 3 rd Edition 1 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/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
CS150 Introduction to Computer Science 1
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Loops and Files.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Control Structures Repetition or Iteration or Looping Part II.
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.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 5: Loops. Slide 5- 2 Outline Increment and Decrement while loop do-while loop for loop.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
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.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 5: Loops and Files.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Repetition Structures (Loops)
Programming Fundamentals
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 4B More Repetition Richard Gesick
Chapter 5: Looping Starting Out with C++ Early Objects Eighth Edition
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 6: Repetition Statements
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Let’s all Repeat Together
Alternate Version of STARTING OUT WITH C++ 4th Edition
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping

Chapter 5 slide 2 Lecture 10 Topics 1. Introduction to Loops and loops definitions: The while Loop 2. The Increment and Decrement Operators 3. Prefix and Postfix 3. Counters 4. Letting the User Control a Loop 5. Keeping a Running Total (accumulators) 6. Sentinels

Chapter 5 slide 3 Loops in general A loops is a fundamental for all programming languages. Enables the program to repeat statements. Repeating can go until a certain condition is met, for a predetermined number of times or forever (infinite loop).

Chapter 5 slide 4 Loops Control structures that cause a statement or a group of statements to repeat. What loops mean in C++ ?? Slide 10 First lets look at increment and decrement.

Chapter 5 slide The Increment and Decrement Operators ++ : add one to a variable val++; is the same as val = val + 1; -- : subtract one from a variable val--; is the same as val = val – 1; can be used before (prefix) or after (postfix) a variable: ++val; --val;

Chapter 5 slide 6 Prefix vs. Postfix ++ and -- operators can be used in complex statements and expressions prefix ( ++val, --val ): increment or decrement, then return the value of the variable postfix ( val++, val-- ): return the value of the variable, then increment or decrement …. See example next page

Chapter 5 slide 7 Prefix and Postfix Example #include using namespace std; int main() { int x = 100; cout<<++x<<" prefix case "; cout << x++ << " postfix case"; system("pause"); return 0; } What is the output ??

Chapter 5 slide 8 Prefix vs. Postfix - Examples int num, val = 12; cout << val++; // displays __, // val is now __; cout << ++val; // sets val to __, // then displays it num = --val; // sets val to __, // stores __ in num num = val--; // stores __ in num, // sets val __

Chapter 5 slide 9 Notes on Increment, Decrement Can be used in expressions: result = num num2; Must be applied to something that has a location in memory. Cannot have: result = (num1 + num2)++; Can be used in relational expressions: if (++num > limit) pre- and post-operations will cause different comparisons

Chapter 5 slide Introduction to Loops: The while Loop Loop: part of a program that may execute > 1 time (repeats). while loop: while (expression) statement; statement; can also be a block of statements enclosed in { }

Chapter 5 slide 11 While Loop Syntax // the condition is also referred to as an expression While (condition) { //statements to execute if condition is true } Example: int count = 0; // has to be declared and initialized first // While loop is a pretest loop while (count < 10) { cout<< “ hello world \n”; count++; // in this case count is also used as a counter } cout<< “Good bye “; What happens if we take the brackets off and why?

Chapter 5 slide 12 What is the output? n = 1; while (n <= 5) cout << n << ' '; n++;

Chapter 5 slide 13 The Logic of a while Loop

Chapter 5 slide 14 while Loop – How It Works? while (expression) statement; expression is evaluated –if true, then statement is executed, and expression is evaluated again –if false, then the the loop is finished and program statements following statement execute

Chapter 5 slide 15 while Loop Example int val = 5; while (val <= 8) cout << val++ < endl; produces output:

Chapter 5 slide 16 while Loop Notes no ; after (expression)same as if statement. while is a pretest loop – expression is evaluated before the loop executes loop must contain a code to make expression become false Infinite loop: loop that does not stop

Chapter 5 slide 17 Infinite loop int num =10; While (num >= 10); { cout << “ helloooooo”; num ++; }

Chapter 5 slide 18 while is a Pretest Loop expression is evaluated before the loop executes. The following loop will never execute!! int number = 6; while (number <= 5) { cout << "Hello\n"; number++; }

Chapter 5 slide 19 Using the while Loop for Input Validation Input validation is the process of inspecting data that is given to the program as input and determining whether it is valid. The while loop can be used to create input routines that reject invalid data, and repeat until valid data is entered.

Chapter 5 slide 20 Using the while Loop for Input Validation Here's the general approach, in pseudocode: Read an item of input. While the input is invalid Display an error message. Read the input again. End While

Chapter 5 slide 21 Input Validation Example cout << "Enter a number Greater than 10: "; cin >> number; while (number >= 10) { cout << "Invalid Entry!" << "Enter a number less than 10: "; cin >> number; }

Chapter 5 slide 22 Data Flow diagram

Chapter 5 slide 23 Input Validation Example from Program 5-4

Chapter 5 slide 24 check for input validation // keep repeating until a valid data is entered. cout << “ enter a number in the range of 1 to 100: “; cin >>number; while (number 100) { cout << “ ERROR…. Try again”; cin >> number; }

Chapter 5 slide 25 check for input validation to avoid infinite loop // Enter an input validation statement after your input statement. while (testscore != -1) { cout << "\n please enter a test score "; cout << "\n enter -1 to exit"; cin >> testscore; if (cin.fail() ) break; if(testscore > 0) { :

Chapter 5 slide Counters Counter: variable that is incremented or decremented each time a loop repeats Can be used to control execution of the loop (loop control variable) Must be initialized before entering loop May be incremented/decremented either inside the loop or in the loop test

Chapter 5 slide 27

Chapter 5 slide 28

Chapter 5 slide Letting the User Control a Loop Program can be written so that user input determines loop repetition Used when program processes a list of items, and user knows the number of items User is prompted before loop. The input is used to control number of repetitions

Chapter 5 slide 30 Letting the User Control a Loop - Example int num = 1, limit; cout << "Table of squares\n"; cout << "How high to go? "; cin >> limit; cout << "number square\n"; while (num <= limit) {cout << setw(5) << num << setw(6) << num*num << endl; num++; }

Chapter 5 slide Keeping a Running Total running total: accumulated sum of numbers from each repetition of loop accumulator: variable that holds running total int sum=0, num=1; // sum is the while (num <= 10) // accumulator { sum += num; num++; } cout << "Sum of numbers 1 – 10 is" << sum << endl;

Chapter 5 slide Sentinels sentinel: value in a list of values that indicates end of data Special value that cannot be confused with a valid value, e.g., -999 for a test score Used to terminate input when user may not know how many values will be entered

Chapter 5 slide 33 Sentinels sentinel: value in a list of values that indicates end of data Special value that cannot be confused with a valid value, e.g., -999 for a test score Used to terminate input when user may not know how many values will be entered

Chapter 5 slide 34 (Program Continues)

Chapter 5 slide 35