Link to home
Start Free TrialLog in
Avatar of searchsanjaysharma
searchsanjaysharma

asked on

How does conditional part work in for loop.

Case 1:
main()
{
      int a=0;
      for(;a;)
      {
            printf("Hello");
      }
}
Output: /*It doesn't print Hello*/

Case 2:
main()
{
      for(;0;)
      {
            printf("Hello");
      }
}

/*prints hello for one time*/

Why this kind of output comes in Turbo C++
Avatar of ozo
ozo
Flag of United States of America image

Either you or Turbo C++ must have done something wrong.
Avatar of searchsanjaysharma
searchsanjaysharma

ASKER

Hi ozo,

You may try with your expertise and check , since i found this an exception, hence i posted that.
Neither case should print Hello at all.
Yes, its there, but when u execute it doesnt happen.
What is where and what doesn't happen when u execute what?
Try compiling to assembler and see what the output is - that should give you some idea of what it is doing.

Can confirm this does not happen in other compilers.
ASKER CERTIFIED SOLUTION
Avatar of Frank Helk
Frank Helk
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> Why this kind of output comes in Turbo C++
There are only two possible reasons:

1. The code example you've provided isn't actually what you're building and running
2. Your compiler is defective

I very much doubt it's number 2 so I have to conclude that it's number 1.

Put simply, I concur with ozo.

Please copy and paste, verbatim, the code you are building.

>> Can confirm this does not happen in other compilers.
Phew! :)
i once had a semicolon ;  after the for statement.

for(;<some_lengthy_condition_which_was_false>;);
{
      some_code();
}

Open in new window


and some_code() function was called once.

it took me some time to find out what was wrong ...

Sara