[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] / [CSE201] / 07
[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]
Tue Apr 16 15:05:27 PDT 2013

Contents


    cs201/07

      Previous

      Study pages 121-132

        You will need one or more loop statements in just about every program you write from now on.

        3.6 The While loop

        The fundamental loop -- all loops can be expressed using this one.

        Syntax 3.3 While Statement

      1. while_statement::="while" "(" condition ")" statement.

        Informal example 1

         		while(some ice cream left)
         			eat a spoonful;

        Informal example 2

         		while(not full)
         		{
         			eat ice cream;
         			get some more ice cream;
         		}

        Example from text [ 02exalg.cpp ]

        Semantics

         		while(A) B
        executes the statements like this
         		A; B; A; B; ... until !A

        [flow chart of while(A)B]

        Common Error 3.6 Infinite loops

        Common Error 3.7 Off-by-one Errors

        3.7 The for Loop

        Use for all situations that involve counting! And many other situations as well

        Syntax 3.4 for Statement

      2. for_statement::= "for" "(" initialization ";" condition ";" update ") statement.
         		for( int i=1; i<=10; i++) s=s+i;

        Informal example

         		for( number_of_bottles=99; number_of bottles > 0; number_of_bottles--)
         			drink bottle of beer;

        Count the semicolons.

        Semantics

         		for(A; B; C) D
        executes the statements like this
         		A; B; D; C;  B; D; C; B; D; C; B; D; C; ... until !B
        It does precisely the same thing as
         		{  A;
         		   while(B)
         		   {
         		       D
         		       C;
         		   }
         		}
        [Semantics of for(A;B;C)D]

        Quality Tip 3.3 Use for loops for their intended purpose

        Use for loops to count!

        Quality Tip 3.4 Don't use != to test for the end of a numeric range

        Notice however that in the next Common error an != is used because the loop is not counting items up to a particular value but searching a string for a particular value -- and this means we have to use "!=".

        Common Error 3.8 Forgetting a semicolon

        Quality Tip 3.5 Symmetric and asymetric Bounds

        Quality Tip 3.6 Count Iterations to get the bounds

        3.8 The do loop

        I don't like this... but you can use it if you like.

        Syntax 3.5 do statement

      3. do_statement::="do" statement "while" "(" condition ")" ";".

        The do loop

         		do A while (B);
        executes
         		A; B; A; B; ... until !A
        and is the smae as
         		A;
         		while(B)
         			A

        Boolean Loops

        We hardly ever need to write loop where a bool variable goes from 'false' to 'true' and then stops. Writing
         		for(bool p = false; p != true; p=!p)
        does not work! (Think about it).

        So in the rare cases when we need to do this try

         		for(int count=0; count <=1; count++)
         		{   bool p = (count == 1);
         		    ...
         		}

      Review Questions

    1. R3.15, 3.16, 3.17, 3.18, 3.20 3.21 3.22 3.23, 3.24, 3.25, 3.26, 3.27

      Exercises on loops and logic

      Fibbonaci revisited This code [ 05ex.cpp ] only does a few terms in the series and is very repetitive. Recode it to use a while loop, and continue while f3 is less than or equal to 100.

      Example of developing a program with a loop [ E_(mathematical_constant) ]

      1. Clarify problem
      2. Write an algorithm
      3. Walk through the algorithm and correct any logic errors
      4. Code in C++
      5. while( compiler gives errors )
        1. Fix compile error

      6. while( bugs show up )
        1. Debug code

      7. Run code

      Example of a quizlike While loop [ while0.cpp ]

      Project 3 Due Next time from chapter 3 with if statements

      Quiz 3 tracing a program with strings next time

      Lab 04 -- Logic

      [ lab04/ ]

      Next -- advanced looping

      [ 08.html ]

    Abbreviations

  1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  2. Class::=A description of a type of object that includes the data it knows and the functions it can execute.
  3. Function::programming=A selfcontained and named piece of program that knows how to do something.
  4. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  5. OOP::="Object-Oriented Programming", Current paradigm for programming.
  6. Semantics::=Rules determining the meaning of correct statements in a language.
  7. SP::="Structured Programming", a previous paradigm for programming.
  8. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  9. Q::software="A program I wrote to make software easier to develop",
  10. TBA::="To Be Announced", something I should do.
  11. TBD::="To Be Done", something you have to do.
  12. UML::="Unified Modeling Language", industry standard design and documentation diagrams.
  13. void::C++Keyword="Indicates a function that has no return value".

( End of document ) <<Contents | Top