Repetition Repeating the Execution of Statements.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lesson 1. Objectives Skills/ConceptsMTA Exam Objectives Understanding Computer Programming Understand computer storage and.
Advertisements

LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Repeating Actions While and For Loops
Do/Loops A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Used when the exact number of iterations.
Control Flow C and Data Structures Baojian Hua
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Do Loops A Do..Loop terminates based on a condition that is specified Execution of a Do..Loop continues while a condition is True or until a condition.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
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: Control Structures II (Repetition)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Statement-Level Control Structures Sections 1-4
Alice in Action with Java Chapter 10 Flow Control in Java.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Chapter 5: Control Structures II (Repetition)
1 C++ Loop Statements Repetition Revisited. 2 Problem Using OCD, design and implement a function that, given a menu, its first valid choice, and its last.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
Controlling Function Behavior Sequence, Selection and Repetition.
C++ Loop Statements Repetition Revisited. Problem Using OCD, design and implement a function that, given a menu, its first valid choice, and its last.
C++ An Introduction to Computing, 3rd ed. 1 Repetition Chapter 7.
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.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
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,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
More Selection Executing Statements Selectively Chap. 7 (Read § & Part of Picture: Boolean Logic and Digital Design) 1.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
1 More Control Structures if and switch (Chap. 8) do-while and forever loops (Chap. 9)
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
C++ Loop Statements Repetition Revisited. Problem Using OCD, design and implement a function that, given a menu, its first valid choice, and its last.
1 Controlling Behavior Chap.5 Study Sections 5.1 – 5.3 The if and for Statements.
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.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Calvin College Controlling Behavior The if, switch and for Statements.
Selection Executing Statements Selectively. Review We’ve seen that the C++ if statement permits a Statement to be executed selectively: if (Expression)
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue 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.
More Selection Executing Statements Selectively Chap. 7 (Read § & Part of Picture: Boolean Logic and Digital Design) 1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
While ( number
6. LOOPS. Example: Summing a Series of Numbers #include int main(void) { int n, sum = 0; printf("This program sums a series of numbers.\n"); printf("Enter.
Controlling Behavior The if and for Statements. Function Behavior The behavior of a function is determined by the statements within the function. Statements.
Introduction to Programming Lesson 1. Algorithms Algorithm refers to a method for solving problems. Common techniques for representing an algorithms:
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
Controlling Behavior The if and for Statements.
Topic 4: Looping Statements
Repeating the Execution of Statements
The switch Statement, and Introduction to Looping
More Repetition Chap. 8 (Read § ) 1.
Looping and Repetition
Outline Altering flow of control Boolean expressions
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Chapter8: Statement-Level Control Structures April 9, 2019
Controlling Behavior The if and for Statements.
Presentation transcript:

Repetition Repeating the Execution of Statements

Review We’ve seen that the C++ for loop permits a statement to be executed repeatedly: for (Expr 1 ; Expr 2 ; Expr 3 ) Statement Expr 1 Expr 2 Statement Expr 3 F T

A Counting Loop The for loop is most commonly used to count from one value first to another value last: for (int count = first; count <= last; count++) Statement count = first count <= last Statement count++ F T

Other Loops C++ also provides the forever loop: a for loop without expressions: for (;;) { StatementList 1 if (Expression) break; StatementList 2 } StatementList 1 Expression F T StatementList 2 Repetition continues so long as Expression is false!

Pretest Loops If StatementList 1 is omitted from a forever loop, we get a test-at-the-top or pretest loop: for (;;) { if (Expression) break; StatementList 2 } Expression F T StatementList 2

The while Loop For such situations, C++ provides the more readable while loop, whose pattern is: while (Expression) Statement Expression T F Statement Statement can be either a single or compound C++ statement. Repetition continues so long as Expression is true!

Post-test Loops If StatementList 2 is omitted in a forever loop, we get a test-at-the-bottom or post-test loop: for (;;) { StatementList 1 if (Expression) break; } Expression FT StatementList 1

The do Loop For such situations, C++ provides the more readable do loop, whose pattern is: do Statement while (Expression); Statement can be either a single or compound C++ statement. Repetition continues so long as Expression is true! Expression TF Statement

Choosing a Loop With four loops at our disposal, how do we know which one to use? Use the for loop for counting problems.Use the for loop for counting problems. Design algorithms for non-counting problems using a general Loop statement, and see where it is appropriate for repetition to terminate:Design algorithms for non-counting problems using a general Loop statement, and see where it is appropriate for repetition to terminate: –If at the loop’s beginning, use the while loop –If at its end, use the do loop –If in its middle, use the forever loop.

Example Write a function that given a positive int value, returns a string of equivalent digits. Example:123 “123” Example:123  “123” 0 “” 0  “”

Algorithm 0. Receive intVal. 1. Initialize stringVal to the empty string; 2. Loop a. Set intDigit to the remainder of intVal / 10. b. Compute charDigit, the char equivalent to intDigit. c. Set stringVal to charDigit + stringVal. d. Set intVal to the quotient of intVal / 10. End loop. 3. Return stringVal. Question: How/where should repetition terminate?

Choice of Loop Our loop should terminate when intVal <= 0. We should check this condition at the beginning, because if intVal is initially zero (or negative), we do not want any of the statements in the loop’s body to execute. The pretest loop is thus the appropriate choice for this particular problem.

Algorithm (revised) 0. Receive intVal. 1. Initialize stringVal to the empty string; 2. Loop a. If (intVal <= 0): terminate repetition. b. Set intDigit to the remainder of intVal / 10. c. Compute charDigit, the char equivalent to intDigit. d. Set stringVal to charDigit + stringVal. e. Set intVal to the quotient of intVal / 10. End loop. 3. Return stringVal.

Coding We thus choose the while loop for this problem: string IntToString(int intVal) { const int ASCII_ZERO = 48; string stringVal = “”; int intDigit; char charDigit; while (intVal > 0) { intDigit = intVal % 10; charDigit = char(intDigit + ASCII_ZERO); stringVal = charDigit + stringVal; intVal /= 10; } return stringVal; }

Discussion The four C++ loops provide very different behaviors: The for loop is a loop designed for counting that provides pretest behavior.The for loop is a loop designed for counting that provides pretest behavior. The while loop is a general-purpose loop that provides test-at-the-top behavior.The while loop is a general-purpose loop that provides test-at-the-top behavior. The do loop is a general-purpose loop that provides test-at-the-bottom behavior.The do loop is a general-purpose loop that provides test-at-the-bottom behavior. The forever loop is a general-purpose loop that provides test-in-the-middle behavior.The forever loop is a general-purpose loop that provides test-in-the-middle behavior.

Discussion The while and for loops have their tests at the top, implying that if the loop’s condition is initially false, the body of the loop will not execute, which is called. The while and for loops have their tests at the top, implying that if the loop’s condition is initially false, the body of the loop will not execute, which is called zero-trip behavior. The do loop has its test at the bottom, implying that the body of the loop will execute at least once, regardless of the value of the loop’s condition, which is called. The do loop has its test at the bottom, implying that the body of the loop will execute at least once, regardless of the value of the loop’s condition, which is called one-trip behavior.

Discussion The forever loop its test in the middle: –Statements in StatementList 1 will be executed at least once, regardless of the value of Expression. –Statements in StatementList 2 will not be executed if Expression causes repetition to terminate. This might be called. This might be called half-trip behavior.

Summary C++ provides four repetitive execution statements: The for loop, for counting.The for loop, for counting. The while loop, a general-purpose pretest loop.The while loop, a general-purpose pretest loop. The do loop, a general-purpose post-test loop.The do loop, a general-purpose post-test loop. The forever loop, a general-purpose test-in-the- middle loop.The forever loop, a general-purpose test-in-the- middle loop. Which loop you use to solve a given problem should be determined by your algorithm for that problem.