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 4 of 15.

Nitin sagar said:   1 decade ago
Once if statement is false.

The control will move to the else block and the break statement.
Present in the else block will pass the control out of the body of the loop,

So nothing gets printed.

SAGAR RANPISE said:   1 decade ago
Here in the program, we have a integer variable. And a integer variable has the tendency to convert negative number. i.e. "signed number" into a positive no. i.e. "unsigned number". due to which the value -1 after converting into a positive no obviously becomes greater than 5 ;

And as there is a break statement after the 'else' statement the program terminates.

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.

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

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

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.

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

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.

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.

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.


Post your comments here:

Your comments will be displayed after verification.