More Repetition Chap. 8 (Read §8.1-8.5) 1.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
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.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Chapter 4: Control Structures II
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
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.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
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.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
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)
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.
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.
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 loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
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.
Controlling Behavior The if and for Statements.
Topic 4: Looping Statements
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Repeating the Execution of Statements
Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II
Lecture 7: Repeating a Known Number of Times
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Loop Structures.
Control Structures II (Repetition)
Chapter 5: Repetition Structures
Repetition-Counter control Loop
Java Programming: Guided Learning with Early Objects
Chapter 5: Control Structures II
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Building Java Programs
Looping and Repetition
Outline Altering flow of control Boolean expressions
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
The ‘while’ Statement September 27, 2006
Control Statements Loops.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CprE 185: Intro to Problem Solving (using C)
Chapter8: Statement-Level Control Structures April 9, 2019
If-statements & Indefinite Loops
Control Statements Loops.
Repetition Statements
Another Example Problem
Building Java Programs
Building Java Programs
Indefinite loop variations
Controlling Behavior The if and for Statements.
Building Java Programs
CprE 185: Intro to Problem Solving (using C)
Looping and Repetition
Presentation transcript:

More Repetition Chap. 8 (Read §8.1-8.5) 1

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

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 3

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

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 } 5

The while Loop For such situations, Java provides the more readable while loop, whose pattern is: Expression T F Statement while (Expression) Statement Statement can be either a single or a compound statement. Repetition continues so long as Expression is true. Unlike forever loops that terminate when Expression is false. 6

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; } 7

The do Loop For such situations, Java provides the more readable do loop, whose pattern is: NOTE! do Statement while (Expression); Expression T F Statement Statement can be either a single or compound statement. Repetition continues so long as Expression is true! Unlike forever loops that terminate when Expression is false. 8

Use in Input Loops Recall the two different types of input loops: counting approach sentinel approach Counting approach asked for number of inputs to be entered, used for loop. Requires foreknowledge of how many inputs Sentinel approach looked for a special input value to signal termination. Requires availability of appropriate sentinel value 9

Query Approach Use a do loop Query user at end of loop body loop body always executed at least one time Query user at end of loop body user response checked in loop condition do { // whatever … . . . theScreen.print("More inputs? (Y/N) : "); response = theKeyboard.readChar(); } while (response=='y' || response =='Y'); 10

Query Methods Note the extra code required in the loop to make the query to check the response This could be simplified by writing a method to ask the query, get the response, and return the boolean result. We might develop a class Query of various query methods. do { . . . } while (Query.moreValues()); 11

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 its end, use the do loop If at the loop’s beginning, use the while loop If in its middle, use the forever loop. 12

Example Write a function that given a positive int value, returns a string of equivalent digits. Example: 123 ® "123" , 1 ® "1" Algorithm: 0. Receive intVal. 1. Initialize stringVal to the empty string; 2. Loop a. Set intDigit to the remainder of intVal / 10. b. Attach intDigit at the beginning of stringVal. c. Set intVal to the quotient of intVal / 10. End loop 3. Return stringVal. Question: How/where should repetition terminate? 13

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. Revised Algorithm: 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. Attach intDigit at the beginning of stringVal d. Set intVal to the quotient of intVal / 10. End loop 3. Return stringVal. 14

Coding We thus choose the while loop for this problem: public static String intToString(int intVal) { String stringVal = ""; int intDigit; while (intVal > 0) intDigit = intVal % 10; stringVal = intDigit + stringVal; intVal /= 10; } return stringVal; 15

Summary The four Java loops provide very different behaviors: The while and for loops have their tests at the top. If the loop’s condition is initially false, the body of the loop will not execute; this is called zero-trip behavior. The do loop has its test at the bottom, so the body of the loop will execute at least once, regardless of the value of the loop’s condition; this is called one-trip behavior. The forever loop has its test in the middle. Statements preceding the test will always be executed at least once. Statements following the test will not be executed if the test causes repetition to terminate. This might be called half-trip behavior. Which loop you use to solve a given problem should be determined by your algorithm for that problem. 16