+ 2
For loop
is it necessary to put values in all three fields i.e init,condition and increment in for loop?
5 Respostas
+ 17
no, but then the loop will run continuously
+ 4
no, a semi colon serves to distinguish what you mean. Obviously the condition must evaluate true
for(;1==1;) cout<<"1";
//is an infinite loop.
To have more than one increment operation, use a coma to separate each.
for(int i = 0; i <= 5; ++i,++j);
notice there is no body, in many cases it can all be inside the original parenthesise.
+ 4
@S!d:
But you can do:
int i=5;
for (;;) {
cout << "Hi" << endl;
i--;
if (i==0) break;
}
( don't ask me why, apart to prove that you can, without running infinitly :D )
0
not all three fields , but any 2 or 1field