For loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

For loop

is it necessary to put values in all three fields i.e init,condition and increment in for loop?

7th Jan 2017, 12:17 PM
Sumit Kumar Pandit
Sumit Kumar Pandit - avatar
4 Answers
+ 17
no, but then the loop will run continuously
7th Jan 2017, 12:24 PM
Frost
Frost - avatar
+ 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.
7th Jan 2017, 12:31 PM
Leon
Leon - avatar
+ 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 )
7th Jan 2017, 12:58 PM
visph
visph - avatar
0
not all three fields , but any 2 or 1field
7th Jan 2017, 12:29 PM
Sumit Kumar Pandit
Sumit Kumar Pandit - avatar