while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Similar documents
Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Chapter 5: Loops and Files

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Repetition Structures

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Looping. Arizona State University 1

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

REPETITION CONTROL STRUCTURE LOGO

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Name Section: M/W T/TH Number Definition Matching (8 Points)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Loops and Files. of do-while loop

LAB 4.1 Relational Operators and the if Statement

Lecture 7 Tao Wang 1

Name Section: M/W T/TH Number Definition Matching (6 Points)

Computer Programming

Why Is Repetition Needed?

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Chapter 5: Control Structures II (Repetition)

Repetition and Loop Statements Chapter 5

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Increment and the While. Class 15

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Lecture 10. Daily Puzzle

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

All about flow control

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

To become familiar with array manipulation, searching, and sorting.

INTRODUCTION TO COMPUTER SCIENCE - LAB

C++ Programming: From Problem Analysis to Program Design, Third Edition

CSCE 206: Structured Programming in C++

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

Chapter 4: Control structures. Repetition

204111: Computer and Programming

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

CPE Summer 2015 Exam I (150 pts) June 18, 2015

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Chapter 4: Control structures

C++ Final Exam 2017/2018

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

LECTURE 04 MAKING DECISIONS

Score score < score < score < 65 Score < 50

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

5. Selection: If and Switch Controls

Chapter 4: Making Decisions

Unit 7. 'while' Loops

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Computer Programming

Chapter 4: Making Decisions

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

