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

Dhaval Chandnani said:   1 decade ago
When the the loop will run first time the value of x will be '-1'.

As 'if' condition checks whether x<5, this condition will be true as -1<5, and always the loop will be continued, until x is not equals to 5 (x!=5).

When x equals to 5 the 'if' condition get false and the 'else' condition will be executed, in else condition the break statement will be executed and ultimately it will move the control out of the loop resulting into unexecution of 'printf' statement throughout the loop.

Vipin dhawan said:   1 decade ago
In above statement first (b++) the value remains 5,

Second (++b) the value gets increase by one so now value is 6, third (++b) the value gets increase by one so now value is 7, fourth (++b) the value gets increase by one so now value is 8.

But the answer is come 26.

Sudharsan said:   1 decade ago
The continue statement does not allow to execute statement that present after the continue statement and it allows next iteration.

Murugesan said:   1 decade ago
@Rohit and Aminul.

Let I explain your program o/p,

b=5 this is the value given in the program, if 'b++' the value will not get increase, in case of '++b' the value will get increase by one.

int c = ((b++) + (++b) + (++b) + (++b));

in above statement first (b++) the value remains 5,

Second (++b) the value gets increase by one so now value is 6, third (++b) the value gets increase by one so now value is 7,fourth (++b) the value gets increase by one so now value is 8.

In C the last incremented value will apply to both side of variables. so fourth (++b) value 8, will apply to third (++b), now third (++b) value change to 8, second (++b) value remains 6, first (b++) value remains 5.

So 5+6+8+8 = 27.

Hope you understand.

Priyanka Rawat said:   1 decade ago
Yes, the control does not go to print statement as before it break statement is given, and if we remove break keyword the word will get get printed 6 times... -1, 0, 1, 2, 3, 4.. as x<5 is given.

Aarushi said:   1 decade ago
#include<stdio.h>
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5)
printf("hi");
else
break;
printf("IndiaBIX");
}
return 0;
}

The output is:
hiIndiaBix
hiIndiaBix
hiIndiaBix
hiIndiaBix
hiIndiaBix
hiIndiaBix

Why?

Arnav Bijalwan said:   1 decade ago
Continue statement transfers control to next iteration. Hence when condition (x<5) gets true, control jumps to next iteration. When x becomes 5, condition in if statement becomes false and it executes else part where it encounters break statement and jumps out of loop.

Hence control never encounters the printf statement and thus, it is executed 0 times. Hence the option C is correct.

Mansi sharma said:   1 decade ago
x is declared as integer so it can not be initialised as x=-1 so program won't run.

ANJUM said:   1 decade ago
There is no {} for if or else, so the statement next to if is continue which starts the next iteration when if condition becomes false control moves to else then to break which takes the control out of loop and program terminates.

Dharmesh said:   1 decade ago
For printing the statement either it should be in if or in else block.


Post your comments here:

Your comments will be displayed after verification.