Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 1 For loop is a counter controlled loop. For loop is a pretest loop. Used when number.

Slides:



Advertisements
Similar presentations
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Advertisements

Computer Science 1620 Loops.
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.
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.
Chapter 5: Loops and Files.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
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)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
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.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
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.
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 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.
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.
Chapter 05 (Part III) Control Statements: Part II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
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.
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.
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.
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++ 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.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
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.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
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.
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.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Loop. 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input.
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.
REPETITION CONTROL STRUCTURE
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 2.2 Control Structures (Iteration)
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Additional Control Structures
Chapter 2.2 Control Structures (Iteration)
Computing Fundamentals
Alternate Version of STARTING OUT WITH C++ 4th Edition
2.6 The if/else Selection Structure
do/while Selection Structure
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

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 of iterations is know before we start the loop. For Loop SYNTAX for ( initialization ; test expression ; update ) { 0 or more statements to repeat }

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) { cout << num << “Potato” << endl; }

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Example 1: What output is produced? int count ; for ( count = 4 ; count > 0 ; count-- ) { cout << count << endl; } cout << " Done " << endl; For Loop - Examples

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Example 2: for (j = 1; j <= 3; j++) cout << "loop index is " << j << endl; Equivalent while loop j = 1; while (j <= 3) { cout << "loop index is "<< j << endl; j++; } For Loop – Examples …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Example 3: What is output? for (count = 10; count != 0; count--) cout << count << endl; For Loop – Examples …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For Loop – Examples … Example 4: What is output? cout << "\n\tNumber\tSquare\tCube " << endl; cout << "\t------\t------\t---- " << endl; for (int num = 1; num < 11; num++) cout << ' \t' << num << '\t' << num*num << '\t' << num*num*num << endl;

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Example 5: Rewrite example 4 using a while loop. cout << "\n\tNumber\tSquare\tCube " << endl; cout << "\t------\t------\t---- " << endl; for (int num = 1; num < 11; num++) cout << '\t' << num << '\t' << num*num << '\t' << num*num*num << endl; For Loop – Examples …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For Loop – Examples … Example 6: What is output? char letter; for(letter = 'A'; letter <= 'Z'; letter++) cout << letter;

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For Loop – Examples … Example 7: What is output? for (j = 10; j >= 30; j += 5) cout << j << " ";

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Example 8: Convert the following while statement to an equivalent for statement j = 1; sum = 0; while (j < =15) { sum += j; j += 2; } cout << sum << endl; For Loop – Examples …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Example 9: Trace for statement and show its output. for (j = 1; j < 10; j++) cout << setw(j) << '* ' <<endl; For Loop – Examples …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Example 10: In the String class, the length() function returns the number of characters in a String object. a) The following loop is designed to take a string and pad it on the right with *’s so that the length is expanded to 15. For example, Nancy becomes Nancy**********. Find the missing statement and loop test: cin >> str; ______________________ for (i=1; __________ ; i++) str += "*"; For Loop – Examples …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Example 10: b) Modify the loop in part a) to take a string and expand it on the left with *’s to a length of 15. cin >> str; For Loop – Examples …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP a) An “out of sync” header can result in the loop being skipped or in an infinite loop. // loop will be skipped for (j=1; j >=10; j++) cout << j << " "; // infinite loop for (j = 5; j > 0; j++) cout << j << " "; Cautions for For Loop

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP b) Don’t alter control variable within the loop body for (j = 1; j <= 10; j++) { cout << j << " "; j = j + 2;//DON’T DO THIS } Cautions for For Loop …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP c) Don’t put a semicolon after the for header, as any statement after the semicolon will be outside the loop. int count; for (count = 0; count < 10; count++) ; { cout << "*" ; } Cautions for For Loop …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP The initialization statement of a for loop may contain two or more statements separated by commas. - The update part of a loop may contain multiple expressions that allow the loop to modify more than one control object. Example 1: Output? for (j = 1, k = 7; j <= k; j++, k--) cout << j+ 2 * k << " "; cout << endl; Generalized For Loop

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Example 2: Give the output for each general for loop statement. a) for (sum=0, i=0, k=0 ; i<k; i++, k-- ) sum += 2 * i + k; Generalized For Loop …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Example 2: Give the output for each general for loop statement. b) for (i=0, j=1; i*j<100; i++, j*=10) cout << i*j << endl; Generalized For Loop …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Nested for loop is a loop within a loop Consists of an outer loop and an inner loop Example 1: What will be printed by the following code segments? for (n = 2; n <= 4; n++) { for (j = 6; j <= 7; j++) cout << n << ' ' << j << endl; cout << " j is now " << j <<endl; } cout << " n is now " << n << endl; Nested Loops

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Example 2: What will be printed by the following program segments? cin >> num_rows; for (r = 1; r <= num_rows; r++) { // indent by printing num_rows -r spaces for (i = 1; i <= num_rows -r; i++) cout << ' ' ; // print r asterisks for (i = 1; i <= r; i++) cout << ' * ' ; // drop cursor to a new line cout << endl; } // end outer for Nested Loops …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Want to set up the nested loops to produce the number triangle below: 1 // line number = // line number = // line number = // // line number = // // line number = 9 Designing Nested Loops

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Form:break Action: At any place in a loop body, a break statement immediately transfers program control to the first statement following the loop. When a break statement is used as part of an inner nested loop, program control exits the inner loop but not the outer loop. We have seen the use of the break statement in the switch statement The break Statement

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Example: The break statement is used in an exit test within an "infinite loop". The loop terminates on a response of 'Q'. while (true)// infinite while loop {... cin >> response;// request user input if (response == 'Q')// test for the QUIT response break; // break from the loop.... // continue the loop body } The break Statement …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Example: Using a break statement with nested loops to produce a multiplication table for(int x = 1; x x) break; else cout << setw(4) << x*y; cout << endl; } The break Statement …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Form:continue Action: The continue statement terminates execution of all remaining statements in the current iteration and passes program control to the next iteration. When continue is used in a while or do..while statement, the next code to execute is the loop test. The programmer is responsible to update control objects, as necessary, before executing continue. The continue statement

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Form:continue Action …: In a for loop, the next code to execute is the update expression. The continue statement

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP Example: i = 1; while (i < 10) {... cin >> n;// input an integer value if (n < 0) // test for negative n { i++; // prepare for next iteration continue; // go to next iteration }...// otherwise continue loop body } The continue statement …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP The equivalent for statement also uses continue. for(i=1; i < 10; i++) {... if (n < 0) // test for negative n continue; // increment i and perform loop test... // otherwise continue loop body } The continue statement …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP What is the output from this program? int main() { for (int i = 0;i < 8;i++) { if (i%2 == 0) cout << i + 1 << endl; else if (i%3 == 0) continue; else if (i%5 == 0) break; else cout << " Not multiple of 2, 3 or 5.\n "; } cout << " End of program.\n "; } The continue/break statement