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.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
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?
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 5: Control Structures II (Repetition)
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 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Chapter 5: Control Structures II (Repetition)
1 Chapter 5 File Objects and Looping Statements (Some slides have been modified from their original format)
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Chapter 6 Looping.
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.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
CPS120 Introduction to Computer Science Iteration (Looping)
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
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 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.
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.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
Chapter 6 Looping. 2 Chapter 6 Topics l While Statement Syntax l Count-Controlled Loops l Event-Controlled Loops l Using the End-of-File Condition to.
1 Looping. 2 Chapter 6 Topics  While Statement Syntax  Phases of Loop Execution  Two Types of Loops: Count-Controlled Loops &Event-Controlled Loops.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Control Structures RepetitionorIterationorLooping Part I.
Overview Go over parts of quiz? Another iteration structure for loop.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
Looping ROBERT REVAES. Logical Operators  && AND  Both have to be true for it to evaluate to be true.  || OR  One or the other has to be true for.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
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.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Chapter 6 Looping. 2 l A loop is a repetition control structure. l it causes a single statement or block to be executed repeatedly What is a loop?
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
while Repetition Structure
Chapter 2.2 Control Structures (Iteration)
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Alternate Version of STARTING OUT WITH C++ 4th Edition
Repetition Statements (Loops) - 2
Presentation transcript:

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 while

The While Statement while (expression) Statement while (inputVal != 25) cin >> inputVal;

while and if Not the same construct If statements do not repeat While statements will repeat until the expression becomes false While statements should always have a statement that will eventually make the expression to become false

Phases of Loop Execution Loop entry – The point at which of the low of control reaches the first statement inside a loop. Iteration – An individual pass through or repetition of, the body of a loop. Loop test – The point at which the While expression is evaluated and a decision is made either to begin a new iteration or skip to the statement immediately following the loop. Loop exit – the point at which the repetition of the loop ends and control passes the first statement following the loop. Termination condition – The condition that causes the loop to be exited.

Loops using the While Statement Count-Controlled Loops Event-Controlled Loops –Sentinel-Controlled Loops –End-of-File Controlled Loops –Flag-Controlled Loops

Count Controlled Loops loopCount = 1; while (loopCount <= 10) { cout << “Hello!\n”; loopCount++;}

Sentinel-Controlled Loops while(!(month == 2&&day == 31)) { cin >> month >> day; …}

Sentinel-Controlled Loops cin >> month >> day; while(!(month == 2&&day == 31)) { cin >> month >> day; …}

Sentinel-Controlled Loops cin >> month >> day; while(!(month == 2&&day == 31)) { … cin >> month >> day; }

End-of-File Controlled Loop cin >> month >> day; while ( cin ) { … cin >> month >> day; }

End-of-File Controlled Loop cin >> month >> day; while ( !cin.eof() ) { … cin >> month >> day; }

Flag-Controlled Loop sum = 0; cin >> number; bool nonNegative = true; while ( nonNegative ) { if ( number > 0) sum = sum + number; else nonNegative = false; cin >> nonNegative; }

Quiz Write a code fragment that uses a loop to compute the sum of the squares of a series of numbers from 1 to a user entered number. Upon loop exit the sum of the squares should be printed.

Looping Subtasks CountingSumming Keeping track of the previous value

Counting Loop count = 0; cin.get( inChar ); while ( inChar != ‘.’ ) {count++; cin.get( inChar ); } Iteration Counter: A counter variable that is incremented with each iteration of a loop

Summing Loop sum = 0; cin >> number; bool nonNegative = true; while ( nonNegative ) { if ( number > 0) sum = sum + number; else nonNegative = false; cin >> nonNegative; }

Remembering Loop count = 0; in.get ( prevChar ); in.get ( currChar ); while ( !in.eof() ) { if ( currChar == ‘=‘ && prevChar == ‘!’ ) count++; prevChar = currChar; in.get( currChar ); }

Designing Loops What is the condition that ends the loop? How should the condition be initialized? How should the condition be updated? What is the process being repeated? How should the process be initialized? How should the process be updated? What is the state of the program on exiting the loop?