Chapter 6 The while Statement

Slides:



Advertisements
Similar presentations
Java Programming Strings Chapter 7.
Advertisements

Chapter 04 (Part III) Control Statements: Part I.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and 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.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5 new The Do…Loop Statement
© 2007 Lawrenceville Press Slide 1 Chapter 6 The while Statement  Loop structure that executes a set of statements as long as a condition is true  The.
Java Programming: From the Ground Up
Chapter 12: How Long Can This Go On?
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 02 (Part III) Introduction to C++ Programming.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
CS101 Computer Programming I Chapter 4 Extra Examples.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
Chapter 4 October 22, The If Statement Programs make decisions If(condition){ Statement(s); } Condition  boolean expression Evaluates to either.
© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement  Loop structure that executes a set of statements as long as a condition.
Controlling Program Flow with Looping Structures
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
A variable is a name for a value stored in memory.
Chapter 4 – C Program Control
Chapter 4 Assignment Statement
Chapter 4 The If…Then Statement
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 7 Top-Down Development
Chapter 2 Assignment and Interactive Input
Loop Structures.
Chapter 3 Assignment Statement
Chapter 5: Repetition Structures
Repetition-Counter control Loop
Java Programming: Guided Learning with Early Objects
Java Programming: From Problem Analysis to Program Design, 4e
Advanced Programming Behnam Hatami Fall 2017.
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
MSIS 655 Advanced Business Applications Programming
Chapter 7: Strings and Characters
Chapter 5 The Do…Loop Statement
Can perform actions and provide communication
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
3 Control Statements:.
LOOPS BY: LAUREN & ROMEO.
Chapter 6 Control Statements: Part 2
Chapter 15 Debugging.
Chapter 6: Repetition Statements
M150: Data, Computing and Information
Repetition Statements (Loops) - 2
Chapter 4 - Program Control
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
Chapter 15 Debugging.
Chapter 3 Flow of Control Loops in Java.
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Control Statements:.
Presentation transcript:

Chapter 6 The while Statement 11/13/2018 7:10 PM Chapter 6 The while Statement Loop structure that executes a set of statements as long as a condition is true The condition is a Boolean expression Will never execute if the condition is initially false The loop below iterates once because the condition is true and then continues to iterate until response is not 1: response = 1; while (response == 1) { System.out.print("Enter 1 or 0:"); response = input.nextInt(); } Refer to page 131 in the text. © 2007 Lawrenceville Press

Chapter 6 The do-while Statement 11/13/2018 7:10 PM Chapter 6 The do-while Statement Alternative form of the while statement Executes at least once The statement do { System.out.print("Enter 1 or 0:"); response = input.nextInt(); } while (response == 1); iterates once and continues to iterate until response is not 1. Refer to pages 131 and 132 in the text. © 2007 Lawrenceville Press

Chapter 6 Infinite Loops 11/13/2018 7:10 PM Chapter 6 Infinite Loops A loop that continues executing forever Can be caused by syntax or logic errors. For example: while (num < 0) //error--no braces System.out.print("Enter a vale: "); num = input.nextInt(); Some errors can result in an overflow Refer to page 132 in the text. An infinite loop can cause an application to stop responding or just "hang". Closing the application window (with JCreator Pro) will end the application so that the source of the infinite loop can be located and corrected. Errors that result in an overflow may generate unexpected results rather than "hanging" the application. © 2007 Lawrenceville Press

A variable that is incremented by a constant value 11/13/2018 7:10 PM Chapter 6 Counters A variable that is incremented by a constant value Often used for counting loop iterations Should be initialized to 0 when declared The counter in the loop counts the number of responses: do { System.out.print("Enter 1 or 0:"); response = input.nextInt(); numResponses += 1; } while (response == 1); Refer to page 133 in the text. © 2007 Lawrenceville Press

A variable that is incremented by a varying amount 11/13/2018 7:10 PM Chapter 6 Accumulators A variable that is incremented by a varying amount Often used for summing Should be initialized to 0 when declared The accumulator in the loop sums values: do { System.out.print("Enter grade:"); grade = input.nextInt(); sumOfGrades += grade; } while (grade != 999); Refer to page 133 in the text. © 2007 Lawrenceville Press

A flag, or sentinel, indicates when a loop should stop iterating 11/13/2018 7:10 PM Chapter 6 Using Flags A flag, or sentinel, indicates when a loop should stop iterating Often a constant Code is easier to modify when sentinels are constants declared at the beginning of an application A flag is used in the condition of the loop: final int STOP = 999; do { System.out.print("Enter grade:"); grade = input.nextInt(); sumOfGrades += grade; } while (grade != STOP); Refer to page 133 in the text. © 2007 Lawrenceville Press

