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

Bhargavi said:   1 decade ago
Since we use break statement it automatically comes out of the else loop, no matter whether it satisfies if condition or not,

Dileep said:   1 decade ago
Thanks to all of you.

Ramnayan said:   1 decade ago
Here for loop executes 06 times when control enters to loop untill i<5 continue keyword is executeddue to which it skips all the code below the continue regain increment the 'i' when i just reach the 5 else is executed and break keyword through the control just outside the loop in this way not printf statement is executed.

Prasanth Reddy.d said:   1 decade ago
Since x is an integer it has a range from -32768 to +32767.Here x value is -1.x checks the condition x<=10 and enters into for loop and then checks that x<5.since it is true continue; statement executes and since no stmt's are there in if condition, it exits from if loop.

Then x gets incremented and goes as 0,1,2,3,4.It continues as above upto x=4.

when x=5, it goes to else condition and executes the stmt break;
finally coming out of the else condition and x gets incremented to 5,6,7,8,9,10.
Thus by not printing IndiaBIX.

S.sivaraj said:   1 decade ago
Yes I agree above this answer.

Varsha said:   1 decade ago
I agree with Sanjeev kumar, thanks.

Ajeshbabu said:   1 decade ago
If x is <5 ,if condition will executed, other 6 then loop process is terminated, then return 0 will occur,then final result get 0 times loop occur

Ashu said:   1 decade ago
Till x is < 5, the if condition is satisfied and continue is encountered so the loop continue execution without executing the following statements. and when the value of x becomes 5, break is encountered and the control is transferred outside the loop. So 'IndiaBix' will not be printed even once.

Owais dar said:   1 decade ago
The value of x starts from -1 and is incremented everytime till x<5. becux the continue statement sends the control back to increment process. And once the value of x reachs 5 it executes else part, which is break statement, break statement terminates loop. This means the control will never encounter printf statement. Hence no 'IndiaBix' will be printed.

Sanjeev kumar said:   1 decade ago
Basically x is initialized with -1. As x<5 (since x is -1) it will came with continue statement.

Continue means "stop the current iteration and go to the next iteration". So x becomes 0 now. This will be happened until x becomes 5. Now if x=5, it will enter the else part where it encounters break statement, as a result it will come out of for loop. As a result it will not go to printf statement. So IndiaBIX will be printed 0 times.


Post your comments here:

Your comments will be displayed after verification.