AP Java 10-13-2015 Java’s version of Repeat until.

Slides:



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

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
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.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
LAB 10.
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
Computer Programming Lab(5).
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.
Java Programming: From the Ground Up
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.
Lec 4: while loop and do-while loop. Review from last week if Statements if (num < 0.5){...println("heads"); } if –else Statements if (num < 0.5){...println("heads");
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
Truth and while Today 15 Minutes online time to finish the random/Swing programs. Truth tables: Ways to organize results of Boolean expressions. Note Taking:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Java Review if Online Time For loop Quiz on Thursday.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Computer Science I: Understand how to evaluate expressions with DIV and MOD Random Numbers Reading random code Writing random code Odds/evens/…
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Jeopardy $100 VariablesErrorsLoops Classes and Objects Program Structure $200 $300 $400 $500 $400 $300 $200 $100 $500 $400 $300 $200 $100 $500 $400 $300.
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.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
Sophomore Scholars Java
Slides by Evan Gallagher
Slides by Evan Gallagher
CSC111 Quick Revision.
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
Repetition-Counter control Loop
Repetition-Sentinel,Flag Loop/Do_While
Repetition.
Chapter 5: Control Structures II
Building Java Programs
Building Java Programs
Building Java Programs
Java Fix a program that has if Online time for Monday’s Program
Building Java Programs
Truth tables: Ways to organize results of Boolean expressions.
Control Statements Loops.
Truth tables: Ways to organize results of Boolean expressions.
Java Fix a program that has if Online time for Monday’s Program
Building Java Programs
Truth tables: Ways to organize results of Boolean expressions.
Computer Science 1 Online time for Graphics Program Random review
How can you make a guessing game?
Java Programming Loops
Building Java Programs
Control Statements Loops.
How can you make a guessing game?
Random Numbers while loop
Building Java Programs
Computer Science Club 1st November 2019.
Presentation transcript:

AP Java Java’s version of Repeat until

Learning Objectives Review the while loop. Be able to write a program using the do loop.

class LoopExample { public static void main (String[] args ) { int a = 10; int b= 4; while (b< a) { b= b + b / 3; System.out.println(a + b); } Dry run the following

class LoopExample2 { public static void main (String[] args ) { int a = 0; String name = “West Salem Cross Country”; while (!name.substring(a,a+1).equals(“C”)) { a++; } System.out.println(name.substring(a)); } Dry run the following

Java does not have repeat..until Instead it uses do..while(condition); Referred to as a ‘do’ loop. When to use it –Repeating an unknown number of times –Occurs at least one time Examples –Checking calculations –If it makes sense to do something ‘until’ a condition is not met. –While the condition is true, it will repeat again.

do..while loop public class DoWhileTest { public static void main( String args[] ) { int counter = 1; // initialize counter do { System.out.printf( "%d ", counter ); counter++; } while ( counter <= 10 ); // end do...while System.out.println(); // outputs a newline } // end main } // end class DoWhileTest What does this do?

Do.. semantics When –When you want to repeat something an unknown number of times and at least once. Semantics –Initialize variable (Pick the number the other person needs to guess) –do Get/calculate variable (Make a guess) (Squiggly line) All the other stuff you want to repeat (Have the timer ticking, tell them if the guess is too high or too low, etc.) –while (variable != flag) (They have not guessed correctly)

import java.util.Scanner; public class GuessGame { public static void main( String [] args ) { Scanner input = new Scanner(System.in); int guess, counter = 0; // initialize counter do { } while ( ); // end do...while System.out.println("You guessed it in " + count + " guesses"); } Modify the following to make a guessing game.

Program options Write a program to test Ulam’s conjecture. –Enter any positive integer. If it is even, divide it by two. If it is odd, multiply it by three and add one. Ulam suggests that if you do this repeatedly, you will eventually get to the number 1. The program will show each number in this sequence. Yahtzee!!! Write a program that will find the number of rolls it takes to get Yahtzee ( 5 dice all with the same value). Write a program that will calculate the number of bounces it takes for a ball to return to a height of less than one foot. –The user enters the starting height, –The ball bounces to 80% of its previous height. –Show the height after each bounce and the total number of bounces. Example: –Input: 10 feet –Calculation: 10*0.80 = 8 feet 8*0.80 = 6.4 feet 6.4*0.80 = 5.12 feet 5.12*0.8 = feet 4.096*0.8 = feet *0.8 = feet *0.8 = feet *0.8 = feet *0.8 = feet *0.8 = feet *0.8 = feet Starting from 10 feet it took 11 bounces to get below 1 foot.