2

I'm new to c++ and programming word, and I'm learning bout For Loop. Now I have studied that In loop works if there are conditions and increment in the body of loop like:

for(int i=10;i<15;i++)

But i have seen a code on some website and it was like this:

for(;NULL;)
{
cout<<"hello";
}

Now, some one can please explain what's it's output and how it works?

2
  • This does an explicit conversion of Null (whatever that variable is) to bool and uses that as the condition. I.e. it's equivalent to a while. Commented Mar 12, 2019 at 9:27
  • Was it Null or NULL? If the former, you'll need to add what Null means (is it a variable? a macro? something else?) Commented Mar 12, 2019 at 9:28

4 Answers 4

4

In C++, all 3 expressions controlling the for loop are optional. This makes the language powerful. In your case you're missing the expression that initialises (typically) a loop counter, and the expression that's typically used to increment a loop counter. What you have is the stopping condition.

If Null is something that has an contextual conversion to a bool type then the program will compile. Otherwise it won't. Let's assume that it does:

If that converted bool value is false then the loop body never runs.

If that converted bool value is true then the loop body will run. (Note that it's possible that the conversion might change the converted object, so perhaps false is eventually yielded so the loop does eventually terminate!)

Sign up to request clarification or add additional context in comments.

2 Comments

The beauty of user defined conversions is that they can also be nasty little things that mutate the converted object. So the forever part is only likely.
@StoryTeller: Indeed, I've pinched that.
1

for loop construct can be generalized to the following.

for(initializationStatement; testExpression; updateStatement) {
    // codes 
}

In the code example that you've given initializationStatement & updateStatement have been omitted i.e. they do nothing.

for loop gets executed until the testExpression is true. In C/C++ any expression that gets evaluated to 0 is false and anything that gets evaluated some other value is true. Null in this case is this case is presumably convertible to bool and yields false. Since the testExpression is false the loop never gets executed.

2 Comments

We don't know what Null is. My answer is an attempt to cover all bases.
Correct. Thanks @Bathsheba. I made an edit to my answer.
0

Based on the condition your loop will be executed, now your condition is NULL and Null means there is no condition present so it will not take anything if it's condition itself is Null so it just skip from the loop and it will not print anything.

Comments

0

The condition for which the loop should run itself is Null. Thus the for-loop will not execute. You should have at least one valid condition so that for-loop gets executed once and print hello

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.