Chapter 8 Repetition Statements

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.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
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.
Objectives You should be able to describe:
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
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.
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.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
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.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
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.
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.
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.
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.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
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.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Repetitive Structures
Lecture 4b Repeating With Loops
REPETITION CONTROL STRUCTURE
CS161 Introduction to Computer Science
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.
Lecture 4B More Repetition Richard Gesick
Control Structures - Repetition
Conditinoal Constructs Review
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Additional Control Structures
Conditinoal Constructs Review
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Computing Fundamentals
Control Structures Part 2
Looping III (do … while statement)
Alternate Version of STARTING OUT WITH C++ 4th Edition
Objectives You should be able to describe: The while Statement
do/while Selection Structure
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Chapter 4 Repetition Structures
Presentation transcript:

Chapter 8 Repetition Statements

Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or statements, that will be repeated

8.1 General Repetition Concepts Two different categories of loops Pre-test: Test condition first - complete the body of the loop if condition is true Post-test: Test condition after body has executed once - body will only be repeated if the condition is true

8.1 General Repetition Concepts

8.1 General Repetition Concepts Looping structures must have the following: Variable(s) with an initial value(s) to control whether the body of the loop is executed (i.e., control variable) Conditional expression involving control variable(s) Statement within the body where control variable is modified each time the body of the loop is executed

8.1 General Repetition Concepts Infinite loop – a loop that continuously executes - the program or loop has to be terminated by the user or programmer Usually an error situation Most common cause - failure to manipulate the control variable In Windows/Unix - pressing Ctrl + C causes program to stop execution

8.1 General Repetition Concepts Nested loop – a loop embedded in another loop Almost any statement type can be the loop body – including loops or conditional statements Once inner loop finishes, flow transfers to the next statement following the inner loop

8.2 The while Loop Pre-test loop Syntax: while ( <condition> ) <action/body> Action, or body, will continue to execute while the condition remains true If the body needs to include multiple statements, surround them (action) with curly braces

8.2 The while Loop Sometimes see a loop written as: while ( 1 ) { ... } The numeric literal 1 is treated as a true condition and never changes – causing an infinite loop

8.2 The while Loop char again = ‘N'; cout << "\nDo you wish to multiply two numbers (y/n)? "; cin >> again; // Priming read //-| Read two numbers as long as usery says so. while ( again == 'y' || again == 'Y' ) // Notice no semicolon { cout << "Enter first number: "; cin >> operand1; cout << "Enter second number: "; cin >> operand2; cout << "Result: " << operand1 << " * " << operand2 << " = " << operand1 * operand2 << endl; // Don’t forget to change the control variable cout << "\nDo you wish to multiply two more numbers (y/n)? "; cin >> again; } // End of the loop body (action) cout << "The End" << endl;

8.2 The while Loop Priming read - a read before loop is reached Initializes a variable that can be used to control the loop. Initializes a variable being used as an accumulator General Concept – initialization To provide a correct answer when the loop does not execute its body Examples: sum = 0; min = first input value, etc.

8.3 The do-while Loop Post-test loop (the body executes at least once) Syntax: do <body/action> while ( <condition> ); Note semicolon after (…)!! If the body needs to include multiple statements, surround them with curly braces

8.3 The do-while Loop char menu_choice; float number; cout << "Please enter a number: "; cin >> number; do { cout << "\n1) Square the Number\n" << "2) Cube the Number\n" << "3) Exit\n\n" << "Please enter menu choice: " << endl; cin >> menu_choice; switch ( menu_choice ) { case '1': cout << number << " Squared = " << number * number << endl; break; case '2': cout << number << " Cubed = " << number * number * number << endl; case '3': cout << "Goodbye" << endl; default: cout << "Invalid menu option" << endl; } } while ( menu_choice != '3' ); // Notice the semicolon

8.4 The for Loop Generally used when: a specific number of iterations is required The control of the repetition is based on a sequence of values (e.g., 1,2,3,…,7; odd numbers; 5,4,3,2,1. Both while loops and for loops are pre-test loops - could be used interchangeably

8.4 The for Loop Syntax: (Red = for loop specification) for ( <initial_value_assignment> ; <continuation_condition> ; <control_variable_update> ) <action> Example: for (k=4; k <= 10; k++) cin >> x;

8.4 The for Loop for ( int i = 0; i < 5; i++ ) cout << i << ' '; // Output 0 1 2 3 4

8.4 The for Loop Four sections: for ( <init> ; <continue_condition> ; <control_var_update> ) <body/action> Four sections: Loop specification components separated by ; <init> and <update> components can include multiple statements separated by commas body can be any executable statement.

8.4 The for Loop Order in which the parts are executed: <init> <continuation_condition> <body/action> (if true condition) <update> <condition> ... The <init> section is only executed once

8.4 The for Loop Variable(s) may be declared in <init> section Scope is limited to the loop specification and body for (int k=1; k< 5; k++) cout << k << “ “;  output sequence 1 2 3 4

8.4 The for Loop // Example 1 for ( int i = 0; i < 5; i++ ) // No semicolon cout << i << endl; // Example 2 // Notice the multiple expressions for ( int i = 0, j = 5; i < 5; i++, j-- ) cout << i << ' ' << j << endl; // Example Output // Ex. 1 // Ex. 2 0 0 5 1 1 4 2 2 3 3 3 2 4 4 1

8.4 The for loop int sum = 0, value; for ( int i = 0; i < 5; i++ ) // No semicolon { cout << "Enter value " << i + 1 << ": "; cin >> value; sum += value; } cout << "The sum of the five values is: " << sum << endl; // Example Output Enter value 1: 4 Enter value 2: 7 Enter value 3: 3 Enter value 4: 12 Enter value 5: 99 The sum of the five values is: 125

8.4.1 Nested for Loops for loops can be nested Used in many algorithms Important when using multi-dimensional arrays

8.4.1 Nested for Loops for ( int row = 0; row < 5; row++ ) { for ( int col = 0; col < 5; col++ ) cout << col << ' '; cout << endl; } // Example Output 0 1 2 3 4

8.4.1 Nested for Loops for ( int row = 0; row < 5; row++ ) { for ( int col = row; col < 5; col++ ) cout << col << ' '; cout << endl; } // Example Output 0 1 2 3 4 1 2 3 4 2 3 4 3 4 4