< Previous | Contents | Next >
In the next group of statements, I assign values to the six variables I declared. I’ll go through a few assignments and talk a little about each variable type.
In the following assignment statement I assign the value of 0 to score.
score = 0;
Now score stores 0.
You assign a value to a variable by writing the variable name followed by the assignment operator (=) followed by an expression. (Yes, technically 0 is an expression, which evaluates to, well, 0.)
In the statement I assign distance the value 1200.76.
distance = 1200.76;
Because distance is of type double, I can use it to store a number with a fractional part, which is just what I do.
In the following statement I assign playAgain the single-character value ’y’.
playAgain = ’y’;
As I did here, you can assign a character to a variable of type char by surrounding the character with single quotes.
22 Chapter 1 n Types, Variables, and Standard I/O: Lost Fortune
Variables of type char can store the 128 ASCII character values (assuming that your system uses the ASCII character set). ASCII, short for American Standard Code for Information Interchange, is a code for representing characters. To see a complete ASCII listing, check out Appendix D.
In the following statement I assign shieldsUp the value true.
shieldsUp = true;
In my program, this means that the player’s shields are up.
shieldsUp is a bool variable, which means it’s a Boolean variable. As such, it can represent either true or false. Although intriguing, you’ll have to wait until Chapter 2, “Truth, Branching, and the Game Loop: Guess My Number,” to learn more about this kind of variable.