Java the UML Way version 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and.

Slides:



Advertisements
Similar presentations
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Advertisements

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.
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
UNIT II Decision Making And Branching Decision Making And Looping
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
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
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.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Chapter 4: Loops and Files
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Chapter 5 Loops.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 4: Control Structures II
Repetition Statements while and do while loops
Chapter 5: Control Structures II
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Three kinds of looping structures The while loop The for loop The do (also called the do-while) loop All have equivalent power, e.g., if you can write.
Slides by Evan Gallagher
Lecture 4b Repeating With Loops
Chapter 5: Control Structures II
Repetition-Counter control Loop
Java Programming: Guided Learning with Early Objects
Chapter 5: Control Structures II
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Chapter 6: Repetition Statements
Lecture Notes – Week 4 Chapter 5 (Loops).
Suggested self-checks:
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 CGS3416 Spring 2019 Lecture 7.
Presentation transcript:

Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 6 Loop as a Control Structure What is a loop?page 2 The while statement with an examplepage 3 Testing loopspage 5 A graphics examplepage 6-7 The for statementpage 8-9 Nested control structurespage 10 The do-while statementpage 11 Choosing the right loop statementpage 12 Controlling input datapage 13

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 6, page 2 What is a Loop? An algorithm is “a limited and ordered set of well-defined rules for solving a problem” (see chapter 2) Three categories of execution order or control structures: –sequential (chapters 2-4) –selection (chapter 5, the if and switch statements) –loop (this chapter, the while, for, and do statements) A loop makes it possible to repeat a single statement or a block one or more times. A counter-controlled loop is a loop where the number of iterations is known in advance.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 6, page 3 The while Statement with an Example class PrintManyLines { public static void main(String[] args) { int counter = 0; while (counter < 5) { System.out.println("This is a line."); counter++; } Printout: This is a line. The while statement: while (boolean expression) statement/block Solve problem, page 163. counter = 0 [counter < 5] [counter >= 5)] print text counter++ loop initialization loop condition loop body loop condition update

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 6, page 4 class SearchForNumbers { public static void main(String[] args) { final int limit1 = 0; final int limit2 = 70; final int limit3 = 80; final int limit4 = 100; int noOfOK = 0; int number = (int) ((limit4 - limit1) * Math.random() + 1); while (number limit3) { noOfOK++; System.out.println(number); number = (int) ((limit4 - limit1) * Math.random() + 1); } System.out.println("We had to draw " + noOfOK + " numbers until we got an illegal one: " + number); } A Loop with a General Condition

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 6, page 5 Testing Loops Formulate test data catching the following situations: –The loop runs 0 times. –The loop runs exactly one time. –The loop runs several times (the most common situation). –Is it possible to construct data that never makes the loop condition false? If yes, that means you’ve created something that can become an endless loop. This is usually a mistake. Solve problems, 1-3 page 165.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 6, page 6 A Graphics Examples The placement of the 11 spots in the applet window is randomly distributed. We have these constants declared: private static final int windowWidth = 400; // have to agree private static final int windowHeight = 300; // with the html file private static final int diameter = 20; private static final int center = diameter / 2; private static final int noOfLines = 10; A random x value is drawn: x = (int) ((windowWidth - diameter) * Math.random() + 1); A circle is drawn: window.fillOval(x, y, diameter, diameter); A line between two points: window.drawLine(x1, y1, x2, y2); Problems –How many loop iterations do we need? –State the loop condition. –Construct the rest of the loop.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 6, page 7 Solution import java.awt.*; import javax.swing.*; public class SpotsAndLines extends JApplet { public void init() { Container content = getContentPane(); Drawing theDrawing = new Drawing(); content.add(theDrawing); } class Drawing extends JPanel { private static final int windowWidth = 400; private static final int windowHeight = 300; private static final int diameter = 20; private static final int center = diameter / 2; private static final int noOfLines = 10; Solve all problems, pp public void paintComponent(Graphics window) { super.paintComponent(window); int x = (int) ((windowWidth - diameter) * Math.random() + 1); int y = (int) ((windowHeight - diameter) * Math.random() + 1); window.fillOval(x, y, diameter, diameter); int counter = 0; while (counter < noOfLines) { int lastX = x; int lastY = y; x = (int) ((windowWidth - diameter) * Math.random() + 1); y = (int) ((windowHeight - diameter) * Math.random() + 1); window.drawLine(lastX + center, lastY + center, x + center, y + center); window.fillOval(x, y, diameter, diameter); counter++; }

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 6, page 8 The for Statement The while statement may be used for all kinds of loops. However, for counter-controlled loops the for statement is usually used. Problem: Identify loop initialization, condition, and condition update in these two loop constructions. Syntax: for (loopInitialization; loopCondition; loopConditionUpdate) statement/block Variables declared in the initialization portion of the for statement have the loop body as their scope. class PrintManyLines { public static void main(String[] args) { for (int counter = 0; counter < 5; counter++) { System.out.println("This is a line."); counter++; } class PrintManyLines { public static void main(String[] args) { int counter = 0; while (counter < 5) { System.out.println("This is a line."); counter++; }

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 6, page 9 The for Statement, Examples Draws 30 circles one after the other: int x = 0; int y = 50; for (int counter = 0; counter < 30; counter++) { window.drawOval(x, y, 20, 20); x += 20; } Other for statements: –Counts down from 1000 to 501 for (int number = 1000; number > 500; number--) { –Increases by 5 for every iteration for (int xPos = 100; xPos < 800; xPos += 5) { –Member no. from 101 to 900 for (int membNo = 101; membNo <= 900; membNo++) {

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 6, page 10 Nested Control Structures if statements can be nested inside each other, loops can be contained within other loops, if statements can be inside loops or loops inside if statements, etc... What will be printed out when this program runs? class SumNumbers { public static void main(String[] args) { for (int lineCounter = 0; lineCounter < 10; lineCounter++) { int sum = 0; for (int number = 1; number <= lineCounter; number++) { sum += number; System.out.print(number + " "); } System.out.println("The sum is: " + sum); } Solve problem 1, page 170.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 6, page 11 The do-while Statement The do-while statement distinguishes itself from the other loop statements in that its loop body is always executed at least once. Useful when input data should be controlled: An example: int noOfStudents; do { String inputText = JOptionPane.showInputDialog("How many students (max. 30)? "); noOfStudents = Integer.parseInt(inputText); } while (noOfStudents 30); Syntax: do statement/block while boolean expression

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 6, page 12 Choosing the Right Loop Statement Use a for statement if the number of loop iterations is known in advance, or if there is an integer variable that increases or decreases by a fixed value for every iteration. Use a while statement if the number of loop iterations is controlled by a general condition. Use a do-while statement if the number of loop iterations is controlled by a general condition and the loop will always be gone through at least once.

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN , John Wiley & Sons Ltd 2002 The Research Foundation TISIP, Chapter 6, page 13 Controlling Input Data Show program listing 6.5 pp