While Loops.

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.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
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.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Chapter 5: Control Structures II (Repetition)
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
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 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
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.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 Three C++ Looping Statements Chapter 7 CSIS 10A.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Control Structures Repetition or Iteration or Looping Part II.
Control Structures RepetitionorIterationorLooping Part I.
Overview Go over parts of quiz? Another iteration structure for loop.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Control Structures RepetitionorIterationorLooping Part I.
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 Chapter 5: Looping.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
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.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Control Structures Repetition or Iteration or Looping Part II.
Chapter 4 Repetition Structures
Introduction to Computer Programming
Topic 4: Looping Statements
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Unit 3 Lesson 9 Repetition Statements (Loops)
Chapter 5: Control Structures II (Repetition)
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Control Structures II (Repetition)
Chapter 5: Loops and Files.
Chapter 2.2 Control Structures (Iteration)
Repetition Structures (Loops)
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Control Structures - Repetition
Iteration with While You can say that again.
For & do/while Loops.
TOPIC 4: REPETITION CONTROL STRUCTURE
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Counting Loops.
Chapter 2.2 Control Structures (Iteration)
Control Structures Part 1
Control Structures Repetition or Iteration Looping Part I.
Chapter 5: Control Structures II (Repetition)
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Chapter 4 Repetition Structures
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

while Loops

While Loop Purpose: to repeat a set of 0 or more statements 0 or more times as long as some condition is true.

while ( conditionalExpression ) oneStatement While Loop Syntax: while ( conditionalExpression ) oneStatement where oneStatement is exactly one statement with a semicolon, or a block

While Loop Semantics: At the beginning: conditionalExpression is evaluated and - If true: oneStatement is executed, then control returns to the beginning. - If false: oneStatement skipped.

While Loop Style #1: one statement while (conditional) Exactly one statement; // indent the 1 statement Style #2: block { // line up with while statements // indent statements } // line up with while

While Loop Quick Vocabulary Body (of the loop): the one statement or block Iteration: one execution of the loop (one time through, one “lap”)

While Loop Example 1: only syntax counts! int a = 1; while (a <= 3) Prints: a++; 4 cout << a << endl;

While Loop Example 2: careful of where begins/ends int a = 1; while (a <= 3) { Prints: a++; 2 cout << a << endl; 3 } 4

While Loop Example 3: 0 iterations possible int a = 12; while (a <= 3) { Prints: a++; Done! cout << a << endl; } cout << ”Done!\n”;

While Loop Strategies Counter-Controlled Loop int i = start; while (i <= stop) { // do something some number of times i += increment; // i = i + increment; }

While Loop Strategies Counter-Controlled Loop Example: start at 1, stop at 20, count by 2's int i = 1; while (i <= 20) { cout << i << ” ”; i += 2; } // prints: 1 3 5 7 9 11 13 15 17 19

While Loop Strategies User Input Validation // answer could be float/string/etc. int answer = wrong_answer; while (answer is wrong) { cout << ”Enter …”; cin >> answer; }

While Loop Strategies User Input Validation int answer = 0; while (answer < 1 || answer > 10) { cout << ”Enter integer 1 to 10: ”; cin >> answer; }

While Loop Strategies User Input Validation string answer = ””; while (answer !=”left” && answer!=”right”) { cout << ”Enter left or right: ”; cin >> answer; }

While Loop Strategies User Input Validation with error message // ask first time before the loop string answer; cout << ”Enter left or right: ”; cin >> answer; while (answer !=”left” && answer!=”right”) { cout << “Error! Type left or right!!\n”; }

While Loop Strategies User Input Validation with error message // use if to see if we need an error message string answer = ””; while (answer !=”left” && answer!=”right”) { cout << ”Enter left or right: ”; cin >> answer; if (answer !=”left” && answer!=”right”) cout << ”Invalid answer!\n”; }

While Loop Strategies Summation of entered Values int num, sum = 0; while (whatever_control) { // any type of loop cout << ”Enter number: ”; cin >> num; sum += num; // sum = sum + num }

While Loop Strategies Counting Strategy int num, count = 0; while (whatever_control) { cout << ”Enter number: ”; cin >> num; count++; // count = count + 1 } cout << count << " numbers entered";

While Loop Strategies Average of Inputs: Count + Sum float num, sum = 0, average; int count = 0; while (whatever_control) { cout << ”Enter number: ”; cin >> num; count++; // count = count + 1 sum += num; // sum = sum + num } average = sum / count;

While Loop Strategies Mini - find the lowest value int min = ridiculously High number; while (whatever_control) { cout << ”Enter number: ”; cin >> num; if (num < min) min = num; }

While Loop Strategies Max - find the highest value int max = ridiculously Low number; while (whatever_control) { cout << ”Enter number: ”; cin >> num; if (num > max) max = num; }

Sentinel Controlled Loop Motivation/Purpose: - programs must often input data serially (one at a time). - the Number of Inputs may not be known: when the program is written when the program is executing and is ready to input the first data value How many data items will there be???

Sentinel Controlled Loop Definition of a Sentinel Value: a value entered as data, but is not processed as data; instead, it signals the end of data. Characteristics: choosing a Sentinel Value - must be the same Data Type as the data - should not "make sense" as data

Sentinel Controlled Loop General Pattern: Input data while (data != sentinel) { process data } Note the check for the sentinel is done immediately after a value is inputted.

Sentinel Controlled Loop Example: Enter an unknown number of test scores and calculate the sum. Use -1 as the Sentinel. Note: the value -1 - is the same type (int) as a test score - "doesn't make sense" as a test score

Sentinel Controlled Loop int score, sum=0; cout << "Enter score, -1 to quit: "; cin >> score; while (score != -1) { sum += score; } cout << "The sum is " << sum << endl;

Sentinel Controlled Loop Origin of the word Sentinel: - a guard on the "lookout" for something (usually the "bad guys" or other mischief) The while() is "on the lookout" for a particular value, and reacts (stops) when it sees the value. The value is not treated as "ordinary".

Vocabulary Term Definition Body the one statement or block of statements repeated by a loop. Iteration one execution of a loop (lap). Sentinel a value entered as data, but is not processed as data; instead, it signals the end of data.