C OMP 110 L OOPS A DVANCED Instructor: Prasun Dewan.

Slides:



Advertisements
Similar presentations
C OMP 110 R ECURSION Instructor: Jason Carter. 2 R ECURSION English Return (Oxford/Webster) procedure repeating itself indefinitely or until condition.
Advertisements

Intro to CS – Honors I Control Flow: Loops GEORGIOS PORTOKALIDIS
Loops For While Do While. Loops Used to repeat something Loop statement creates and controls the loop.
Recursion English –Return (Oxford/Webster) –procedure repeating itself indefinitely or until condition met, such as grammar rule (Webster) adequate: satisfactory.
C OMP 110 A RRAYS Instructor: Jason Carter. 2 O UTLINE for loops Arrays.
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.
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.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Loops More loops Off-by-one errors Infinite loops Nested Loops.
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 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.
©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.
©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.
Comp 114 Foundations of Programming Instructor: Prasun Dewan (Pr  sün Divän)
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
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++ 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.
Chapter 5 Loops.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
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.
Chapter 4: Control Structures II
Repetition Statements while and do while loops
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Overview Go over parts of quiz? Another iteration structure for loop.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
C OMP 110 L OOPS Instructor: Prasun Dewan 2 P REREQUISITE Conditionals.
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:
C OMP 401 U SER -I NTERFACE VS. M AIN T HREADS Instructor: Prasun Dewan.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
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.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
While ( number
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Chapter 4 Repetition Statements (loops)
Outline Altering flow of control Boolean expressions
More Loops Topics Counter-Controlled (Definite) Repetition
Repetition Statements
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.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 4: Loops and Iteration
Presentation transcript:

C OMP 110 L OOPS A DVANCED Instructor: Prasun Dewan

2 P REREQUISITE Loops.

3 L OOPS More loops Off-by-one errors Infinite loops Nested Loops Animations Concurrency Synchronized methods Property changes

4 M ULTIPLYING N UMBERS int product = 1; int nextNum = Console.readInt(); while (nextNum >= 0) { product = product* nextNum; nextNum = Console.readInt(); } print (product);

5 C UMULATIVE A SSIGNMENT int product = 1; int nextNum = Console.readInt(); while (nextNum >= 0) { product *= nextNum; nextNum = Console.readInt(); } print (product); product *= nextNum;product = product * nextNum; = sum += numsum = sum+num

6 M ULTIPLYING P OSITIVE N UMBERS int product = 1; int nextNum = Console.readInt(); while (nextNum >= 0) { product *= nextNum; nextNum = Console.readInt(); } print(product);

7 M ULTIPLYING N N UMBERS (E DIT ) int product = 1; int nextNum = Console.readInt(); while (nextNum >= 0) { product *= nextNum; nextNum = Console.readInt(); } print(product);

8 M ULTIPLYING N N UMBERS int product = 1; int nextNum = Console.readInt(); while (nextNum >= 0) { product *= nextNum; nextNum = Console.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);

9 M ULTIPLYING F IRST N N UMBERS : N! (E DIT ) 1*2*3*4*…*nFactorial(n) = n!

10 M ULTIPLYING F IRST N N UMBERS : N! 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

11 M ULTIPLYING F IRST N N UMBERS : N! int product = 1; int n = ???; int counter = 0; while (counter < n) { product *= counter; counter += 1; } System.out.println (product); 1*2*3*4*…*nOff by one 1*0*1*2*3*4*…*n-1

12 M ULTIPLYING F IRST N N UMBERS : N! int product = 1; int n = ???; int counter = 1; while (counter < n) { product *= counter; counter += 1; } System.out.println (product); 1*2*3*4*…*nOff by one 1*1*2*3*4*…*n-1

13 M ULTIPLYING F IRST N N UMBERS : N! 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*3*4*…*n-1*n

14 B ETTER N AME 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*3*4*…*n-1*n

15 B ETTER N AME int product = 1; int n = ???; int nextMultiplier = 0; while (nextMultiplier <= n) { product *= nextMultiplier; nextMultiplier += 1; } System.out.println (product); 1*2*3*4*…*n Easier to spot off-by-one errors 1*1*2*3*4*…*n-1*n

16 I NCREMENTING C OUNTER B EFORE O PERATION 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*3*4*…*n-1*n*n+1 Off by one

17 I NCREMENTING C OUNTER B EFORE O PERATION 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*3*4*…*n-1*n

18 C HECKING OF N ON E QUALITY int product = 1; int n = ???; int prevMultiplier = 0; while (prevMultiplier != n) { prevMultiplier += 1; product *= prevMultiplier; } System.out.println (product); 1*2*3*4*…*n

19 C HECKING OF N ON E QUALITY 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

20 C HECKING OF N ON E QUALITY 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

21 C OUNTER N OT C HANGED 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*… Infinite loop

22 C OUNTER C HANGED IN THE W RONG D IRECTION 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*… Infinite loop

23 G UARDING A GAINST I NFINITE L OOPS Update variable(s) in loop expression Expression must converge to false

24 D ECREMENTING S OLUTION 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

25 D ECREMENTING S OLUTION int product = 1; while (n > 0) { product *= n; n--; } System.out.println(product); n--  n = n - 1 n++  n = n + 1

26 C OUNTER -C ONTROLLED VS. E VENT -C ONTROLLED int product = 1; int nextNum = Console.readInt(); while (nextNum >= 0) { product *= nextNum; nextNum = Console.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

27 C OUNTER -C ONTROLLED VS. E VENT -C ONTROLLED 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

28 C OUNTER -C ONTROLLED VS. E VENT -C ONTROLLED Counting until 10 in Hide & Seek Searching for others in Hide & Seek Grading Comp14 exams Fishing for the right answer from students Counting the days until it is vacation Counting # of candies in a jar Counter-controlled Event-controlled Counter-controlled Event-controlled & counter-controlled Counter-controlled Event-controlled

29 F ACTORIAL L IST (E DIT ) int n = ???; int product = 1; while (n > 0) { product *= n; n -= 1; } return product;

30 F ACTORIAL L IST 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 newVal = Console.readInt(); while (newVal >= 0) { System.out.println(“factorial =“ + factorial(newVal)); newVal = Console.readInt(); }

31 R EMOVING C ODE D UPLICATION (E DIT ) public static void main (String[] args) { int newVal = Console.readInt(); while (newVal >= 0) { System.out.println(“factorial =“ + factorial(newVal)); newVal = Console.readInt(); } public static int factorial ( int n) { int product = 1; while (n > 0) { product *= n; n -= 1; } return product; }

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

33 S TRING P ROCESSING Prints each character on a separate line int i = 0; while (i < s.length()) { System.out.println(s.charAt(i)); i++; }

34 D ISSECTING A L OOP int i = 0; while (i < s.length()) { System.out.println(s.charAt(i)); i++; } Loop Condition Loop Body

35 F INGER - GRAINED D ISSECTION // String s declared and initialized earlier int i = 0; while (i < s.length()) { System.out.println(s.charAt(i)); i++; } Loop Condition Loop Body Resetting Loop Variable Initializing Loop Variable for (int i=0; i<s.length(); i++) System.out.println ( s.charAt(i));

36 M EANING OF F OR L OOP for ( S1; E; S2) S3 for ( S1; E; S2) S3 for ( ; E; S2) S3 for ( ; E; S2) S3 for ( ; E;) S3 for ( ; E;) S3 for ( ;;) S3 for ( ;;) S3 S1; while (E) { S3; S2; } S1; while (E) { S3; S2; } while (E) { S3; S2; } while (E) { S3; S2; } while (E) { S3; } while (E) { S3; } S1; while ( true ) { S3; S2; } S1; while ( true ) { S3; S2; }