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.

Yogesh Sharma said:   1 decade ago
The printf Code Line is unreachable in the function

As The control from the if Statement is revert back to For loop increment till x<5 and as it become greater than 5 it stop executing the program.

That is in any circumstances the Printf Code is not executing.

Hence Option C is correct

Guna said:   1 decade ago
The program with using continue we get result.

But if using any body of while, do, for break statement (true or false) not get out put.

Kalaivani said:   1 decade ago
Else statement should not be followed by break;.

If break is used in else statement it returns nothing.

RAJASHEKER.T said:   1 decade ago
Whenever x=5, it goes to else part.
In that else part break statement is there.
So break statement, breaks the forloop and come out of the loop.
So there is no output.

Samikshya said:   1 decade ago
thanks to everyone

Satti babu said:   1 decade ago
It will continue every time when x<5 that means it goes into again starting loop then when it is greater than 5 it will break that means exit from loop and gives out put 0 times.

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.


Post your comments here:

Your comments will be displayed after verification.