Overview Go over parts of quiz? Another iteration structure for loop.

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.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
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.
CS150 Introduction to Computer Science 1
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Control Structures II (Repetition)
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Objectives You should be able to describe:
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
Chapter 5: Control Structures II (Repetition)
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
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.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Control Structures Repetition or Iteration or Looping Part II.
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.
Control Structures RepetitionorIterationorLooping Part I.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
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++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
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.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Controlling Behavior The if and for Statements. Function Behavior The behavior of a function is determined by the statements within the function. Statements.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Chapter 6. Loops A control structure that causes a statement or group of statements to be executed repeatedly There are 3 types of loops –while –for –do.
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)
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.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
REPETITION CONTROL STRUCTURE
CS161 Introduction to Computer Science
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Control Statements Kingdom of Saudi Arabia
Programming Fundamentals
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 6: Repetition Statements
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Alternate Version of STARTING OUT WITH C++ 4th Edition
2.6 The if/else Selection Structure
Objectives You should be able to describe: The while Statement
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

Overview Go over parts of quiz? Another iteration structure for loop

for loop for (expression1; expression2; expression3) statement; Step 1: Evaluate expression1 Step 2: Evaluate expression2. If true, execute statement If false, continue execution at next statement following the for loop. Step3: Evaluate expression3 Step 4: Repeat Step 2 and 3 Example: Display the numbers from 1 to 10 for (int i = 1; i <= 10; i++) cout << i << endl;

for loop with compound statement for (expression1; expression2; expression3) { statement1; statement2; } Example: Input 10 numbers and calculate the total int i, number, total = 0; for (i = 1; i <= 10; i++) { cout << “\nEnter next number: “; cin >> number; total = total + number; } cout << “The total of the numbers entered is “ << total << endl;

Exercises Use a for loop structure to solve the following problems: 1)Display the numbers from 7 to 77 in steps of 7 2)Display the numbers from 20 to 2 in steps of –2 3)Display the following numbers 2,5,8,11,14,17,20 4)Display the following numbers 99,88,77,66,55, 44,33,22,11,0

control variable for the for loop for (i = 1; i <= 10; i++) cout << i << endl; i is the control variable for the for loop in this example Initialize control variable Final value of control variable Update control variable

Declaration of loop control variable inside for structure for (int i = 1; i <= 10; i++) cout << i << endl; instead of int i; for (i = 1; i <= 10; i++) cout << i << endl;

for loops for (expression1; expression2; expression3) statement; is equivalent to: expression1; //Initialize loop control variable while (expression2) { //Test loop control variable statement; expression3; // Update loop control variable }

Example: for loop vs while loop Display the numbers from 1 to 10 for (int i = 1; i <= 10; i++) cout << i << endl; OR int i = 1; while (i <= 10) { cout << i << endl; i++; }

Example: change while loop to a for loop int sum = 0; int count = -5; while (count <= 15) { sum = sum + count; count ++; } int sum = 0; for (int count = -5; count <= 15; count++) sum = sum + count;

When to use for loop Use the for loop for a counter controlled repetition Use the while loop for repetition when there is no counter. (i.e reading in input). for (int i = 1; i <= 10; i++) cout << i << endl; bool notDone = true; while (notDone) { cin << input; if (-1 == input) notDone = false; }

Omitting expression1 for (; expression2; expression3) statement; Can omit expression 1 if loop control variable is initialized some place else. Note ; place holder Example: Display the numbers from 1 to 10 int i = 1; for (; i <= 10; i++) cout << i << endl;

Omitting expression2 for (expression1; ; expression3) statement; When expression2 is omitted, C++ assumes the condition is always true, thereby creating an infinite loop Example: Display the numbers from 1 to infinity and beyond for (int i = 1; ; i++) cout << i << endl;

Omitting expression3 for (expression1; expression2;) statement; When expression3 is omitted, this loop also becomes an infinite loop unless the loop control variable is updated inside the loop Example: Display the numbers from 1 to infinity and beyond for (int i = 1; i <= 10;) cout << i << endl;

comma separated lists of expressions Expression1 and Expression3 may be a comma separated list of expressions. For example: int inputData; for (int i = 0, sum = 0; (i < 10) && (sum < 1000); i++) { cin >> inputData; sum = sum + inputData; } Note: Put only expressions involving the control variables in the initialization and increment expressions of the for structure.

common programming error Changing the loop control variable inside the body of the loop is allowed but dangerous. for (int i = 0; i < 10; i++) { cin >> i; }

common programming error Problem: Vary the control variable over the following sequence of values 2, 5, 8, 11, 14, 17, 20 Solution1: for (int i = 2; i <= 20; i +=3) Solution2: for (int i = 2; i < 22; i +=3) Solution2 more confusing. More likely to lead to boundary condition problems.

Exercises 1)Write a program that inputs 10 grades, determines the highest grade and displays the result. Use a for loop. 2)Write the same program as in problem number 1 using a while loop.