Looping III (do … while statement)

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

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
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.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
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.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
C++ Loop Statements Repetition Revisited. Problem Using OCD, design and implement a function that, given a menu, its first valid choice, and its last.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
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.
Chapter 5 Repetition and Loop Statements Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville,
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
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.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
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.
Fundamental Programming Fundamental Programming for Loops.
1 More Control Structures if and switch (Chap. 8) do-while and forever loops (Chap. 9)
Introduction to Loops Iteration Repetition Counting Loops Also known as.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Computer Programming -1-
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.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Repetitive Structures
Topic 4: Looping Statements
Review 1.
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II (Repetition)
CS 1430: Programming in C++ No time to cover HiC.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
Chapter 8 Repetition Statements
Loops October 10, 2017.
Repetition Statements
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
M150: Data, Computing and Information
Control Structures Part 1
Let’s all Repeat Together
Alternate Version of STARTING OUT WITH C++ 4th Edition
A LESSON IN LOOPING What is a loop?
Objectives You should be able to describe: The while Statement
do/while Selection Structure
Fundamental Programming
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Looping III (do … while statement)

Outline do … while statement Review of looping break and continue in loops CSCE 106

Repetition Repetition or looping provides for the repeated execution of part of the algorithm. CSCE 106

do … while Statement The do - while statement is similar to the while loop, but the do - while loop has the test at the end. General syntax: do { statement; } while ( loop repetition condition ) ; Notice the iterative part executes before the loop-test Convenient when at least one repetition of the loop body is required CSCE 106

Data-validation Loop do prompt for and read a data item while data item is invalid E.g. { cout << “Enter number of employees: “; cin >> numEmp; } while (numEmp <= 0); do … while is very useful in validating input and only continue with the algorithm when correct range of input is provided. CSCE 106

Data-validation Loop (cont’d) do { cout << “Please enter a number between 0 and 255”; cin >> number; } while (number < 0 || number > 255); This loop keeps repeating until a number is input between 0 and 255. CSCE 106

do … while and Menus do … while is also very useful to control a menu-driven program Displaying a list of choices for user to select from E.g. list of edit operations: D - Delete a substring F - Find a string I - Insert a string R - Replace a substring Q - Quit Enter D, F, I, R, or Q as your selection: Write the loop for the above menu in your notes. CSCE 106

Review of Looping C++ provides three statements for implementing loops Use for to implement counting loops Use do … while to implement loops that must execute at least once Use while to code other conditional loops Remember that you could nest all types of loop statements when necessary. CSCE 106

break and continue in Loops break and continue are two C++ reserved words. You have seen break before with the switch statement. It could be used in the body of a loop to give the same effect of jumping out of the body of the loop. continue reserved word is used to jump to the loop testing condition, and hence ignoring anything else in the rest of the loop body. Using break and continue in loops is considered to be bad programming practice. Please don’t use them. You are only requested to understand their effect. CSCE 106

break and continue in Loops (cont’d) int k = 40; while (k < 100) { k += 10; if (k == 70) continue; if (k == 80) break; cout << "k = " << k <<'\n'; } cout << "Finished looping" << endl; Output k = 50 k = 60 Finished looping This slide gives you an example of a while loop with break and continue reserved words. CSCE 106

Next lecture will be about Arrays CSCE 106