FOR loop in C, condition part

1.1k views Asked by At

Small question just to clarify.

In ZX-Spectrum BASIC compiler there is FOR TO STEP NEXT looping, where TO is unconditional:

10 FOR i=1 TO 5 STEP 1

I've tried similar practice in C

for (i=1; i==5; i++); 

and the of course loop does not work (== is never true here)... so the question is:

Is in C FOR loop we should always use a CONDITION to stop the loop (I mean CONDITION in parentheses of the FOR statement), like FOR (i=0; i<6; i++);

6

There are 6 answers

7
tckmn On

You could use

for (i = 1; i != 6; i++); 

But it would be better to use i = 0 ... i < 5, in case you alter i inside of the loop. It also communicates what you are doing better.

0
paxdiablo On

The correct translation of:

for i = 1 to 5 step 1

would be:

for (i = 1; i <= 5, i++)

In other words, a loop running five times with the control variable set to 1, 2, 3, 4 and 5 on sequential iterations.

There are other variations you could use such as different conditional operators and different termination values, but the one shown most matches the BASIC variant while still protecting you in the case where your step may be more than one (such as for i = 1 to 4 step 2).

Keep in mind that C arrays are 0-based so, if you're using that i to access an array, it needs to run from 0 to n-1, not 1 to n. If you just want the variable for other purposes (such as printing out the numbers one through five inclusive), the 1..n variant is okay.

0
ranger101 On

Loop conditions are not necessary you can write loops like

for(int i=0;;i++)
{
//body
}

but note that body of the loop should contain some break statement else loop executes infinite time

0
shalchianmh On

There are two problems in your code, you shouldn't use a ; after your loop head, and you can use < or <= in your condition part like this:

for(int i=1; i<=5; i++){}

so it should be a condition but you do not have to use an inequality you can use any other condition but it should be finished a time

0
Rahul Goyal On

There is no any restriction that you must use the conditional statement in the for loop..
You also use this conditional statement in the body of for loop as follow....

for(int i=0;;i++)
{
      if(i>=5)
      {
         break;// to break the loop
      }
}

if you not use the conditional statement at in the for loop or in the for loop body then the loop goes into the infinite state as follow....

for(int i=0;;i++)
{
    //any statements
}

Because In the for loop all three part are optional (initialization , conditional , incri/decri)

int i=0
for(;;)
{
      if(i>=5)
      {
           //any statements
      }

      i++
}
0
nischal sharma On

yes, the condition must have "<"or ">" or ">=" or "<=" as we need to apply a limit to the loop. when you use "==" the loop will test if the number is equal to 5 or not and of course 0 is not equal to 5 and hence the loop will be terminated. where as when you use "<" the loop will check if the value of "i" is less than the 5 or not. Also you have put ";" after the for loop statement which is incorrect since, any of the statement inside the for loop braces wont be executed.