Loops More loops Off-by-one errors Infinite loops Nested Loops.

Slides:



Advertisements
Similar presentations
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Advertisements

Recursion English –Return (Oxford/Webster) –procedure repeating itself indefinitely or until condition met, such as grammar rule (Webster) adequate: satisfactory.
C OMP 110 L OOPS Instructor: Jason Carter. 2 L OOPS More loops Off-by-one errors Infinite loops Nested Loops Animations Concurrency Synchronized methods.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
©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.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
©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.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
C OMP 110 L OOPS A DVANCED Instructor: Prasun Dewan.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
Repetition Statements while and do while loops
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Iteration Hussein Suleman UCT Dept of Computer Science CS115 ~ 2004.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Introduction to Computers and Programming Lecture 7:
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
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.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
UCT Department of Computer Science Computer Science 1015F Iteration
ECE Application Programming
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Lesson 05: Iterations Class Chat: Attendance: Participation
Chapter 5 Repetition.
Iteration with While You can say that again.
Outline Altering flow of control Boolean expressions
Iteration: Beyond the Basic PERFORM
More Loops Topics Counter-Controlled (Definite) Repetition
Repetition Statements
More Loops Topics Counter-Controlled (Definite) Repetition
Programming Fundamental
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 4: Loops and Iteration
Presentation transcript:

Loops More loops Off-by-one errors Infinite loops Nested Loops

Multiplying Numbers int product = 1; int nextNum = Keyboard.readInt(); while (nextNum >= 0) { product = product* nextNum; nextNum = Keyboard.readInt(); } print (product);

Cumulative Assignment int product = 1; int nextNum = Keyboard.readInt(); while (nextNum >= 0) { product *= nextNum; nextNum = Keyboard.readInt(); } print (product); = product *= nextNumproduct = product*num sum += num sum = sum+num

Multiplying Positive Numbers int product = 1; int nextNum = Keyboard.readInt(); while (nextNum >= 0) { product = product* nextNum; nextNum = Keyboard.readInt(); } print (product);

Multipling N Numbers (edit) int product = 1; int nextNum = Keyboard.readInt(); while (nextNum >= 0) { product *= nextNum; nextNum = Keyboard.readInt(); } print (product);

Multipling N Numbers (edited) int product = 1; int nextNum = Keyboard.readInt(); while (nextNum >= 0) { product *= nextNum; nextNum = Keyboard.readInt(); } print (product); int listLength = readListLength(); int counter = 0; int nextNum; int product = 1; while (counter < listLength) { counter += 1; nextNum = readNextNumber(); product *= nextNum; } print (product);

Multipling N Numbers (soln) int product = 1; int n = readNumElements(); int counter = 0; while (counter < n) { int nextNum = readNum(); product *= nextNum; counter += 1; }

Multipling First N Numbers: N! (edit) ??? 1*2*3*4 … n Factorial(n) == n!

Multipling First N Numbers int product = 1; int n = 2; int counter = 0; while (counter < n) { product *= counter; counter += 1; } System.out.println (product); 1*2*3*4 … n 1*0*1

Multipling First N Numbers int product = 1; int n = ???; int counter = 0; while (counter < n) { product *= counter; counter += 1; } System.out.println (product); 1*2*3*4 … n 1*0*1*2* … n-1 Off-by-one

Multipling First N Numbers int product = 1; int n = ???; int counter = 1; while (counter < n) { product *= counter; counter += 1; } System.out.println (product); 1*2*3*4 … n 1*1*2* … n-1 Off-by-one

Multipling First N Numbers int product = 1; int n = ???; int counter = 1; while (counter <= n) { product *= counter; counter += 1; } System.out.println (product); 1*2*3*4 … n 1*1*2* … n-1*n

Better Name int product = 1; int n = ???; int nextMultiplier = 1; while (nextMultiplier <= n) { product *= nextMultiplier; nextMultiplier += 1; } System.out.println (product); 1*2*3*4 … n 1*1*2* … n-1*n

Better Name int product = 1; int n = ???; int nextMultiplier = 0; while (nextMultiplier <= n) { product *= nextMultiplier; nextMultiplier += 1; } System.out.println (product); 1*2*3*4 … n 1*0*1* 2… n-1*n easy to spot off-by-one error

Incrementing Counter Before Operation int product = 1; int n = ???; int prevMultiplier = 0; while (prevMultiplier <= n) { prevMultiplier += 1; product *= prevMultiplier; } System.out.println (product); 1*2*3*4 … n 1*1*2* … n-1*n*n+1 Off-by-one

