[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci201] / 12
[Text Version] [Syllabus] [Schedule] [Glossary] [Labs] [Projects] [Resources] [Grading] [Contact] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] <12> [13] [14] [15] [16] [17] [18] [19] [20]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Wed Feb 25 10:00:40 PST 2009

Contents


    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

    [ 11.html ]

    Prepare

      5.5 do...while Repetition Statement

      Examples: [ dowhile.cpp ] (buggy), [ dowhile2.cpp ] (runs but a joke).

      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

      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
       		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

    1. setw(n)::iomanip="Set width of next output to n".

      What is ASCII

    2. ASCII::= See 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 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
      		}

      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

    3. 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.

       		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.

      Explain line 60 on page 209

       		while ( ( grade = cin.get() ) != EOF )
      This is a common cliche -- it does these things:
      1. Get the next character from cin (if any).
      2. If there was a character put its code in grade. If not, put EOF in grade.
      3. Test if grade has been set to EOF or is it a character
      4. If not EOF enter the loop.
      5. Elsejump to the end of the loop.

      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: [ 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.

    Exercises

    Quiz 5

    Next -- Logic and Conditions

    [ 13.html ]

    Abbreviations

  1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  2. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  3. KDE::="Kommon Desktop Environment".
  4. OOP::="Object-Oriented Programming", Current paradigm for programming.
  5. Semantics::=Rules determining the meaning of correct statements in a language.
  6. SP::="Structured Programming", a previous paradigm for programming.
  7. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  8. Q::software="A program I wrote to make software easier to develop",
  9. TBA::="To Be Announced", something I should do.
  10. TBD::="To Be Done", something you have to do.
  11. UML::="Unified Modeling Language".

End