while loop Condition Statement list

Slides:



Advertisements
Similar presentations
1 CS2200 Software Development Lecture 27: More Testing A. O’Riordan, 2008 K. Brown,
Advertisements

 Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Announcements The first graded lab will be posted Sunday 2/15 and due Friday 2/27 at midnight It is brand new, so please don’t hand in last semester’s.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
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.
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
LECTURE 5 Learning Objectives  To apply division algorithm  To apply the Euclidean algorithm.
1 LoopsBranching Condition Statement list T F Condition Statement list T F.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
CSC 212 Object-Oriented Programming and Java Part 2.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1/30/2016IT 2751 Operators Arithmetic operators: + - / * % Relational operators: == > = != Logical operators: || && !
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
2/19/2016IT 279, Chung-Chih Li1 Branching Condition Statement list 1 T F Statement list 2 Condition Statement list T F.
CSCI 125 & 161 Lecture 12 Martin van Bommel. Prime Numbers Prime number is one whose only divisors are the number 1 and itself Therefore, number is prime.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
8/2: Recursion About Scoping.java Recursion Program of the Day.
1 Identifiers: Names of variables, functions, classes (all user defined objects), Examples: a b gcd GCD A COSC1373 TAX Tax_Rate Tax Rate if else while.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Encryption Take 2: Practical details
Java Fundamentals 4.
4. Java language basics: Function
Whatcha doin'? Aims: To start using Python. To understand loops.
Chapter 6: Loops.
Chapter 4: Control Structures I
Repetition (While-Loop) version]
Chapter 5: Control Structures II
MODIFIED SIEVE OF ERATOSTHENES
Computer Science 101 While Statement.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Building Java Programs
Building Java Programs
Building Java Programs
Flow Control in Java.
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Chapter 4: Control Structures I
CSS161: Fundamentals of Computing
2.3 Recursion do automated demo of towers of hanoi before class
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
import becker.robots.City;
Building Java Programs
Coding Concepts (Basics)
Patterns to KNOW.
Building Java Programs
Three Special Structures – Case, Do While, and Do Until
CS2011 Introduction to Programming I Loop Statements (II)
CS302 - Data Structures using C++
Building Java Programs
Computer programming Lecture 3.
Application: Algorithms
Application: Algorithms
Building Java Programs
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Building Java Programs
Names of variables, functions, classes
Chapter 4: Loops and Files
The while Looping Structure
Presentation transcript:

while loop Condition Statement list How do I know the condition will become false? while (mark.isFacingNorth()) { mark.pickThing(); mark.move(); } Condition Statement list T while (mark.getStreet()!=10) { mark.move(); } F We should make sure the statement list will work towards the exit condition of the loop. But how? Drink more coffee! Think clear! 5/12/2019 ITK 168

Variables are name of memory locations where values are stored. int j; int i = 1; j = i; City ny; // ny = i; // this is illegal ny = new City(); Robot mark; RobotSE tom = new RobotSE(ny,0,0,Direction.EAST); mark = tom; // tom = mark; // this is illegal We have to decide for a variable: the name the type the value This may be changed later 5/12/2019 ITK 168

Variables We can use variable to control a loop. // the following code will ask the robot to keep moving // until it picks up 10 things. RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); int count=0; while (count != 10) { if (mark.canPickThing()) { mark.pickThing(); count ++; } else { mark.move(); How do I know the robot eventually will have 10 things? 5/12/2019 ITK 168