Chapter 6 The for Statement 11/13/2018 7:10 PM Chapter 6 The for Statement Loop structure that executes a set of statements a fixed number of times Uses a loop control variable (lcv) The increment (++) or decrement (--) operators are used to change the value of the loop control variable The loop below executes until i is greater than 10: for (int i = 0; i <= 10; i++) { sum += i; } Refer to page 135 in the text. © 2007 Lawrenceville Press

Chapter 6 Debugging Techniques 11/13/2018 7:10 PM Chapter 6 Debugging Techniques The debugger included with many compilers Variable trace, which is a manual technique of list values of variables at the points of assignment Additional println() statements for displaying variable values at points of assignment "Commenting out" code to detect bugs through a process of elimination Refer to pages 136 and 137 in the text. © 2007 Lawrenceville Press

Chapter 6 Variable Trace 11/13/2018 7:10 PM Chapter 6 Variable Trace int num1 = 0; int num2 = 0; while (num1 < 10) { if (num1 % 3 == 0) { num2 += num1; System.out.print(num2 + " "); } num1 += 1; } Refer to page 136 in the text. © 2007 Lawrenceville Press

Chapter 6 Using println() to Debug 11/13/2018 7:10 PM Chapter 6 Using println() to Debug int num1 = 0; int num2 = 0; System.out.println("num1 before while: " + num1); //debug while (num1 < 10) { System.out.println("num1 in while: " + num1); //debug if (num1 % 3 == 0) { num2 += num1; System.out.println("num2: " + num2); //debug System.out.print(num2 + " "); } num1 += 1; } Refer to page 137 in the text. When using println() statements to debug a segment of code, it is usually most effective to place statements just after assignment changes the value of a variable. © 2007 Lawrenceville Press

Chapter 6 Using Comments to Debug 11/13/2018 7:10 PM Chapter 6 Using Comments to Debug int num1 = 0; int num2 = 0; while (num1 < 10) { //if (num1 % 3 == 0) { // num2 += num1; // System.out.print(num2 + " "); //} num1 += 1; } Refer to page 137 in the text. Comments can be used to comment out segments of code to determine through a process of elimination the location of any bugs. © 2007 Lawrenceville Press

Chapter 6 The String Class 11/13/2018 7:10 PM Chapter 6 The String Class Part of the java.lang package A String object is comprised of a sequence of characters with the first character at index position 0 String methods include: length() isEmpty() substring() toLowerCase() toUpperCase() trim() replaceFirst() replaceAll() Refer to page 138 in the text. The String class is used for representing text. The first character of a string is at index position 0. The last character is at position length()-1. © 2007 Lawrenceville Press

Chapter 6 String Data and the Scanner Class 11/13/2018 7:10 PM Chapter 6 String Data and the Scanner Class The next() method should be used for reading string data after numeric data has been read. If the nextLine() method is used, the end-of-line character left by the numeric entry is read and any text typed is ignored. System.out.print("Enter age: "); age = input.nextInt(); System.out.print("Enter first name: "); firstName = input.next(); Refer to page 138 in the text. You may also want to refer students back to page 82. The nextInt() and nextDouble() methods read in data up to an end-of-line character. The nextLine() method reads data up to and including the end-of-line character. Because of this, calling a nextLine() method after a nextInt() or nextDouble() reads the end-of-line left by those methods and the string entry just made is lost. © 2007 Lawrenceville Press

Chapter 6 String Data and the Scanner Class (con't) 11/13/2018 7:10 PM Chapter 6 String Data and the Scanner Class (con't) Alternatively, a statement can be added to read the end-of-line character left by the numeric entry, so that the nextLine() method can be used to read a string that may contain white space: System.out.print("Enter age: "); age = input.nextInt(); input.nextLine(); //remove end-of-line System.out.print("Enter full name: "); fullName = input.nextLine(); Refer to page 138 in the text. You may also want to refer students back to page 82. Note that an input.nextLine() statement can be added to simply remove the end-of-line left by the numeric entry. © 2007 Lawrenceville Press

Chapter 6 String Assignment 11/13/2018 7:10 PM Chapter 6 String Assignment Strings are immutable. Assigning a new string to a String object simply changes the object reference to point to the new string in memory: String text; text = "heLlO"; text = text.toLowerCase(); text heLlO hello Note: This slide contains animations. Press the space bar or click the mouse button to display each animation. This slide contains three (3) animations. Refer to page 139 in the text. The object instantiation, String text, associates the object name text with a memory location <press space bar> The first assignment statement points to the location in memory that stores the mixed-case string <press space bar> The second assignment statement, moves the pointer to the location in memory that stores the lowercase string <press space bar> © 2007 Lawrenceville Press

Chapter 6 Comparing Strings 11/13/2018 7:10 PM Chapter 6 Comparing Strings The String class includes several methods for comparing two strings: equals() equalsIgnoreCase() compareTo() compareToIgnoreCase() indexOf() lastIndexOf() startsWith() endsWith() Refer to page 140 in the text. © 2007 Lawrenceville Press