-2

I am bothered by a question of for loop in C language.

when i write:

#include<conio.h>
#include<stdio.h>
main()
{
    int i = 0;
    for( ; i ; )
    {
        printf("In for Loop");
    }
    getch();
}

Output: NOTHING PRINT.

Code get executed, but printf statement didn't print due to condition. OK, No problem here.

But when i write this code:

#include<conio.h>
#include<stdio.h>
main()
{
    for( ; 0 ; )
    {
        printf("In for Loop");
    }
    getch();
}

Output: In for Loop.

My for loop get executed 1 time, but actually it must not to be executed. I don't know why? can coder/programmer/hacker of stackoverflow help me. explain me please why my for loop giving this output only one time.

14
  • 1
    I cannot reproduce this behavior. Compile without flag with g++.
    – nhahtdh
    Aug 7, 2012 at 14:47
  • nothing is printed in the second loop for me. Used Apple LLVM 3.1
    – Andrew
    Aug 7, 2012 at 14:47
  • How are you compiling your program? Also: Would be nice if you pasted the entire second version of the program, exactly as it is, instead of just the loop. Aug 7, 2012 at 14:50
  • can't reproduce the problem on gcc and clang on linux.
    – Aftnix
    Aug 7, 2012 at 14:52
  • 2
    Possible duplicate: stackoverflow.com/questions/11086759/…
    – tinman
    Aug 7, 2012 at 15:18

3 Answers 3

12

What you wrote shouldn't print anything, but I suspect the actual second-case code (not what you typed in) that causes the problem is:

for( ; 0 ; );     // <==== note the trailing semicolon there.
{
    printf("In for Loop");
}

In this case the for loop doesn't execute the empty statement, and then the { } code is executed once.

EDIT: If this isn't the problem please just paste a complete program that exhibits the problem directly into your question.

EDIT2:

The following minimal compilable example doesn't print anything:

#include <cstdio>

int main()
{
    for( ; 0 ; )
    {
        std::printf("In for Loop");
    }
}
14
  • 3
    Every programmer runs in this problem at least once in its lifetime
    – akappa
    Aug 7, 2012 at 14:50
  • Yeah. I'd bet money on this one, not just because it would have the behaviour described, but also as @akappa says, everyone's done it at least once. (And if you find cases where empty for loops are useful, you'll do the opposite at least once too).
    – Jon Hanna
    Aug 7, 2012 at 14:52
  • 1
    When I need an empty for loop I still use braces and a big fat comment saying that it's a null loop body.
    – Mark B
    Aug 7, 2012 at 14:55
  • 8
    @MohdIftekharQurashi You don't make any silly mistakes in coding? You'd be the only one in the whole world. Aug 7, 2012 at 14:56
  • 2
    Link to runnable code example
    – JoeFish
    Aug 7, 2012 at 15:04
1

Both ways should output nothing.

0
0

I can't replicate this behavior on my machine, compiling with gcc. The two programs were:

#include <iostream>
int main()
{
  for( ; 0 ; )
  {
    std::cout << "Here I am!";
  }
  std::cout << "End of the program.";
}

outputs

End of Program

as does

#include <iostream>
int main()
{
  int i = 0;
  for( ; i ; )
  {
    std::cout << "Here I am!";
  }
  std::cout << "End of Program";
}

This is what we would expect to happen, as the 0 read as the loop continuation condition is evaluated to false, so the loop is never entered.

3
  • But really believe me. my turbo c++ 3.0 gives me my output. your gcc may give correct answer as expected. Aug 7, 2012 at 15:24
  • 1
    @MohdIftekharQurashi Damn, that's a really old compiler. Does it run on coal? Aug 7, 2012 at 15:26
  • @MohdIftekharQurashi It's a bug in TurboC++-3.0. See here You should choose a better compiler. Aug 7, 2012 at 15:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.