Repeating the Execution of Statements

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
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.
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,
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.
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,
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)
Repetition Repeating the Execution of Statements.
C++ Loop Statements Repetition Revisited. Problem Using OCD, design and implement a function that, given a menu, its first valid choice, and its last.
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 Controlling Behavior Chap.5 Study Sections 5.1 – 5.3 The if and for Statements.
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)
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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 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.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
Controlling Behavior The if and for Statements.
Topic 4: Looping Statements
CNG 140 C Programming (Lecture set 3)
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 5: Control Structures II
The switch Statement, and Introduction to Looping
Lecture 7: Repeating a Known Number of Times
Chapter 5: Control Structures II
More Repetition Chap. 8 (Read § ) 1.
Control Structures II (Repetition)
Java Programming: Guided Learning with Early Objects
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Loop Control Structure.
Looping and Repetition
Outline Altering flow of control Boolean expressions
Chapter 6: Repetition Statements
Chapter8: Statement-Level Control Structures April 9, 2019
Intro to Computer Science Loops
Chapter 5: Control Structures II (Repetition)
Introduction to Programming
Controlling Behavior The if and for Statements.
Looping and Repetition
Presentation transcript:

Repeating the Execution of Statements Repetition Repeating the Execution of Statements

Review We’ve seen that the C++ for loop permits a statement to be executed repeatedly: Expr1 Expr2 Statement Expr3 F T for (Expr1; Expr2; Expr3) Statement

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

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

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

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

Post-test Loops If StatementList2 is omitted in a forever loop, we get a test-at-the-bottom or post-test loop: Expression F T StatementList1 for (;;) { StatementList1 if (Expression) break; }

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

Choosing a Loop With four loops at our disposal, how do we know which one to use? 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: 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” 0 ® “”

Algorithm Question: How/where should repetition terminate? 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 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 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 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 one-trip behavior.

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

Summary C++ provides four repetitive execution statements: The for loop, for counting. The while loop, a general-purpose pretest loop. The do loop, a general-purpose post-test 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.