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

Ashutosh chaturvedi said:   1 decade ago
For x= -1 to x=4 in for loop the control will be passed to if statement and if statement contain the continue statement and the purpose of continue statement is to redirect the control to the beginning of the loop so printf statement is not executed. And for x=5 to x=10 the control will be passed to else statement containing the statement break which will redirect the control out of the loop so, here as well printf will not get executed. So, overall "indiabix" will be printed 0 times.

Ritesh said:   1 decade ago
The code should be like as under
#include<stdio.h>
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5)
continue;
printf("IndiaBIX");
else
break;

}
return 0;
}


And output shd be like that
IndiaBIX
IndiaBIX
IndiaBIX
IndiaBIX
IndiaBIX

Suvidha said:   1 decade ago
Else loop is not executed because break statement is encountered first and break statement will terminate you from that else loop thats why nothing gets printed. When your true statement gets executed for 5 times and when true condition fails it comes to else part but in else the first statement is break so it throws you out of the loop.

Samaleswari Prasad Nayak said:   1 decade ago
When the condition of for loop is true, the control will enter to the for block. Again when the condition of if is true it will enter the if block. But as it will encounter the continue statement, the control will move to the next step i.e. it will continue its execution. After that when the control will enter to the else block it will encounter the break statement. So control will come out of the loop.

So 0 times is the answer.

Santosh Waghmode said:   1 decade ago
When control is passes to the else block then it found the break statement and the BREAK statement pass the control to the last statement so the message in the printf() function is not printed even one time.

Utsav said:   1 decade ago
#include<stdio.h>
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5)
printf("\nIndiaBIX");
else
break;
printf("\nIndiaBIX");

}
return 0;
}

Indiabix is printed 12 times. why ?

Raviteja Kokkula said:   1 decade ago
In the above program the "continue" statement is like

if (x < 5) continue;

this continue statement will take the compiler to continue without further steps and it will go for the "for loop" and it increment the i value until the condition like if (x = 5) and then the compiler execute the ELSE statement i.e. "break;" here the compiler will not reach the "print statement", so it will not execute the printf ("IndiaBIX") ;.

Sriram kumar said:   1 decade ago
If x is <5, if condition will executed, other 6 then loop process is terminated, then return 0 will occur, then final result get 0 times loop occur.

Nithin said:   1 decade ago
Will break and exit statements work similarly ?

Hitesh Soni said:   1 decade ago
Continue takes the control to the beginning of the loop. Therefore as far as x<5, nothing happens.

As soon as value of x is 5,break statement takes the control outside the loop body.


Post your comments here:

Your comments will be displayed after verification.