5.6 switch Multiple-Selection Statement
5.7 break and continue Statements
. . . . . . . . . ( end of section Prepare) <<Contents | End>>
Deliver -- Question and P6
Questions with Answers
if(answer=='y')
The single equals sign is used to assign a new value to a variable. Here is the format for an assignment statement:
variable = expression ;It evaluates the expression and puts the result into the variable.
It is quite legal to put an assignment inside any other statement because it returns a value -- the value that was assigned to the variable. So you can find things like this
sum=sumsquares=0;and it will compile and assign the 0 to two variables (useful!). But also this
if ( a = 24 )which will compile, assign 24 to a and return a non-zero value of 24 which the if will interpret as true. This is often a bug..... but some master C++ programmers use an "=" in a condition to great effect. There is an example in [ lab07/ ]
How long a chain of else-if can you have
As long as you like.
Why use setprecision
We use this to make the output has a certain number of decimal places. For example money
usually has two deimal places, only.
Example setw
What is type char
This is a "golden oldy" of computing.
C++ inherits a special data type that is like an integer but only big enough to store
a character -- in most cases an ASCII character. A character (roughly) is what is sent to the
computer when you tap a key once. The constant characters are shown in a program
with single quotes, by the way. You can do arithmetic on chars. You can assign and
test chars. They often appear in a switch like the next one.
Give an example of a switch
switch( answer )
{
case 'Y':
case 'y': //handle Yes....
break;
case 'N':
case 'n': //handle No...
break;
default: //handle unexpected answer
}
C++ has roots in the time before standard character codes -- when each company had their own way of signaling end of file. This was when C was young and didn't know that it would give birth to C++. The C people had a clever idea. Not intuitive -- clever. They decide that when you tried to read a character from an input stream you would be given an int value. If this value represented a character (typically in range 0 to 255 of ASCII) all was well. If the stream was ended then the value '-1' (negative one) would be returned instead.
int input=cin.get();
if(input!= -1)...
In the input/output libraries they defined a constant called EOF equal to minus 1. This made code a little easier to read.
int input=cin.get();
if(input!=EOF)...
When the standard was written it was decreeed that EOF could be any int that was not in the character range. So, we dare not write "-1" when we should have written "EOF". Some new library may use -42!
Why is grade or cin.get() an int
There was and is no common end of file char.
If character input is put in a "char" you can not determine if the end of file has arrived. It always looks like a char. By using an int we have at least one extra value we can use to signal end of file.
while ( ( grade = cin.get() ) != EOF )This is a common cliche -- it does these things:
Meanwhile: keep a copy of a loop like this so you can copy it into your own programs. For example: [ kitten.cpp ] , the little 'copycat' command.
Is there another way to input characters
Yes. There is a different way to get a character from an input
stream:
char c;
while( cin.get(c) ) ...Notice: we input the character (if any) into a char, and the command itself returns True if OK and false if not. To see how this works go to [ copy.cpp ] , a version of kitten.