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

Amol said:   1 decade ago
The continue statement takes the control out of the loop for the next iteration and the break terminates the loop.

Srinu said:   1 decade ago
Break and continue statements does not allow to execute printf statement.

Ayodhya Prasad said:   1 decade ago
Since loop will execute until condition is true. After break it will not execute. Hence it will not print the IndiaBix.

Harsha said:   1 decade ago
When break is encountered in loop then the control directly jumps to the first statement after the loop.

Vishnu o.m said:   1 decade ago
Indiabix is not printed because the continue instruction will take the control back to the for loop until i<5, till then printf don't get executed. When if statement fails, else statement break is executed which will take the control out of the loop. Hence printf is not executed.

K. Parthi said:   1 decade ago
The statement will be executed until x IS 5 THEN it terminates the loop by break, prints return 0.

Manikanta said:   1 decade ago
0 times print because---x value initialized with -1 and it is <=10 now the control enter into loop it checks the if condition it is also satisfied (i.e <5).

Then it executes the statement continue. Now the control will go to the for loop and again incremented this during these iterations the control will never goes to the print statement after some iterations if conditions fails then the control goes to the break then the loop will break again x value incremented and if condition fails, goes to break and process repeats during these iterations also the control never goes to the print statement.

Nazia firdous said:   1 decade ago
If this program having print statement outside the main then it will print one time.

Otherwise if in else part break will not be there then it will print. We have to write this prog in different way then it will print.

K v ajay kumar choudhury said:   1 decade ago
Generally continue is used to skip the statements and break is used to terminate the statements.

When continue is executed then control is passed back to the condition and when break is executed control will come out of loop body.

So here in the above program till x=-1 to 4 continue will be executed and control will be passed back to loop condition.

Now when x becomes 5, the if condition becomes false and it will come to else part without executing continue statement. In else part we have break statement.

So when break will be executed, control will come out of the loop body (i.e rest of the statements will be terminated i.e. printf statement) and hence prinntf will not be executed.

Pranab Nandi said:   1 decade ago
The condition x<5 is true until x=5, so the true block executed. The code after the continue statement(including printf("IndiaBIX");) will not executed and go to increment step(x++). Again the loop is continued.

When x=5, then else block executed, it has break statement, So loop is terminated.

Thus "IndiaBix" Will never get printed.


Post your comments here:

Your comments will be displayed after verification.