The for loop is for counting..... we can use while for other loops.
Previous -- Fundamental Control Structure
[ 10.html ]
Prepare
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
for(A; B; C ) DA 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
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).
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.
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++)....
i++, j--, k*=2for instance.
long int cents;