JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN 0133862119 © 2015 Pearson Education, Inc., Upper Saddle River,

Slides:



Advertisements
Similar presentations
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
Intro to CS – Honors I Control Flow: Loops GEORGIOS PORTOKALIDIS
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
COMP 110 Loops Tabitha Peck M.S. February 11, 2008 MWF 3-3:50 pm Philips
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Chapter 5: Control Structures II (Repetition)
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
© 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 Chapter 6 Repetition Statements.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Edited by JJ Shepherd
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
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.
Flow of Control Chapter 3 Flow of control Branching Loops
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CS101 Computer Programming I Chapter 4 Extra Examples.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Programming with Loops. When to Use a Loop  Whenever you have a repeated set of actions, you should consider using a loop.  For example, if you have.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
COMP Loop Statements Yi Hong May 21, 2015.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
Chapter 31 Announcements Project 2 has been posted –Due Feb 1st at 10:00pm –Work ALONE! Help hours –Monday – Thursday, 7-10pm –LWSN B146 Quiz solutions.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
Loops.
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Chapter 4 Control structures and Loops
Outline Altering flow of control Boolean expressions
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Chapter 3 Debugging Section 3.4
Chapter 3 Flow of Control Loops in Java.
Chapter 4: Loops and Iteration
Presentation transcript:

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Flow of Control: Loops Chapter 4 Part 2 Edited by JJ Shepherd

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

The for Statement A for statement executes the body of a loop a fixed number of times. Example for (int count = 1; count < 3; count++) System.out.println(count); //OR for (int count = 1; count < 3; count++) { System.out.println(count); }

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved The for Statement Syntax for (Initialization; Condition; Update) { Body_Statement } Body_Statement can be either a simple statement or a compound statement in {}. Corresponding while statement Initialization while (Condition) { Body_Statement Update }

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved The for Statement Figure 4.5 The action of the for loop in listing 4.5

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved The for Statement Figure 4.6 The semantics of the for statement

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved The for Statement Possible to declare variables within a for statement int sum = 0; for (int n = 1 ; n <= 10 ; n++) sum = sum + n * n; Note that variable n is local to the loop Most people do it this way

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved The for Statement A comma separates multiple initializations Example for (n = 1, product = 1; n <= 10; n++) product = product * n; Only one boolean expression is allowed, but it can consist of && s, || s, and ! s. Multiple update actions are allowed, too. for (n = 1, product = 1; n <= 10; product = product * n, n++); This is confusing and hardly anyone does this

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = doesn’t exist j = doesn’t exist Variable Values Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 0 j = doesn’t exist Variable Values Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 0 j = doesn’t exist Variable Values Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 0 j = doesn’t exist Variable Values Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 0 j = 0 Variable Values Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 0 j = 0 Variable Values Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 0 j = 0 Variable Values * Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 0 j = 1 Variable Values * Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 0 j = 1 Variable Values * Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 0 j = 1 Variable Values ** Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 0 j = 2 Variable Values ** Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 0 j = 2 Variable Values ** Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 0 j = 2 Variable Values *** Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 0 j = 3 Variable Values *** Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 0 j = 3 Variable Values *** Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 0 j = doesn’t exist Variable Values *** Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 1 j = doesn’t exist Variable Values *** Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 1 j = doesn’t exist Variable Values *** Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 1 j = doesn’t exist Variable Values *** Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 1 j = 0 Variable Values *** Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 1 j = 0 Variable Values *** Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 1 j = 0 Variable Values *** * Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = 1 j = 1 Variable Values *** * Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved

Nested for loop example for(int i = 0; i < length; i++) { for(int j = 0; j < width; j++) { System.out.print(“*”); } System.out.println(“”); } length = 2 width = 3 i = doesn’t exist j = doesn’t exist Variable Values *** Output

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved The for-each Statement Possible to step through values of an enumeration type Example enum Suit {CLUBS, DIAMONDS, HEARTS, SPADES} for (Suit nextSuit : Suit.values()) System.out.print(nextSuit + " "); System.out.println(); While this is important to know… We will come back to this a little later

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Programming with Loops: Outline The Loop Body Initializing Statements Controlling Loop Iterations break and continue statements Loop Bugs Tracing Variables Assertion checks

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved The Loop Body To design the loop body, write out the actions the code must accomplish. Then look for a repeated pattern. The pattern need not start with the first action. The repeated pattern will form the body of the loop. Some actions may need to be done after the pattern stops repeating.

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Initializing Statements Some variables need to have a value before the loop begins. Sometimes this is determined by what is supposed to happen after one loop iteration. Often variables have an initial value of zero or one, but not always. Other variables get values only while the loop is iterating.

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Controlling Number of Loop Iterations If the number of iterations is known before the loop starts, the loop is called a count-controlled loop. Use a for loop. Asking the user before each iteration if it is time to end the loop is called the ask-before-iterating technique. Appropriate for a small number of iterations Use a while loop or a do-while loop.

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved For large input lists, a sentinel value can be used to signal the end of the list. The sentinel value must be different from all the other possible inputs. A negative number following a long list of nonnegative exam scores could be suitable Controlling Number of Loop Iterations

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Example - reading a list of scores followed by a sentinel value int next = keyboard.nextInt(); while (next >= 0) { Process_The_Score next = keyboard.nextInt(); } Controlling Number of Loop Iterations

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved The break Statement in Loops A break statement can be used to end a loop immediately. The break statement ends only the innermost loop or switch statement that contains the break statement. break statements make loops more difficult to understand. Use break statements sparingly (if ever).

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Note program fragment, ending a loop with a break statement, listing 4.8 The break Statement in Loops

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved The continue Statement in Loops A continue statement Ends current loop iteration Begins the next one Text recommends avoiding use Introduce unneeded complications

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Tracing Variables Tracing variables means watching the variables change while the program is running. Simply insert temporary output statements in your program to print of the values of variables of interest Or, learn to use the debugging facility that may be provided by your system.

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Assertion Checks Assertion : something that says something about the state of the program Can be true or false Should be true when no mistakes in running program

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Assertion Checks Example found in comments //n == 1 while (n < limit) { n = 2 * n; } //n >= limit //n is the smallest power of 2 >= limit Syntax for assertion check Assert Boolean_Expression;

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Assertion Checks Equivalent example using assert assert n == 1; while (n = limit; //n is the smallest power of 2 >= limit.

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Loop Bugs Common loop bugs Unintended infinite loops Off-by-one errors Testing equality of floating-point numbers Subtle infinite loops The loop may terminate for some input values, but not for others. For example, you can’t get out of debt when the monthly penalty exceeds the monthly payment. Really good to know these

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Summary A loop is a programming construct that repeats an action Java has the while, the do-while, and the for statements The while and do-while repeat the loop while a condition is true The logic of a for statement is identical to the while

JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Summary Loops may be ended using a sentinel value or a boolean value Typical loop bugs include infinite loops or loops which are off by 1 iteration Variables may be traced by including temporary output statements or a debugging utility The assert statement can be used to check conditions at run time Use drawString to display text in an applet