[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci201] / 08
[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]
Sun Feb 8 15:34:05 PST 2009

Contents


    08 Control Structures

      Previous -- classes and objects

      [ 07.html ]

      Prepare

        4.1 Introduction

        Here we look at more advanced techniques for coding our main() functions and member functions in our classes.

        Tracing a program will now involve tracing instructions that are repeated many times.

        4.2 Algorithms

        An algorithm is a precise description or recipe for achieving some goal, given some data. The description need to be precise enough that a machine can carry out the instructions.

        Algorithms often have complex structure.

        4.3 Pseudocode

        Pseudo code was used after flow charts went out of fashion in the 1980's.

        4.4 Control Structures

        There are three classic types of control structure
        1. Sequence {....}
        2. Selection if(...).... else ....
        3. Iteration while(....) ...

        4.5 if Selection Statement

        4.6 if...else Double-Selection Statement

        Two way choices: this or that.
         	if ( condition )
         		statement executed if condition was true
         	else
         		statement executed if condition was false

        Example: We want to count the number of odd and even numbers supplied by our user:

         	if( i % 2 == 0 )
         		odd=odd+1;
         	else
         		even=even+1;

        Exercise: There is a logic error above.... what is it?

        4.7 while Repetition Statement

         	while( condition )
         		statement repeated if condition is true and until it becomes false.

        Example -- we want to read a series of numbers until one is less than or equal to zero and count the number of odd and even numbers.

         	...
         	cin>>i;
         	while (i > 0 )
         	{
         		if(i%2==0)
         			even=even+1;
         		else
         			odd=odd+1;
         		cin>>i;
         	}
         	cout << "#odd " << odd << "#even "<< even << endl;
         	...

      . . . . . . . . . ( end of section Prepare) <<Contents | End>>

      Deliver a question + P4

      Project iteration 4 should have a print out of a program... complete with errors, probably.

      Questions and Answers

        What are the differences between float and double

        Both have addition, subtraction, multiplication, division, etc.... But this is because float numbers are converted to doubles before the arithmetic is done! Double take twice the space when the number is stored. Float is less accurate and doesn't have as large a range of numbers. So you are more likely to have errors with float than with double. We used float when RAM was expensive and measured in Kb not Gb.

        These days..... use double for all measurements and int for counting.

        We have if and else clauses, is there a then clause

        No.

        Have there been calls for other control structures

        Yes. But they haven't survived the consensus. When an idea has lots of support it gets put into a language like C++.

        Indeed the tendency has been to reduce the number of control structures.

        One thing -- we have three or four statements (break, raise, goto, return,...) which modify the standard behaviors of the three structures. More later in this class and/or CS202.

        The number of structures is pretty much standard these days.

        Can you prove that only Sequence + Selection + Iteration are needed

        The proof takes several pages and the use of Boolean variables (later). You can find it by following this link [BohmJaccopini66] and (on campus) to the Assoc Computer Machinery's digital library at [ 355592.365646 ] (off campus -- sign into library with your Coyote ID first).

        Other than if and else is there another way to check a condition

        We use switch to choose between a set values. The rest of the control structures give you loops rather than choices.

        There is a function in the <cassert> library that can checks is a condition is true, and if not, aborts the program. For example if I'm about the calculate the square root of an unknown number x then I should put this command first:

         		assert(x>=0);

        How do you compile Pseudocode

        You can't because it is a fake code. It is read and written by humans, for humans, only.

        As a result Pseudocode omits a lot of things that compilers need -- declarations, correct syntax, horrible details, predefined commands, etc.

        How do you type Pseudocode

        You can use any word processor or editor.

        I often put my Pseudocode in a comment at the start of a function with the heading: Algorithm.

         /*Algorithm
         	1. Open data file
         	...
         */

        When is it worth writing Pseudocode

        Any time you are not sure how to code something.... write it in strict English first and then translate it by hand into C++ or a real code.

        Can an if select several lines without using braces

        NO!

        Can you use while for anything except repeating statement

        I don't think so.

        Can you put while inside a while

        Yes.

        You can any control structure inside another one... and so on. Example in lab05.

        Can you make C++ start executing the last statement in the program

        No.

        The best you can do is to start with a command that jumps, somehow, to the last one. But C++ programs always start executing at "main".

        Do if-else and switch work the same way

        No. If-else is always 2 way and switch is n-way. The machine code is quite different. If-else has a two way condition branch command (BNZ for example). A switch is translated by computing the address of the next statement from the value in the switch.

        However, you can fake an if_else with a case:

         	switch(condition)
         	{
         		case true: true_part; break;
         		default: false_part;
         	}

      Exercises

      1. Without compiling and running this program [ hailstone.cpp ] and using pencil and paper, predict what it outputs.

      Next -- For statements

      [ 09.html ]

      Quiz 3 -- Classes and Objects

    . . . . . . . . . ( end of section 08 Control Structures) <<Contents | End>>

    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