+ 1

How to apply for loop condition in any programme

8th Oct 2016, 2:48 PM
Madhav Kotnala
Madhav Kotnala - avatar
3 Answers
+ 1
for (VariableDeclaration; Condition; VariableChange) e.g. for (int i = 1; i < 10; i++) std::cout << i; //prints 123456789
8th Oct 2016, 3:44 PM
Cohen Creber
Cohen Creber - avatar
+ 1
example cases where for loop can be applied: 1. when going through an array or a vector (if you just learned for loop, they will come soon) -> game example: going through player's inventory to check if he has an apple 2. when you want to do something an exact number of times -> game example: producing the same monster 5 times at a position (I use game examples because people have easier time understanding them, but both of these are applied everywhere)
8th Oct 2016, 4:05 PM
dragonbro1015
dragonbro1015 - avatar
0
* syntax * for (<start>; <bool expression>; <action>) Ex: for (counter = 1; counter <= 500; ++counter) { cout << counter << endl; } This example counts from 1 to 500 Note: all fields are optional but the semicolons are not (you must include the semicolons)
8th Oct 2016, 6:52 PM
Luis Cervantes
Luis Cervantes - avatar