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

Rohan said:   7 years ago
@All.

According to my knowledge.

#include<stdio.h>
int main()
{
int x;
for(x=-1; x<=10; x++)

/* (i) for loop is initialized with x=-1 and condition x<=10............... */
{
if(x < 5)
continue;

/* (i) First time ->
if (-1<5) , condition true -> continue statement transfer the control to for loop...
(ii) Last time ->
if (5<5) , condition false -> the else part will execute....
*/

else
break;

/* (i) -> the break statement make sure the for loop is terminated.........
(ii) -> it will not print IndiaBix because for loop has been terminated.............. */

printf("IndiaBIX");
}
return 0;
}
(5)

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.

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.

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...

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.

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.

Nitika said:   1 decade ago
Till the time x is less than 5 it will satisfy the if condition and will move to continue. And we know that when continue is encountered the loop moves to its starting i.e. here it will go back to for loop.

And when if condition will fail i.e when x>5 the else part will be executed according to which break statement will be encountered and when break is encountered the loop moves to the first statement after that loop.

Therefore here it will go out off or loop. And hence printf will never get executed.

Wester9208 said:   1 decade ago
#include<stdio.h>
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5) // here this is true for x=-1
continue;//so this is excuted
else //next this is executed
break;//the moment it reaches here, the break //statement gets the control out of for loop
printf("IndiaBIX");//so this statement not printed
}
return 0;
}

------------------------------------------------------
So according to that it executes 0 times.

Sam said:   1 decade ago
Continue always takes the control back to the beginning of the loop and thus the value of x keeps on incrementing until it reaches the value 5. Once it becomes 5, the if condition fails and thus else comes into the picture. In the else block there is a break statement and hence entering into else makes the control to come come out of the for loop and point to the very next statement outside the for loop. Since there is no other printf statement. The whole of the program does not print IndiaBix.

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.


Post your comments here:

Your comments will be displayed after verification.