. 12 Other Control Structures -- do-while + switch + break and continue You have to know these to be competent, but I reccommend that you don't use them. . Previous -- the for statement .See ./11.html .Open Prepare . 5.5 do...while Repetition Statement Examples: .See ./dowhile.cpp (buggy), .See ./dowhile2.cpp (runs but a joke). . 5.6 switch Multiple-Selection Statement . 5.7 break and continue Statements .Close Prepare . Deliver -- Question and P6 .Open Questions with Answers . Explain == and = The double equals is a test to see if the two sides are equal. It mostly appears inside ifs, whiles, fors, etc. It returns a bool value of either `true` or `false` .As_is 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: .As_is 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 .As_is sum=sumsquares=0; and it will compile and assign the 0 to two variables (useful!). But also this .As_is 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 .See ./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 setw(n)::iomanip="Set width of next output to `n`". . What is ASCII ASCII::=http://cse.csusb.edu/dick/samples/comp.text.ASCII.html . 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 `char`s. You can assign and test `char`s. They often appear in a switch like the next one. . Give an example of a switch .As_is switch( answer ) .As_is { .As_is case 'Y': .As_is case 'y': //handle Yes.... .As_is break; .As_is case 'N': .As_is case 'n': //handle No... .As_is break; .As_is default: //handle unexpected answer .As_is } . When should we use the default case All good switches have a default case. . What is cin.get() This is an expression that sends the "get" message to the `cin` object. When `cin` has `get()` called it takes the next character from the input and returns it as a value of `cin.get()`. BUT if there is no next character then it returns $EOF. . Explain EOF and character input EOF::acronym="End Of File", term used thru out computing. 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. .As_is int input=cin.get(); .As_is 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. .As_is int input=cin.get(); .As_is 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. . Explain line 60 on page 209 .As_is while ( ( grade = cin.get() ) != EOF ) This is a common cliche -- it does these things: .List Get the next character from cin (if any). If there was a character put its code in grade. If not, put EOF in grade. Test if grade has been set to EOF or is it a character If not EOF enter the loop. Else jump to the end of the loop. .Close.List This is what Lewis Carrol would call a "portmanteu" it combines several functions into one statement. This is not normally a good idea, but this one has been used in thousands of programs and so you can trust it. Meanwhile: keep a copy of a loop like this so you can copy it into your own programs. For example: .See ./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: .As_is char c; .As_is 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 .See ./copy.cpp , a version of `kitten`. .Close . Exercises . Quiz 5 . Next -- Logic and Conditions .See ./13.html