Presentation is loading. Please wait.

Presentation is loading. Please wait.

More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }

Similar presentations


Presentation on theme: "More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }"— Presentation transcript:

1 More loops while and do-while

2 Recall the for loop in general for (initialization; boolean_expression; update) { }

3 while loop in general while (condition) { statement 1 ; statement 1 ; statement 2 ; statement 2 ;... statement n ; statement n ;}

4 while loop in general while (condition) single-statement; single-statement;

5 do-while loop in general do { //one or more statements here //one or more statements here statement 1 ; statement 1 ;... statement n ; statement n ; } while (condition);

6 while loop vs. do-while loop while (condition) { …}//---------- do { … } while (condition); What’s the difference?

7 while loop vs. do-while loop while (condition) { …}//---------- do { … } while (condition); The body of the do-while is always executed at least one time (regardless of the condition)!

8 while loop vs. do-while loop while (condition) { …}//---------- do { … } while (condition); Minor difference: If the body of the while-loop contains only a single statement, one doesn’t need to declare a block. The do-while always requires the declaration of a block.

9 What other statements included a condition? What is a condition?

10 Russian Roulette game Let chamber #1 be the chamber with the shell. All other chambers are empty. Let chamber #1 be the chamber with the shell. All other chambers are empty. You get 10 tries. If in 10 tries you never get the shell, you win! Otherwise... You get 10 tries. If in 10 tries you never get the shell, you win! Otherwise... Output should be something like: Output should be something like: 1: click!1: click! 1: click!1: click! 2: click!… 2: click!… 3: Bang!10: click! 3: Bang!10: click! You’re dead!You win! You’re dead!You win!

11 import java.util.Random; /* Let chamber #1 be the chamber with the shell. All other chambers are empty. You get 10 tries. If in 10 tries you never get the shell, you win! Otherwise... Output should be something like: 1: click! 2: click! 3: Bang! You’re dead! */ class RussianRoulette { public static void main ( String args[] ) { public static void main ( String args[] ) { //declare vars //declare vars //declare our random number generator. //declare our random number generator. //1..6 = one for each chamber //1..6 = one for each chamber Random r = new Random(); Random r = new Random(); final int N = 10; //max number of tries final int N = 10; //max number of tries //play the game //play the game ? //check the results //check the results if ( ? ) System.out.println( "You win!" ); if ( ? ) System.out.println( "You win!" ); else System.out.println( "You're dead!" ); else System.out.println( "You're dead!" ); }}

12 class RussianRoulette { public static void main ( String args[] ) { public static void main ( String args[] ) { //declare vars //declare vars //declare our random number generator. //declare our random number generator. //1..6 = one for each chamber //1..6 = one for each chamber Random r = new Random(); Random r = new Random(); final int N = 10; //max number of tries final int N = 10; //max number of tries //play the game //play the game boolean dead = false; boolean dead = false; ? //check the results //check the results if ( !dead ) System.out.println( "You win!" ); if ( !dead ) System.out.println( "You win!" ); else System.out.println( "You're dead!" ); else System.out.println( "You're dead!" ); }}

13 class RussianRoulette { public static void main ( String args[] ) { public static void main ( String args[] ) { //declare vars //declare vars //declare our random number generator. //declare our random number generator. //1..6 = one for each chamber //1..6 = one for each chamber Random r = new Random(); Random r = new Random(); final int N = 10; //max number of tries final int N = 10; //max number of tries //play the game //play the game boolean dead = false; boolean dead = false; while (!dead) { while (!dead) { ? } //check the results //check the results if (!dead) System.out.println( "You win!" ); if (!dead) System.out.println( "You win!" ); else System.out.println( "You're dead!" ); else System.out.println( "You're dead!" ); }}

14 public static void main ( String args[] ) { public static void main ( String args[] ) { //declare vars //declare vars //declare our random number generator. //declare our random number generator. //1..6 = one for each chamber //1..6 = one for each chamber Random r = new Random(); Random r = new Random(); final int N = 10; //max number of tries final int N = 10; //max number of tries //play the game //play the game boolean dead = false; boolean dead = false; while (!dead) { while (!dead) { switch (r.nextInt() % 6 + 1) { switch (r.nextInt() % 6 + 1) { case -1: case -1: case 1: case 1: System.out.println( i + ": Bang!" ); System.out.println( i + ": Bang!" ); dead = true; dead = true; break; break; default: default: System.out.println( i + ": click!" ); System.out.println( i + ": click!" ); break; break; } } //check the results //check the results if (!dead) System.out.println( "You win!" ); if (!dead) System.out.println( "You win!" ); else System.out.println( "You're dead!" ); else System.out.println( "You're dead!" ); } This is good but it keeps pulling the trigger until the inevitable eventuall happens!

15 public static void main ( String args[] ) { public static void main ( String args[] ) { //declare vars //declare vars //declare our random number generator. //declare our random number generator. //1..6 = one for each chamber //1..6 = one for each chamber Random r = new Random(); Random r = new Random(); final int N = 10; //max number of tries final int N = 10; //max number of tries //play the game //play the game boolean dead = false; boolean dead = false; int i = 0; int i = 0; while (!dead && i<N) { while (!dead && i<N) { switch (r.nextInt() % 6 + 1) { switch (r.nextInt() % 6 + 1) { case -1: case -1: case 1: case 1: System.out.println( i + ": Bang!" ); System.out.println( i + ": Bang!" ); dead = true; dead = true; break; break; default: default: System.out.println( i + ": click!" ); System.out.println( i + ": click!" ); break; break; } i++; i++; } //check the results //check the results if (!dead) System.out.println( "You win!" ); if (!dead) System.out.println( "You win!" ); else System.out.println( "You're dead!" ); else System.out.println( "You're dead!" ); }

16 import java.util.Random; /* Let chamber #1 be the chamber with the shell. All other chambers are empty. You get 10 tries. If in 10 tries you never get the shell, you win! Otherwise... Output should be something like: 1: click! 2: click! 3: Bang! You’re dead! */ class RussianRoulette { public static void main ( String args[] ) { public static void main ( String args[] ) { //declare vars //declare vars //declare our random number generator. //declare our random number generator. //1..6 = one for each chamber //1..6 = one for each chamber Random r = new Random(); Random r = new Random(); final int N = 10; //max number of tries final int N = 10; //max number of tries //play the game //play the game boolean dead = false; boolean dead = false; for (int i=1; i<=N && !dead; i++) { for (int i=1; i<=N && !dead; i++) { switch (r.nextInt() % 6 + 1) { switch (r.nextInt() % 6 + 1) { case -1: case -1: case 1: case 1: System.out.println( i + ": Bang!" ); System.out.println( i + ": Bang!" ); dead = true; dead = true; break; break; default: default: System.out.println( i + ": click!" ); System.out.println( i + ": click!" ); break; break; } } //check the results //check the results if (!dead) System.out.println( "You win!" ); if (!dead) System.out.println( "You win!" ); else System.out.println( "You're dead!" ); else System.out.println( "You're dead!" ); }} Another version using a for- loop. One could use a do- while as well.


Download ppt "More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }"

Similar presentations


Ads by Google