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

Divyasree said:   1 decade ago
control never goes to printf("IndiaBix"); statement.. So there exists compilation error.. Hence the solution is 0 times

Anita said:   1 decade ago
The "Continue" Statement Takes the control to the beginning of the loop,So When ever value of if(x<5) continue appreas,control goes to begining of loop and value of X is incremented.

"Break" Satatement takes the control out of the loop.So when it is occured,control is taken out of the Loop.

Thus "IndiaBix" Will never get printed.

Jayaprasad said:   1 decade ago
Good Ritesh...
In the above program the "IndiaBix" will be printed 12 times because...

At x= -1,0,1,2,3,4 i.e 6 times the condition x<5 is satisfied so the statement in the if block and the one after the block(in the loop level) will be printed... As both the statements are "indiaBix" it will be printed 12 times(6x2).......

After x=4 i.e at x=5,6,7... the condition x<5 is not satisfied so the control is transferred to else block which contains break; statement. here break; terminates the loop execution so nothing is printed after x=5.....

So only 12 times it is printed...

Adil saifi said:   1 decade ago
True block evaluated continuously tat is (x<5)0,1,2,3,4 until 4<5 when 5<5 condition checked the condition bcm false then the control pass to else block which will break and exit. but if u want to print the given statement just write it upon the break statement it will print 1 time indiaBIX on ur output screen.

Rohit said:   1 decade ago
Can someone please tell me why the output is 27 for the following code?

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

Agi said:   1 decade ago
int i=1
for(i=-1;i<=10;1++)
Then how it calculate -1 in for loop ?
Then how many it print?
Please explain this.

Himanshu said:   1 decade ago
Hi agi. No matter you take value of i=1 or something else only the value inside the loop will be executed. So take i=1 or 100 or 200 loop will work for i=-1.

Keshav saini said:   1 decade ago
Here printf ("IndiaBIX") ; is unreachable code. Because break keyword is stop the working of the block.

K@vy@ said:   1 decade ago
Here when the loop start if x<5 it will continue but it won't print anything otherwise if x>5 it will take else statement and it will break. If it breaks there won't be any printing. So there is no printing of indiabix.

Mayank said:   1 decade ago
Because when ever the break statement given in loop.

The loop terminates and control get out from the loop.


Post your comments here:

Your comments will be displayed after verification.