Lets discuss about the last post where you experimenting with the for statement.
What happen when you initiate the variable "i" with 2?
The coding inside the loop will only be executed once. Since the variable "i" initial value is 2. Thats mean, in the beginning, the variable still fulfill the looping condition which is "i must less than 3" but then, after the first run of the coding, the variable "i" increased by 1 and thats mean the value now is 3 (duh). So, the coding will no longer be executed since it does not fulfill the looping condition. But if you modify the condition a bit, you can make it executed 2 times which is by changing to "i<=3" which mean "i less or equal to 3".
What happen when you change the looping condition with "i>4"?
Well, this is depends on the variable "i" initial value. If you set it to 0, then the coding will not be executed at all. "i" should be more than 4. Therefore, the variable does not satisfy the looping condition on it first run. So, no output is produced. BUT, if you set the initial value of "i" with a value that is bigger than 4, the coding will be executed in such way that it is a wise decision to already click the 'close' button without watching any longer. Let say the initial value of "i" is 5. After the first run it will increase to 6, and then 7, then 8,9,10,11 and so on (you already know the counting). No matter how many times the program run the repetition, the variable "i" is still more than 4. So, the coding will run a hundred, thousand, million times until your computer lose memory. In programming, this is what they called as INFINITE LOOP.
What happen when you change the increment with "i--" or "i+2"?
"i++" is equal to "i=i+1", therefore "i--" is equal to "i=i-1". If you applied this condition on the for statement without changing the looping condition and the variable "i" initial value, thats mean that it will also cause the program to experience the annoying infinite loop phenomenon(:p). "i" will be decrease like such: 0,-1,-2,-3,-4,-5........ So, no matter how much the coding run, variable "i" still satisfy the condition. Now, "i+2" means that "i" will be increased by 2 each time it runs the coding inside the for statement. So, how many times does the word "repeating" will be displayed?. Yup, only once.
And what happen when you declare the looping variable with data type "double" or "float"?
The repetition runs as usual. You actually can declare the looping variable with any data type as long as long as the variable is functioning mathematically with mathematical operation(+,-,*..etc). So, a number of type string or char will not do. Well, there is a trick to make it loop properly even if the data type is a char but theres no need to learn about that. And theres even a rumors said that there are also some compiler that will produce error message if the data type of the looping variable is other than int, thats mean, double and float are also not allowed. Now, just forget about this one, and continue using int for the looping variable. Much shorter to type(:p).
You can also declare the loop variable outside the for statement. Example:
int i;
for(i=0;i<10;i++)
{
cout<<"i declared the loop variable outside the for statement, so what? ";
}
This is an example of repetition and selection:
for (int i=2;i<24;i+4)
{
if (i<10)
{
cout<<"Monkey"<<endl;
}
else
{
cout<<"Monyet"<<endl;
}
}
Trace it.
Tuesday, June 7, 2011
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment