While and do.. while

Download Report

Transcript While and do.. while

AP Java
10-13-2015
Java’s version of Repeat until
Learning Objectives
• Review the while loop.
• Be able to write a program using the do loop.
class LoopExample
{
Dry run
the
following
public static void main (String[] args )
{
int a = 10;
int b= 4;
while (b< a)
{
b= b + b / 3;
System.out.println(a + b);
}
}
}
class LoopExample2
{
Dry run
the
following
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));
}
}
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);
Modify the
following to
make a
guessing
game.
int guess, counter = 0; // initialize counter
do
{
} while (
); // end do...while
System.out.println("You guessed it in " + count + " guesses");
}
}
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 = 4.096 feet
4.096*0.8 = 3.2768 feet
3.2768*0.8 = 2.62144 feet
2.62144*0.8 = 2.097152 feet
2.097152*0.8 = 1.6777216 feet
1.6777216*0.8 = 1.3421772 feet
1.3421772*0.8 = 1.0737417 feet
1.0737417*0.8 = 0.8589933 feet
Starting from 10 feet it took 11 bounces to get below 1 foot.