more loops while and do-while. recall the for loop in general for (initialization;...

16
More loops More loops while while and and do-while do-while

Upload: amie-little

Post on 18-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

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

More loopsMore loops

whilewhile and and do-whiledo-while

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

Recall the Recall the forfor loop in general loop in general

for (for (initializationinitialization; ; boolean_expressionboolean_expression; ; updateupdate) {) {

}}

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

while while loop in generalloop in general

while (condition) {while (condition) {

statementstatement11;;

statementstatement22;; .. .. ..

statementstatementnn;;}}

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

while while loop in generalloop in general

while (condition)while (condition)

single-statement;single-statement;

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

do-whiledo-while loop in general loop in general

do {do { //one or more statements here//one or more statements here

statementstatement11;; .. .. ..

statementstatementnn;;} while (condition);} while (condition);

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

whilewhile loop vs. loop vs. do-whiledo-while loop loop

while (condition) {while (condition) { … …}}//----------//----------do {do { … …} while (condition);} while (condition);

What’s the difference?What’s the difference?

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

whilewhile loop vs. loop vs. do-whiledo-while loop loop

while (condition) {while (condition) {

… …

}}

//----------//----------

do {do {

… …

} while (condition);} while (condition);

The body of the The body of the do-whiledo-while is is alwaysalways executed executed at least one time (regardless of the at least one time (regardless of the condition)!condition)!

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

whilewhile loop vs. loop vs. do-whiledo-while loop loop

while (condition) {while (condition) { … …}}//----------//----------do {do { … …} while (condition);} while (condition);Minor difference:Minor difference:

If the body of the If the body of the while-loopwhile-loop contains only a contains only a single statement, one doesn’t need to single statement, one doesn’t need to declare a block.declare a block.The The do-whiledo-while alwaysalways requires the requires the declaration of a block.declaration of a block.

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

What other statements What other statements included a condition?included a condition?

What is a condition?What is a condition?

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

Russian Roulette gameRussian Roulette game

Let chamber #1 be the chamber with Let chamber #1 be the chamber with the shell. All other chambers are empty.the shell. All other chambers are empty.

You get 10 tries. If in 10 tries you never You get 10 tries. If in 10 tries you never get the shell, you win! Otherwise . . .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!3: Bang! 10: click!10: click!You’re dead!You’re dead! You win!You win!

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

import java.util.Random;import java.util.Random;/*/*Let chamber #1 be the chamber with the shell.Let chamber #1 be the chamber with the shell.All other chambers are empty. You get 10 tries.All other chambers are empty. You get 10 tries.If in 10 tries you never get the shell, you win!If in 10 tries you never get the shell, you win!Otherwise . . .Otherwise . . .

Output should be something like:Output should be something like:1: click!1: click!2: click!2: click!3: Bang!3: Bang!You’re dead!You’re dead!*/*/class RussianRoulette {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 triesfinal int N = 10; //max number of tries //play the game//play the game

?? //check the results//check the results

if (if (??) System.out.println( "You win!" );) System.out.println( "You win!" );

else System.out.println( "You're dead!" );else System.out.println( "You're dead!" ); }}}}

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

class RussianRoulette {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 triesfinal 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 (if (!dead!dead) System.out.println( "You win!" );) System.out.println( "You win!" ); else System.out.println( "You're dead!" );else System.out.println( "You're dead!" ); }}

}}

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

class RussianRoulette {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 triesfinal 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!" ); }}

}}

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

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 triesfinal 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!

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

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 triesfinal 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!" ); }}

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

import java.util.Random;import java.util.Random;/*/*Let chamber #1 be the chamber with the shell.Let chamber #1 be the chamber with the shell.All other chambers are empty. You get 10 tries.All other chambers are empty. You get 10 tries.If in 10 tries you never get the shell, you win!If in 10 tries you never get the shell, you win!Otherwise . . .Otherwise . . .

Output should be something like:Output should be something like:1: click!1: click!2: click!2: click!3: Bang!3: Bang!You’re dead!You’re dead!*/*/class RussianRoulette {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 triesfinal 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.