Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping."— Presentation transcript:

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

2 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

3 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).

4 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.

5 Chapter 5 slide 5 5.1 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;

6 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

7 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 ??

8 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 __

9 Chapter 5 slide 9 Notes on Increment, Decrement Can be used in expressions: result = num1++ + --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

10 Chapter 5 slide 10 5.2 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 { }

11 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?

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

13 Chapter 5 slide 13 The Logic of a while Loop

14 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

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

16 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

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

18 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++; }

19 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.

20 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

21 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; }

22 Chapter 5 slide 22 Data Flow diagram

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

24 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; }

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

26 Chapter 5 slide 26 5.3 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

27 Chapter 5 slide 27

28 Chapter 5 slide 28

29 Chapter 5 slide 29 5.4 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

30 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++; }

31 Chapter 5 slide 31 5.5 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;

32 Chapter 5 slide 32 5.6 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

33 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

34 Chapter 5 slide 34 (Program Continues)

35 Chapter 5 slide 35


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

Similar presentations


Ads by Google