Looping and Repetition

Slides:



Advertisements
Similar presentations
Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
Advertisements

Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
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.
Repetition Chapter 4: Control structures. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Loop Statements After reading and studying this Section,
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
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.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
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.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
While ( number
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
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.
Repetition Statements
ECE Application Programming
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Lecture 7: Repeating a Known Number of Times
Chapter 5: Control Structures II
Loop Structures.
Quick Test What do you mean by pre-test and post-test loops in C?
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
CiS 260: App Dev I Chapter 4: Control Structures II.
Java Programming: Guided Learning with Early Objects
JavaScript: Control Statements I
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
Programming Fundamentals Lecture #6 Program Control
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Introduction to Object-Oriented Programming with Java--Wu
Control Statements Loops.
CMPT 102 Introduction to Scientific Computer Programming
Chapter 6: Repetition Statements
Lab5 PROGRAMMING 1 Loop chapter4.
More Loops Topics Counter-Controlled (Definite) Repetition
Repetition Statements (Loops) - 2
Control Statements Loops.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
More Loops Topics Counter-Controlled (Definite) Repetition
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Loops and Iteration CS 21a: Introduction to Computing I
More Loops Topics Counter-Controlled (Definite) Repetition
Looping and Repetition
Presentation transcript:

Looping and Repetition SE-1010 Dr. Mark L. Hornick

Looping and Repetition Statements control a block of code to be executed for: a fixed number of times 1, 10, 10000… until a certain condition is met a value remains positive (or negative) a value remains below (or above) some limit … SE-1010 Dr. Mark L. Hornick

The two (three) main loop types: while() and it’s cousin do…while() Sentinel-controlled loops, where the loop body is executed repeatedly until a designated condition, called a sentinel, is encountered. for() A count-controlled loop, where the loop body is executed a fixed number of times. SE-1010 Dr. Mark L. Hornick

The do-while() Statement Format: do { <statements> } while (<boolean expression>); The <statements> part is known as the loop body The loop body is executed until the <boolean expression> becomes false And is always executed at least once! Even if <boolean expression> is false SE-1010 Dr. Mark L. Hornick

The do-while() statement Demo SE-1010 Dr. Mark L. Hornick

The while Statement Format: while ( <boolean expression> ){ <statements that repeat> // zero or more } As long as the <boolean expression> is true, the loop body is executed SE-1010 Dr. Mark L. Hornick

The while() statement Demo SE-1010 Dr. Mark L. Hornick

while vs. do-while The while statement is a pre-test loop because the test is done before the loop body is entered and executed Therefore, the loop body may not be executed if the test is false to begin with. The do-while statement is a post-test loop. With a post-test loop statement, the loop body is executed at least once. SE-1010 Dr. Mark L. Hornick

while vs. do-while int x=0, y=1; while(x==y) { // never true //statements never execute }; do { //statement executes once } while(x==y); // never true SE-1010 Dr. Mark L. Hornick

The break statement terminates a loop early, in the middle of the loop body int i=0; do { i=i+1; if( i>5 ) break; // quit early } while( i<100 ); <more statements> SE-1010 Dr. Mark L. Hornick

The continue statement causes the loop to repeat immediately, without processing the remaining statements int i=0; do { i=i+1; if( i>5 ) continue; // skip remaining code <other statements to be repeated> } while( i<100 ); <more statements> SE-1010 Dr. Mark L. Hornick

Common mistakes when using loops: Identify the error: int product = 0; while (product < 5000) { product = product * 5; } SE-1010 Dr. Mark L. Hornick

The infinite loop pitfall Identify the error: int product = 0; while (product < 5000) { product = product * 5; } Because product is initialized to 0, product will never be larger than 5000 (0 = 0 * 5), so the loop will never terminate. SE-1010 Dr. Mark L. Hornick

Identify the error: int count = 1; while (count != 10) { count = count + 2; } SE-1010 Dr. Mark L. Hornick

The Overflow pitfall Identify the error: int count = 1; while (count != 10) { count = count + 2; } Here, count will never equal 10 (1,3,5,7,9,11…) …so the while() loop runs forever Q: How large does count get?? SE-1010 Dr. Mark L. Hornick

About Overflow errors… An overflow error occurs when you attempt to assign a value larger than the maximum value the variable can hold. In Java, an overflow does not cause program termination. With types float and double, a value that represents infinity is assigned to the variable. With type int, the value “wraps around” and becomes a negative value. SE-1010 Dr. Mark L. Hornick

Identify the error: float count = 1.3f; while (count != 100003.0) { count = count + 1.0; } SE-1010 Dr. Mark L. Hornick

The Floating-point roundoff error Identify the error: float count = 1.0F; while (count != 1000.0) { count = count + 1.0; } Real numbers should not be used in testing or increment, because only an approximation of real numbers can generally be stored in a computer. SE-1010 Dr. Mark L. Hornick