. 11 The For Statement The .Key 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 .See ./10.html .Open 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.... .Close Prepare . Deliver a question .Open Questions and Answers . Explain the initialization continuationCondition and increment in a for loop A for loop always has precisely three parts plus a body: .As_is 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 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: .As_is { .As_is A; .As_is while(B) .As_is { .As_is D; .As_is C; .As_is } .As_is } (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. 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 .As_is 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 .As_is for(int i=1; i<10;i++)... .As_is 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: .As_is 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 .As_is long int cents; .Close . Exercises . Next .See ./12.html .See ./lab06/