There are different syntax of for loop in cpp(c++), but in tradition the syntax of for loop in c++ or c, looks like this:
For(initialization; condition; increment)
Statement;
DIFFERENT SYNTAX OF FOR LOOP IN CPP(C++)
1. FOR LOOP w/o initialization - But do you think it is possible to remove the initialization; in for loop? If you say yes, your answer is correct, just remember declare the variable type and the initial value of that variable. Example I use x variable for the for loop:
Int x=0;
for(; x<5; x++)
printf(“COUNT[%d]”,x);
*as you notice for(; x<5; x++)
I have terminator(;
) before the x<5
, because if you remove that will get the following error:
- Code has no effect
- For statement missing
- Function should return a value
2. FOR LOOP w/o increment - Another different syntax of for loop is removing third statement which is the increment or decrement. But in-order to execute the for loop error free, you must include the increment or the decrement inside the for loop. Example:
Int x;
for(x=0; x<5){
printf(“COUNT[%d]”,x);
x++;
}
3. FOR LOOP w/o initialization and increment - Then lastly, is the removing of both initialization and the increment or in other word the combination of the example one and two. Example:
Int x=0;
for(; x<=0;){
printf(“COUNT[%d]”,x);
x++;
}
Remember there’s nothing wrong trying new thing in programming, it is much wrong if you don’t take a risk and try new things. I hope you enjoy reading my different syntax of for loop in cpp.
for(; x<5; x++) are you sure this is valid?
ReplyDelete