C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - General Questions (Q.No. 1)
1.
How many times "IndiaBIX" is get printed?
#include<stdio.h>
int main()
{
    int x;
    for(x=-1; x<=10; x++)
    {
        if(x < 5)
            continue;
        else
            break;
        printf("IndiaBIX");
    }
    return 0;
}
Infinite times
11 times
0 times
10 times
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
142 comments Page 11 of 15.

Vipin dhawan said:   1 decade ago
In above statement first (b++) the value remains 5,

Second (++b) the value gets increase by one so now value is 6, third (++b) the value gets increase by one so now value is 7, fourth (++b) the value gets increase by one so now value is 8.

But the answer is come 26.

Dhaval Chandnani said:   1 decade ago
When the the loop will run first time the value of x will be '-1'.

As 'if' condition checks whether x<5, this condition will be true as -1<5, and always the loop will be continued, until x is not equals to 5 (x!=5).

When x equals to 5 the 'if' condition get false and the 'else' condition will be executed, in else condition the break statement will be executed and ultimately it will move the control out of the loop resulting into unexecution of 'printf' statement throughout the loop.

Hemant Sharma said:   1 decade ago
Let's think what's compiler reads this program.
Since when i<5 it skips the remaining part and when i>=5 it breaks the loop and exit the function. As no print command outside the loop so there is 0 times print, that's it.

Wester9208 said:   1 decade ago
#include<stdio.h>
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5) // here this is true for x=-1
continue;//so this is excuted
else //next this is executed
break;//the moment it reaches here, the break //statement gets the control out of for loop
printf("IndiaBIX");//so this statement not printed
}
return 0;
}

------------------------------------------------------
So according to that it executes 0 times.

Swarup said:   1 decade ago
Since the continue transfers the flow of program at the starting of the loop and break statement transfers the flow of program outside the loop it will be printed for 0 times.

Bulbul said:   1 decade ago
We can not use continue with if statement.

Sam said:   1 decade ago
Continue always takes the control back to the beginning of the loop and thus the value of x keeps on incrementing until it reaches the value 5. Once it becomes 5, the if condition fails and thus else comes into the picture. In the else block there is a break statement and hence entering into else makes the control to come come out of the for loop and point to the very next statement outside the for loop. Since there is no other printf statement. The whole of the program does not print IndiaBix.

Kondorpa said:   1 decade ago
can anyone please explain me the output?

#include<stdio.h>
main()
{
int p=5;
printf(" %d %d %d ",p,p++,++p);
}

Output is, as per indiabix.com compiler is 7 6 7

Ashish Sawant said:   1 decade ago
Here for(x=-1), therefore the IndiaBix is printed 0 times.

Uday kumar said:   1 decade ago
@Murugesan @Vipin dhawan.

I has little bit query you guys answered for @Rohit question.
When it is b++ there will be no change in the value.

for ++b the value get increased by 1.
OK.

Let us take you are right for a while then,
for(i=-1;i<10;i++)

In this case what will be occur? I mean what will happens here for i++ whether it will increase or won't change.


Post your comments here:

Your comments will be displayed after verification.