• C

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++
searchsanjaysharmaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
Either you or Turbo C++ must have done something wrong.
0
searchsanjaysharmaAuthor Commented:
Hi ozo,

You may try with your expertise and check , since i found this an exception, hence i posted that.
0
ozoCommented:
Neither case should print Hello at all.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

searchsanjaysharmaAuthor Commented:
Yes, its there, but when u execute it doesnt happen.
0
ozoCommented:
What is where and what doesn't happen when u execute what?
0
Julian HansenCommented:
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.
0
frankhelkCommented:
I agree with ozo - neither case should execute the inner part of the for loop, because the condition in the for statement doesn't allow it.

Maybe the Turbo C compile is somewhat crappy at that point when optimizing the code. In case 2 a good compiler would just omit the entire loop when optimizing, because there's no chance for the code inside the loop to get executed. In case 1 it would require a real wizard of optimizer, because that's not that simple to see for a non-human intelligence :-)

Have you tried to disable any optimizations and to fully rebuild the project (deleting project output and not usig incremental build) ?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evilrixSenior Software Engineer (Avast)Commented:
>> 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! :)
0
sarabandeCommented:
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
0
searchsanjaysharmaAuthor Commented:
tx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.