Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
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.
Chapter 5: Loops and Files.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Chapter 5: Control Structures II (Repetition)
CONTROLLING PROGRAM FLOW
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
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.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
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.
Overview Go over parts of quiz? Another iteration structure for loop.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
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.
Loops and Files. 5.1 The Increment and Decrement Operators.
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.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Fundamental Programming Fundamental Programming More on Repetition.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
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.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
While ( number
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.
Topic 4: Looping Statements
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II (Repetition)
Control Structures II (Repetition)
Chapter 2.2 Control Structures (Iteration)
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
MSIS 655 Advanced Business Applications Programming
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Outline Altering flow of control Boolean expressions
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Chapter 6: Repetition Statements
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:

Chapter 6 - Repetition

while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass loop body if test false Lesson 6.1

General Structure while (expression) { statement1 statement2 … } Logical expression (variable or arithmetic expression) Boolean results (True or False) Lesson 6.1

Steps In Using a while Loop 1. Initialize variable acting as test expression 2. Evaluate the boolean test 3. If true execute statements within loop 4. Update the variable 5. Go back to step 2 Note: if no change to variable then infinite loop (never ends) is created Lesson 4.1

Example while Loop int i= 0, number = 1; while (number) { cout << “Please type a number. ” << “Type 0 (zero) to stop execution.\n”; cin >> number; i++; if (i > 50) break; } Initialize variables Variable as expression value other than zero tests true Loop body Statements that provide exit from loop Lesson 6.2

Example while Loop int i= 0; while (i <= 5) { cout << “Loop number is “ << i; i++; } Initialize test expression variable Expression evaluated, when true, statements in loop executed Statements within loop Changing variable to provide exit from loop Lesson 6.2

do-while Loop u Used when you want to execute loop at least once u Loops on test being true and exits when test is false Lesson 6.3

General Syntax do { statement1; statement2; … } while (expression); Lesson 6.3

do while Loop Example u Bare bones u Does not alert user of problems do { cout << “\n enter an id number:”; cin >> id_num; } while ((id_num 1999)); Lesson 6.3

Better do while Loop do { cout << “\n Enter an id number:”; cin >> id_num; if ((id_num 1999)) { cout << “\n invalid number entered” << “\n please check and re-enter”; } else break; } while (1); Decision Statement – Chapter 5 Expression always true Exit loop if id number is valid Lesson 6.3

Recap while(s) u while –Most commonly used when repetition not counter controlled –Pre-tested –Loop body may not be executed u do-while –Convenient when at least one repetition needed –Post-tested Lesson 6.3

For Loop u Convenient for counter controlled loops u Pre-tested loop u Same behavior as while –Set lcv to initial value –Test lcv against terminating value –Update lcv to next value u All done in for header statement Lesson 6.4

General Syntax for (initialize lcv; test lcv; update lcv) { statement; } Lesson 6.4

for Loop (cont.) u Example: for (int i = 0; i < 10; i++) cout << i; u Initialization, testing and updating in same statement u Semicolons required u Braces optional if only one statement Lesson 6.4

Example: int finalValue; cin >> finalValue; for (int counter = 0; counter < finalValue; counter++) cout << "*"; Display Screen 3 finalValue 3 counter 0 True * 1 * 2 * 3 False Lesson 6.4

For Loop Examples float sum = 0; for (float x = 0.5; x < 20.1; x += 0.5) sum += sqrt (x); for (int count = 0; count < n; count++) cout << ch; Lesson 6.4

Comparison: int finalvalue; cin >> finalValue; for (int counter = 0; counter < finalValue; counter++) cout << "*"; counter = 0; cin >> finalValue; while (counter < finalValue) { cout << "*"; counter++;} int counter, finalValue;int finalvalue; cin >> finalValue; for(int counter = 0; counter < finalValue; counter ++) cout << "*"; Lesson 6.4

Debugging and Testing u Off by one errors –Check loops to ensure you are executing the correct number of times –x < 10 or x <= 10 u Check loop boundaries –Can create infinite loop u Does answer make sense Lesson 6.4

For Loop Modifications u break statement –Can use within loop to terminate early –Controversial u Multiple expressions for initialization and increment –Use comma separated list –for (I = 1, j = 2; I < 10; I++, j++) Lesson 6.4

Summary u Create while loops u Create do-while loops u Create for loops u Trace and debug loops u Use loops to solve problems Learned how to: Chapter 6