[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci201] / 11
[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 Feb 17 15:07:25 PST 2009

Contents


    11 The For Statement

    The for statement is a very important statement to learn. We use it in just about every real program we write.

    The for loop is for counting..... we can use while for other loops.

    Previous -- Fundamental Control Structure

    [ 10.html ]

    Prepare

      5.1 Introduction

      5.2 Essentials of Counter-Controlled Repetition

      5.3 for Repetition Statement

      5.4 Examples Using the for Statement

      Common Programming Error 5.7

      We do our taxes using a spreadsheet that I wrote.... and entered the numbers rounded to whole dollars. The spreadsheet did the computation using a version of double length but displayed the answer on the screen as whole numbers. We copied these into the form and sent it off to the IRS.

      The sent us back a check for $1. The rounding introduced a difference in the taxes. Since then my wife checks every calculation by hand. She doesn't trust the computer or the programmer.

      Using stream manipulators to format numerical output

      Neat stuff....

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

    Deliver a question

    Questions and Answers

      Explain the initialization continuationCondition and increment in a for loop

      A for loop always has precisely three parts plus a body:
       		for(A; B; C ) D
      A is the initialization part is is executed before the loops start. The B is the continuation condition and means that the loop will execute if its value is true. Then it executes the body D, followed by the increment C. Finally it returns to the condition B and tests it again... The pattern is
    1. A (B D C B D C B D C ... ) !B

      In effect the above for loop does the exact same thing as this set of more complex code:

       		{
       			A;
       			while(B)
       			{
       				D;
       				C;
       			}
       		}
      (The braces are necessary).

      Explain scope

      The scope of a variable or other name is the parts of the program where it has a particular meaning. It is always determine by the braces in the program. It starts where the name is declared and runs down to the closing brace that matches the opening brace that surrounds the declaration.

      If the same identifier is declared in different places then it gets different meanings.

      If a variable or function is redeclared in an inner block {...} then there is a hole in its scope. The different meaning hold in the hole.

      Are there different kinds of break statements

      No.
    2. The same type of statement has different effects inside a loop to a switch.

      How many breaks in a program

      As many as you need.

      But beware getting confused between breaking out of a loop and a surrounding switch.

      How do you get rid of the ./ in executing a program in UNIX

      You need to put "." (the current directory) in your shell PATH variable. Something like this
       PATH=$PATH:.
      needs to be put in a file with name ".profile" in your home directory. Do this very carefully and don't close the window until you have saved the change and opened a test window (which will get the new .profile).

      Do not do this if you are root.

      What kind of logic errors can changing the control variable inside a for loop

      The results are completely unpredictable. You could do all the wrong calculations -- or if clever and lucky the right ones.

      The loop may very well go on forever....

      Why do non-void functions without a return give a changing number

      A function that is not void is supposed to leave the result on top of a stack of values (called the run-time stack). If there is no return statement nothing is placed there, but the next part of the program doesn't no that and takes what ever garbage that is on the stack as the returned value.

      What is the purpose of a for loop

      Counting.

      When to us a for vs a while

      Counting --> for.

      Why do we still use while

      Some loops are not counting loop.

      What happens if I miss one of the parts of a for loop

      It substitutes "do nothing". Usually the loop runs forever.

      What happens if I forget parts of a loop using while

      It does evil things.

      Which operator has the highest priority

      In the operators with two operands it is multiplication and division.

      Does pow(x,n) use a for loop

      I don't thinks so.

      Why can't I use a local control variable outside its loop

      The compiler doesn't permit this for two reasons. (1) it reduces bugs. (2) it lets the same identifier be used in many loops. You can have a whole series of
       	for(int i=1; i<10;i++)...
       	for(int i=10; i<20; i++)....

      If a controlled variable is declared outside a loop can you use it outside

      Yes. Be careful about the last value left in it.

      Can a comma operator be used in any statement

      Yes. But it is most useful in a for or while statement to increment several variable:
       		i++, j--, k*=2
      for instance.

      Is there any way to do money better than using a double

      You could try doing it in a whole number of cents declared as
       		long int cents;

    Exercises

    Next

    [ 12.html ] [ lab06/ ]

    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