Processing: An Introduction to Programming by Jeffrey L. Nyhoff & Larry R. Nyhoff

Processing: An Introduction to Programming by Jeffrey L. Nyhoff & Larry R. Nyhoff

Author:Jeffrey L. Nyhoff & Larry R. Nyhoff [Nyhoff, Jeffrey L.]
Language: eng
Format: azw3
Publisher: CRC Press
Published: 2017-05-19T04:00:00+00:00


Because the while  statement creates repetition, it is also known as a while  loop . In this chapter, we will work through several examples that illustrate the functionality of a while  statement (loop).

USING the while  Statement: Rolling a Die Repeatedly

Let’ s return to our Roll  program from Chapter  3. Select File >  Save As  and resave this program as RollWhile  .

Currently, our program consists of the following:

 int roll;

 roll = int( random(1, 7) );

 println("Roll: " + roll);

When we run this program, we see console output like the following:

 Roll: 2

If we keep running the program, it is only a matter of time before we see that a 6 is rolled:

 Roll: 6

We can be certain of this because we know that the statement

 roll = int( random(1, 7) );

will eventually assign the roll  variable the value 6  if we repeatedly run the program.

Currently, our program simulates just a single roll of a die. Thus, if we wish to simulate multiple  rolls of a die, we currently need to rerun the entire program. However, suppose that we would like to change this program into a game that will automatically  keep repeatedly rolling the die as long as  the roll is not a 6 (i.e., is a 1, 2, 3, 4, or 5). Once the roll is a 6, the repetition stops, and the game is over.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.