FORM 2 (Please put your name and form # on the scantron!!!!)

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

Quiz Determine the output of the following program:

Name SECTION: 12:45 2:20. True or False (12 Points)

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. A single variable can only hold one value. Declared using [] operator:

Control Structures of C++ Programming (2)

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Chapter 3. More Flow of Control

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

Repetition CSC 121 Fall 2014 Howard Rosenthal

Chapter 6 Topics. While Statement Syntax Count-Controlled Loops Event-Controlled Loops Using the End-of-File Condition to Control Input Data

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Dept. of CSE, IIT KGP

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

Information Science 1

Lecture 7: General Loops (Chapter 7)

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

STUDENT LESSON A12 Iterations

Week 3: File I/O and Formatting 3.7 Formatting Output

Loops (while and for)

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Transcription:

Week 7: Advanced Loops while Loops in C++ (review) while (expression) may be a compound (a block: {s) Gaddis: 5.7-12 CS 1428 Fall 2015 Jill Seaman 1 for if expression is true, is executed, repeat equivalent to: do while for (expr1; expr2; expr3) do while (expression); expr1; while (expr2) { expr3; is executed. if expression is true, then repeat 2 Q1! Counting! Summing Common tasks solved using loops! Calculating an average (the mean value)! Read input until sentinel value is encountered! Read input from a file until the end of the file is encountered 3 Counting (review)! set a counter variable to 0! increment it inside the loop (each iteration)! after each iteration of the loop, it stores the # of loop iterations so far int count = 0; cout << Enter a number between 1 and 10: ; cin >> number; while (number < 1 number > 10) { count = count + 1; cout << Please enter a number between 1 and 10: ; cin >> number; cout << count << invalid numbers entered << endl; // Do something with number here 4

5.7 Keeping a running total (summing)! After each iteration of the loop, it stores the sum of the numbers added so far (running total)! set an accumulator variable to 0! add the next number to it inside the loop int days; //Count for count-controlled loop float total = 0.0; //Accumulator float miles; //daily miles ridden cout << How many days did you ride your bike? ; cin >> days; for (int i = 1; i <= days; i++) { cout << Enter the miles for day << i << : ; cin >> miles; total = total + miles; total is 0 first time through cout << Total miles ridden: << total << endl; 5 Output: Keeping a running total How many days did you ride you bike? 3 Enter the miles for day 1: 14.2 Enter the miles for day 2: 25.4 Enter the miles for day 3: 12.2 Total miles ridden: 51.8 How would you calculate the average mileage? 6 Q2 5.8 Sentinel controlled loop Sentinel example! sentinel: special value in a list of values that indicates the end of the data! sentinel value must not be a valid value! -99 for a test score, -1 for miles ridden! User does not need to count how many values will be entered! Requires a priming read before the loop starts so the sentinel is NOT included in the sum the loop can be skipped (if first value is the sentinel) 7 Example: float total = 0.0; //Accumulator float miles; //daily miles ridden cout << Enter the miles you rode on your bike each day, ; cout << then enter -1 when finished. << endl; cin >> miles; //priming read while (miles!= -1) { total = total + miles; //skipped when miles==-1 cin >> miles; //get the next one cout << Total miles ridden: << total << endl; Output: Enter the miles you rode on your bike each day, then enter -1 when finished. 14.2 25.4 12.2-1 8 Total miles ridden: 51.8

5.9 Which Loop to use?! Any loop can work for any given problem! while loop: test at start of loop, good for: validating input, sentinel controlled loops, etc.! for loop: initialize/test/update, good for: count-controlled loops! do-while loop always do at least once, good for: repeating on user request, simple menu processing 9 5.10 Nested loops! When one loop appears in the body of another! For every iteration of the outer loop, we do all the iterations of the inner loop! Example from real life :! A clock. For each hour in a day (24), we iterate over 60 minutes. 12:00 1:00 2:00 3:00 12:01 1:01 2:01. 12:02 1:02 2:02........... 12:59 1:59 2:59. 10 Print a bar graph Input numbers from a file. For each number, output that many asterisks (*) in a row. inputfile >> number; //priming read while (number!=-1) { for (int i = 1; i <= number; i++) cout << * ; cout << endl; inputfile >> number; numbers.txt: 8 3 6 10-1 Output: ******** *** ****** ********** 11 Calculate grades for a class For each student, input the test scores from the user and output the average. int numstudents, numtests; cout << How many students? ; cin >> numstudents; cout << How many test scores? ; cin >> numtests; for (int student=1; student <= numstudents; student++) { float total = 0, score; cout << Enter the << numtests << test scores for student << student << endl; for (int test=1; test <= numtests; test++) { cin >> score; total = total + score; Inner loop float avgscore = total/numtests; cout << Average for student << student Outer loop << is: << avgscore << endl; 12

Output: Calculate grades for a class How many students? 3 How many test scores? 4 Enter the 4 test scores for student 1 88 90.5 92 77.5 Average for student1 is: 87.0 Enter the 4 test scores for student 2 66.5 70.5 80 86 Average for student2 is: 75.8 Enter the 4 test scores for student 3 99 93.5 80 79 Average for student3 is: 87.9 5.11 More File I/O Can test a file stream variable as if it were a boolean variable to check for various errors. After opening a file, if the open operation failed, the value of file stream variable is false. ifstream infile; infile.open("test.txt"); if (!infile) { cout << "File open failure!"; return 1; //abort program! 13 Q3 Note: after ANY input operation, if it fails, the value of file stream variable will then be false. 14 Reading data from a file Using >> to detect end of file Use fin>>x; in a loop Problem: when to stop the loop? First entry in file could be count of number of items problems: maintenance (must update it whenever data is modified), large files (might be hard to count) Could use sentinel value problem: may not be one (someone might delete it), maintenance Want to automatically detect end of file 15! stream extraction operation (>>) returns true when a value was successfully read, false otherwise int num; bool foundvalue = (inputfile >> num);! inputfile >> num: tries to read a value into num if it was successful, result is true (foundvalue is true) if it failed (non-number char or no more input), result is false (foundvalue is false, but the value in num does not change!) 16

Example: Using the result of >> bool foundvalue = (inputfile >> number); if (foundvalue) cout << The data read in was: << number << endl; else cout << Could not read data from file. << endl; Can also use directly as relational expression: if (inputfile >> number)... 17 Code: Sum all the values in the file without using a count or sentinel value int total = 0; while (inputfile >> number) { total = total + number; cout << The sum of the numbers in the file: << total << endl; numbers.txt: 84 32 99 77 52 Output: puts the priming read directly in the test expression The sum of the numbers in the file: 344 18 5.12 Breaking and Continuing Sometimes we want to abort (exit) a loop before it has completed. The break can be used to terminate the loop from within: cout << Guess a number between 1 and 10 << endl; while (true) { cin >> number; if (number == 8) break; cout << You got it. << endl; Don t do this. It makes your code hard to read and debug. 19 Stopping a single iteration Sometimes we want to abort an iteration (skip to the end of loop body) before it is done. The continue can be used to terminate the current iteration: for (int i=1; i <= 6; i++) { if (i == 4) continue; cout << i << ; Output: 1 2 3 5 6 Don t do this either. It makes your code hard to read and debug. 20 Q4

Programming Assignment 4.5 Practice only, don t submit Rewrite PA3, Prepare a Lab Report, so that it uses a loop to enter the data for any number of rats (ask the user to specify the number of rats before the loop starts). Then rewrite it to take the input from a file (do not input the number of rats, just loop until the end of the file). Rewrite PA4, Calculate a Cell Phone Bill, to ask the user if they want to repeat the program after the bill and savings are output. Also put the input validation in a loop. 21