+ 1
How to apply for loop condition in any programme
3 Answers
+ 1
for (VariableDeclaration; Condition; VariableChange)
e.g.
for (int i = 1; i < 10; i++)
std::cout << i;
//prints 123456789
+ 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)
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)