You absolutely can write such a condition.
It just doesn't make sense.
Here's the broken syntax for
loop:
for(<initial statement>;<boolean expression - keep going while true>;<statement performed after each iteration>)
So, in a typical scenario that you described
for (int i = 0; i <= 10; i++) k++;
the initial statement is declaration i
. The loop will work until i
remains 10 or less , and after each iteration i
will increase by one.
To summarize - you can write i == 10
, it just wonβt make much sense, because the loop will not work, since i
already 0. If you replaced the initial condition with int i = 10
then it will only start once . You could also write i != 10
as a condition, and then the loop will execute 10 times.
source share