Incrementing Counter Before Operation int product = 1; int n = ???; int prevMultiplier = 0; while (prevMultiplier < n) { prevMultiplier += 1; product *= prevMultiplier; } System.out.println (product); 1*2*3*4 … n 1*1*2* … n-1*n

Checking of Non Equality int product = 1; int n = ???; int prevMultiplier = 0; while (prevMultiplier != n) { prevMultiplier += 1; product *= prevMultiplier; } System.out.println (product); 1*2*3*4 … n

Checking of Non Equality int product = 1; int n = -5; int prevMultiplier = 0; while (prevMultiplier != n) { prevMultiplier += 1; product *= prevMultiplier; } System.out.println (product); 1*2*3*4 … n

Checking of Non Equality int product = 1; int n = -5; int prevMultiplier = 0; while (prevMultiplier != n) { prevMultiplier += 1; product *= prevMultiplier; } System.out.println (product); 1*2*3*4 … n -5*-4*-3*-2*-1*0*1*2... Infinite Loop

Counter not changed int product = 1; int n = ???; int prevMultiplier = 0; while (prevMultiplier < n) { product *= prevMultiplier; } System.out.println (product); 1*2*3*4 … n 1*0*0*0*0….. Infinite Loop

Counter changed in wrong direction int product = 1; int n = ???; int prevMultiplier = 0; while (prevMultiplier < n) { prevMultiplier -= 1; product *= prevMultiplier; } System.out.println (product); 1*2*3*4 … n 1*0*-1*-2*-3 Infinite Loop

Guarding Against Infinite Loops Update variable(s) in loop expression. Expression must converge to false.

Decrementing Solution int product = 1; while (n > 0) { product *= n; n -= 1; } System.out.println(product); 1*2*3*4 … n 1*n*n-1*n-2*..1 Backwards multiplication

Decrementing Assignment int product = 1; while (n > 0) { product *= n; n --; } System.out.println(product); n = n-1;n-- n++ n = n+1;

Counter-Controlled Vs Event Controlled int product = 1; int nextNum = Keyboard.readInt(); while (nextNum >= 0) { product = product* nextNum; nextNum = Keyboard.readInt(); } print (product); int product = 1; int n = readNumElements(); int counter = 0; while (counter < n) { int nextNum = readNum(); product *= nextNum; counter += 1; } Event-Controlled Counter-Controlled

Counter-Controlled Vs Event- Controlled Number of loop iterations (executions of loop body) known before loop executed –initialize counter to some value –increment/decrement counter by fixed step beginning/end of body – exit when counter reaches limit. Limit not known before loop starts. –Test one more events (e.g. reading of input) occurring while loop executes. –Terminate when events make loop condition false.

Counter-Controlled Vs Event- Controlled Counting until 10 in Hide & SeekCounter-controlled Searching for others in Hide & Seek Event-controlled Grading Comp14 exams Counter-controlled Fishing for the right answer from students Event-controlled & Counter-controlled Counting the days until it is vacationCounter-controlled Counting # of candies in a jarEvent-controlled

Factorial List (edit) int n = ???; int product = 1; while (n > 0) { product *= n; n -= 1; } return product;

Factorial List (edited) int newVal = Keyboard.readInt(); while (newVal >= 0) { int product = 1; n = newVal; while (n > 0) { product *= n; n -= 1; } return product;

Factorial List (soln) public static int factorial (int n) { int product = 1; while (n > 0) { product *= n; n -= 1; } return product; } public static void main (String[] args) { int n = Keyboard.readInt(); while (n >= 0) { System.out.println(“factorial =“ + factorial(n)); n = Keyboard.readInt(); }

Removing Code Duplication (edit) public static void main (String[] args) { int n = Keyboard.readInt(); while (n >= 0) { System.out.println(“factorial =“ + factorial(n); n = Keyboard.readInt(); }

Removing Code Duplication (edited) public static void main (String[] args) { int n = Keyboard.readInt(); while (n >= 0) { System.out.println(“factorial =“ + factorial(n); n = Keyboard.readInt(); }

break Statement public static int factorial (int n) { int product = 1; while (n > 0) { product *= n; n -= 1; } return product; } public static void main (String[] args) { while (true) {// loop condition never false int n = Keyboard.readInt(); if (n < 0) break; System.out.println(“factorial =“ + factorial(n); }