To guarantee exit, we may provide an upper bound of effort How about, pick 10 things, if possible, with 100 steps? RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); int count=0, distance=0; while (count != 10 && distance < 100) { if (mark.canPickThing()) { mark.pickThing(); count ++; } else { mark.move(); distance++; 5/12/2019 ITK 168

while loop for loop Statement Condition Statement list list repeat n times Statement list < n F = n while (Condition) { Statement list } for (???;???;???) { Statement list } 5/12/2019 ITK 168

don’t forget “;” be careful Another form of loop: do loop do { Statement list } while (condition); Statement list don’t forget “;” be careful Condition T This is not uncommon (forget the author’s comments on page 243) F E.g., ITK’s student has to take ITK168 until he/she gets a C or better. 5/12/2019 ITK 168

Another example of using do-loop Ask the user to input y (yes) or n (no), where y and n are the only two valid inputs for the program to proceed. This way, we avoid a great chance of accidentally pressing the keyboard. String answer; do { answer = JOptionPane.showInputDialog(“y or n?”); } while (!answer.equals(“y”) && !answer.equals(“n”)); String answer; answer = “x”; while (!answer.equals(“y”) && !answer.equals(“n”)) { answer = JOptionPane.showInputDialog(“y or n?”); } 5/12/2019 ITK 168

Primality Test A prime number is a positive integer that cannot be factorized, i.e., no numbers other that 1 and itself can divide it. Is (893 % 2 == 0) ? Is (893 % 3 == 0) ? Is (893 % 4 == 0) ? Is (893 % 5 == 0) ? Is (893 % 6 == 0) ? . Is (893 % 892 == 0) ? is 893 a prime? This test is silly but can do the job! We can write a loop to test these. 5/12/2019 ITK 168

Primality Test int p,r,i; // r: remainder, i: factor do { p = Integer.parseInt( JOptionPane.showInputDialog(“input an number:)); } while (p <= 1); i=1; do { i++; r = p % i; // r is the remainder of p divided by i } while (i < p && r != 0); if (i == p) System.out.println(“a prime number.”); else System.out.println(“not a prime number.”); System.out.println(“The least factor is ”+i); } 5/12/2019 ITK 168

The break statement in a loop will force the program to jump out of the loop immediately. Break a loop int i = 2; do { r = p % i; i++; } while (i < p && r != 0); int i = 2; do { r = p % i; if (r == 0) break; i++; } while (i < p); 5/12/2019 ITK 168

Break a loop import javax.swing.JOptionPane; .... public static void marryAsk() { String answer; do { answer = JOptionPane.showInputDialog( "Would you marry me? (y/n)"); if (answer.equals("Y") || answer.equals("y")) "Really? (y/n)"); } while (!answer.equals("Y") && !answer.equals("y")); JOptionPane.showMessageDialog(null, "Great!!"); } if (answer.equals("F") || answer.equals("f")) break; 5/12/2019 ITK 168

The Loops with multiple breaking points .... while (......) { if (....) break; ..... } Statement list Condition T F Spaghetti code? Why spaghetti code is bad? Well, this is not spaghetti code! 5/12/2019 ITK 168

The Loops with multiple breaking points This is Spaghetti code? This is not spaghetti code! All paths come to the same point Multiple-break-loop does not cause the trouble as the spaghetti code does 5/12/2019 ITK 168

1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 5/12/2019 ITK 168

Continue in a loop The continue statement in a loop will force the program to check the loop condition immediately. RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); while (mark.getAvenue() <= 10) { if (mark.frontIsClear()) { mark.move(); continue; } mark.turnRight(); mark.turnLeft(); 5/12/2019 ITK 168

Continue in a loop The continue statement in a loop will force the program to check the loop condition immediately. String answer; do { answer = JOptionPane.showInputDialog( "Would you marry me? (y/n)"); if (!answer.equals("Y") && !answer.equals("y")) continue; JOptionPane.showMessageDialog(null, "Great!!"); break; } while (true); 5/12/2019 ITK 168

Euclid Algorithm: finding the greatest common divisor int euclidAlgorithm(int a, int b) { int r = a % b; while (r != 0) a = b; b = r; r = a % b; } JOptionPane.showMessageDialog(null,“The GCD is”+b); return b; 5/12/2019 ITK 168

Bottom Factoring RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); if (mark.canPickThing()) { mark.pickThing(); mark.turnAround(); } else { mark.putThing(); } RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); if (mark.canPickThing()) { mark.pickThing(); } else { mark.putThing(); } mark.turnAround(); 5/12/2019 ITK 168

Top Factoring RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); if (mark.canPickThing()) { mark.turnAround(); mark.pickThing(); } else { mark.move(); } RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); mark.turnAround(); if (mark.canPickThing()) { mark.pickThing(); } else { mark.move(); } 5/12/2019 ITK 168

Top Factoring RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); if (mark.countThingsInBackpack()>2) { mark.putThing(); mark.move(); } else { mark.turnArounde(); } Top Factoring RobotSE mark = new RobotSE(ny,0,0,Direction.EAST); mark.putThing(); if (mark.countThingsInBackpack()>2) { mark.move(); } else { mark.turnAround(); } 5/12/2019 ITK 168

Top Factoring RobotSE mark = new RobotSE(ny,0,0,....); ... if (mark.isFacingNorth()) { mark.turnAround(); mark.pickThing(); } else { mark.move(); } RobotSE mark = new RobotSE(ny,0,0,....); ... mark.turnAround(); if (mark.isFacingNorth()) { mark.pickThing(); } else { mark.move(); } 5/12/2019 ITK 168

Top Factoring int a=1,i=0; ... if (i==0) { i++; a = a+i; } else { 5/12/2019 ITK 168