Download presentation
Presentation is loading. Please wait.
1
Repetition Structures: Do-while Loop Random Number Generation CSC 1401: Introduction to Programming with Java Week 6 Wanda M. Kunkle
2
Repetition Structures do-while do-while Usage: Usage: Used to specify that an action is to be repeated as long as some condition remains true Used to specify that an action is to be repeated as long as some condition remains true Always executes at least once (Why?) Always executes at least once (Why?) Format: do { // Braces are only needed when there // are multiple statements Statement(s) } while (Condition);// Note the semicolon after the condition Format: do { // Braces are only needed when there // are multiple statements Statement(s) } while (Condition);// Note the semicolon after the condition Example: int counter = 0; do { out.writeln(counter); // Display counter counter = counter + 1; // Increment counter by 1 } while (counter < 100); // Loops as long as counter is less than 100 Example: int counter = 0; do { out.writeln(counter); // Display counter counter = counter + 1; // Increment counter by 1 } while (counter < 100); // Loops as long as counter is less than 100
3
Repetition Structures Examples of all three types: int counter = 0; Examples of all three types: int counter = 0; for (counter = 0; counter < 100; counter++) out.writeln(counter); for (counter = 0; counter < 100; counter++) out.writeln(counter); while (counter < 100) { out.writeln(counter); counter = counter + 1; } while (counter < 100) { out.writeln(counter); counter = counter + 1; } do { out.writeln(counter); counter = counter + 1; } while (counter < 100); do { out.writeln(counter); counter = counter + 1; } while (counter < 100);
4
Sample Programs Now let’s look at a sample program that demonstrates the use of the do-while loop (It should look familiar!): Now let’s look at a sample program that demonstrates the use of the do-while loop (It should look familiar!): twoPowersWithDoWhile.java twoPowersWithDoWhile.java twoPowersWithDoWhile.java
5
(Pseudo) Random Numbers A computer cannot perform a task without being provided with instructions on how to do so. A computer cannot perform a task without being provided with instructions on how to do so. It is therefore logical to assume that a computer is not capable of generating truly random numbers. It is therefore logical to assume that a computer is not capable of generating truly random numbers. Since it does not possess this capability, we call the so-called random numbers generated by a computer pseudo random numbers. Since it does not possess this capability, we call the so-called random numbers generated by a computer pseudo random numbers.
6
(Pseudo) Random Numbers The software that comes with our book provides us with a class for generating (pseudo) random numbers. The software that comes with our book provides us with a class for generating (pseudo) random numbers. To use it, we must specify a range in which to generate the numbers. To use it, we must specify a range in which to generate the numbers. Example: // Generate a number in the range 0 through 1, // inclusive, to represent the possible values (tails vs. // heads) when a coin is flipped random toss = new random(0, 1); // Retrieve an integer within that range int flip = toss.readint(); Example: // Generate a number in the range 0 through 1, // inclusive, to represent the possible values (tails vs. // heads) when a coin is flipped random toss = new random(0, 1); // Retrieve an integer within that range int flip = toss.readint();
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.