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

Ramsan said:   1 decade ago
After the break statement, It will come out of the loop. Then it will not going to print IndiaBix.

Jajanpreet Singh said:   1 decade ago
ITS SIMPLE:

As soon as x reaches value 5.

The block with "break" is executed which cause the compiler to abandon the loop and HENCE ANY OF ITS FURTHER ITERATIONS (EVEN THE STATEMENT AFTER BREAK IN SAME BLOCK).

Navdeep Charan said:   1 decade ago
What will be the Output of this program and How?

#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=10;
while(++a || ++b)
{
printf("%d %d",a,b);
break;
}
getch();
}

Pooja said:   1 decade ago
Up to the x<5 condition the else statement is executed. But when x=5 then else statement is executed, but in else statement break statement is there.

Because of this execution is stop and loop is terminated. And printf function is not executed.

Thus "IndiaBix" will not print.

Karan said:   1 decade ago
When condition will satisfy means x<5 is true then break statement will be executed. And then control will not transfer on printf statement.

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.

Aminul said:   1 decade ago
#include<stdio.h>
int main()
{
int b=5;
int c= ((b++) + (++b) + (++b) + (++b));
printf("%d",c);
return 0;
}


I think the output will be 5+7+8+9 = 29.

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.

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.

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.


Post your comments here:

Your comments will be displayed after verification.