The for statement is typically used to count a number of items. At its regular structure, it is divided in three parts. The first section specifies the starting point for the count. The second section sets the counting limit. The last section determines the counting frequency. The syntax of the for statement is:
for( Start; End; Frequency) Statement;
The Start expression is a variable assigned the starting value. This could be Count = 0;
The End expression sets the criteria for ending the counting. An example would be Count < 24; this means the counting would continue as long as the Count variable is less than 24. When the count is about to rich 24, because in this case 24 is excluded, the counting would stop. To include the counting limit, use the <= or >= comparison operators depending on how you are counting. The Frequency expression would let the compiler know how many numbers to add or subtract before continuing with the loop. This expression could be an increment operation such as ++Count.
Here is an example that applies the for statement:
The Start expression is a variable assigned the starting value. This could be Count = 0;
The End expression sets the criteria for ending the counting. An example would be Count < 24; this means the counting would continue as long as the Count variable is less than 24. When the count is about to rich 24, because in this case 24 is excluded, the counting would stop. To include the counting limit, use the <= or >= comparison operators depending on how you are counting. The Frequency expression would let the compiler know how many numbers to add or subtract before continuing with the loop. This expression could be an increment operation such as ++Count.
Here is an example that applies the for statement:
#include <iostream>
void main()
{
for(int Count = 0; Count <= 12; Count++)
cout << "Number " << Count << endl;
getch();
}
The C++ compiler recognizes that a variable declared as the counter of a for loop is available only in that for loop. This means the scope of the counting variable is confined only to the for loop. This allows different for loops to use the same counter variable. Here is an example:#include <iostream>
void main()
{
for(int Count = 0; Count <= 12; Count++)
cout << "Number " << Count << endl;
cout << endl;
for(int Count = 10; Count >= 2; Count--)
cout << "Number " << Count << endl;
getch();
}
Some compilers do not allow the same counter variable in more than one for loop. The counter variable’s scope spans beyond the for loop. With such a compiler, you must use a different counter variable for each for loop. An alternative to using the same counter variable in different for loops is to declare the counter variable outside of the first for loop and call the variable in the needed for loops. Here is an example:#include <iostream>
void main()
{
int Count;
for(Count = 0; Count <= 12; Count++)
cout << "Number " << Count << endl;
cout << endl;
for(Count = 10; Count >= 2; Count--)
cout << "Number " << Count << endl;
getch();
}
Thanks,
Sidh.
0 comments:
Post a Comment