Fundamental Programming 310201 Fundamental Programming More on Repetition.

Slides:



Advertisements
Similar presentations
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Advertisements

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.
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.
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.
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.
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.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
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.
 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.
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.
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
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.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Fundamental Programming Fundamental Programming for Loops.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
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.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
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.
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.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
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.
Introduction to Computer Programming
REPETITION CONTROL STRUCTURE
CS161 Introduction to Computer Science
Chapter 5: Loops and Files.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Programming Fundamentals
Looping and Repetition
Loops October 10, 2017.
MSIS 655 Advanced Business Applications Programming
Repetition Control Structure
do/while Selection Structure
Fundamental Programming
Repetition Statements (Loops) - 2
Presentation transcript:

Fundamental Programming Fundamental Programming More on Repetition

Fundamental Programming Repetition statements we have been using one C++ repetition statement – while repetition statements allow us to perform a task more than once – the number of times depends on the input data in this class, we introduce another repetition statement – the do-while statement

Fundamental Programming while – a Review while ( ) { } NbrTimesTold = 0; while (NbrTimesTold < NbrTimesToTell) { cout << “ No new taxes!“ << endl; NbrTimesTold = NbrTimesTold + 1; }

Fundamental Programming while – a Review common looping errors are:  loops that fail to stop (continue forever)  loops that stop one repetition too early  loops that perform one repetition too many  loops that fail to start normal while-loop structure is…

Fundamental Programming while – a Review while ( ) { } NbrTimesTold = 0; while (NbrTimesTold < NbrTimesToTell) { cout << “ No new taxes!“ << endl; NbrTimesTold = NbrTimesTold + 1; }

Fundamental Programming Activity Number = 5; while (Number > 0) { Sum = Sum + Number ; Number = Number - 1 ; } cout << “The sum is “ << Sum << endl; what will the following code output?

Fundamental Programming Activity Break

Fundamental Programming Activity Feedback Number = 5; while (Number > 0) { Sum = Sum + Number ; Number = Number - 1 ; } cout << “The sum is “ << Sum << endl; output depends on initial value of Sum if Sum is zero at start: “The sum is 15” must always initialise a sum to zero!

Fundamental Programming Activity Sum = 0 ; while (Number > 0) { Sum = Sum + Number ; Number = Number - 1; } cout << “The sum is “ << Sum << endl; what will the following code output?

Fundamental Programming Activity Break

Fundamental Programming Activity Feedback output depends on initial value of Number must initialise variables in while condition! Sum = 0 ; while (Number > 0) { Sum = Sum + Number ; Number = Number - 1; } cout << “The sum is “ << Sum << endl;

Fundamental Programming More C++ operators C++ has operators to increment or decrement loop control variables Number = Number - 1; NbrTimesTold = NbrTimesTold + 1; can be replaced by: Number-- ; NbrTimesTold++ ;

Fundamental Programming Activity Sum = 0 ; Number = 5; while (Number > 0) { Sum = Sum + Number ; Number++; } cout << “The sum is “ << Sum << endl; What will the following code output?

Fundamental Programming Activity Break

Fundamental Programming Activity Feedback loop will never end – an infinite loop this is a logic error! Always check loop conditions carefully. Sum = 0 ; Number = 5; while (Number > 0) { Sum = Sum + Number ; Number++; } cout << “The sum is “ << Sum << endl;

Fundamental Programming The while Statement while is the only repetition statement you will every need however, most programming languages include at least two other repetition statements we introduce one of these statements in this class, the other one will be covered later

Fundamental Programming while vs do-while the while statement tests a condition at the start of the loop the do-while statement tests a condition at the end of the loop

Fundamental Programming while vs do-while the while statement tests a condition at the start of the loop cout “; cin >> Char; while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) { cout << “Invalid direction!” << endl; cout “; cin >> Char; }

Fundamental Programming do-while loops if a task must be performed at least once, we can perform the test at the end of the loop using do-while do { cout “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; } while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’));

Fundamental Programming Activity what are advantages and disadvantages of this design (compared to using while)? do { cout “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; } while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)); can you suggest an improvement?

Fundamental Programming Activity Break

Fundamental Programming Activity Feedback one advantage of do-while is that there is only one copy of prompt and input lines cout “; cin >> Char; while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) { cout << “Invalid direction!” << endl; cout “; cin >> Char; }

Fundamental Programming Activity Feedback one advantage of do-while is that there is only one copy of prompt and input lines do { cout “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; } while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’));

Fundamental Programming Activity Feedback one disadvantage of do-while is that the loop condition appears twice do { cout “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; } while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’));

Fundamental Programming Activity Feedback one disadvantage of do-while is that the loop condition appears twice cout “; cin >> Char; while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) { cout << “Invalid direction!” << endl; cout “; cin >> Char; }

Fundamental Programming Activity Feedback repetition of complex loop conditions can be avoided using a Boolean variable… WaitingForDirection = true; do { cout “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; else WaitingForDirection = false; } while ( WaitingForDirection );

Fundamental Programming Activity is the following logic OK? Sum = 0 ; do { cout "; cin >> Number; Sum = Sum + Number; } while (Number != -9); cout << “Sum = “ << Sum; if not, fix it.

Fundamental Programming Activity Break

Fundamental Programming Activity Feedback the problem with the logic is that it will include –9 in the sum – it should be: Sum = 0 ; do { cout "; cin >> Number; if (Number != -9) Sum = Sum + Number; } while (Number != -9); cout << “Sum = “ << Sum; note: you will often see loop conditions repeated in do-while statements

Fundamental Programming Activity Number = 5 ; Sum = 0 ; do { Sum += Number ; Number-- ; } while (Number > 0) ; Code the following as a while loop:

Fundamental Programming Activity Break

Fundamental Programming Activity Feedback Number = 5 ; Sum = 0 ; while (Number > 0) { Sum += Number ; Number-- ; } note: loop condition remains the same... loop condition controls value of variable during the final trip through the loop – so, it should be the same

Fundamental Programming while vs do-while difference between while and do-while is that while allows for the possibility that the task in the loop is performed 0 times with do-while, the task in the loop is performed at least once hence, while is more flexible - that’s why we started with it