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

Siva said:   9 years ago
If condition false means it should be executed the else statements?

Then why is not printed one time? Can anyone explain to me?

Ashish said:   9 years ago
Once the value of x reaches 5 it executes else part, which is break statement, break statement terminates loop. This means the control will never encounter printf statement. Hence no 'IndiaBix' will be printed.
(1)

Sri Harsha said:   9 years ago
Continue statement in the loop will not allow to print.

Because when the continue statement encountered by the c compiler it simple goes to the beginning of the loop.

The x gets incremented up to 5 and after that loop will be braked.
(1)

Ram said:   9 years ago
Thank you @Ankit.
(1)

Ankit Kumar said:   9 years ago
When x=-1 to x=4 in this case, continue statement executed by increasing value of x only not go to print statement

When x=5 in this case break statement executed and loop will terminated

So zero times print statement executed.
(3)

Bhavna Bhoir said:   9 years ago
Break statement is used to take control of execution abruptly out of the loop.

When continue statement is executed, then the control of execution simply continue to next iteration of the loop.

Akhil said:   10 years ago
I. Break statement "STOP's" executing the loop.

II. Continue will execute the next instruction.

1st iteration: x = -1.

Process: if (x<5) [condition true] so "continue" executes and control goes to "else" where we break the loop and iterate again.

So the statement actually never gets printed.
(1)

Roger said:   10 years ago
After 6 loops it will exit.
(1)

Abhishek said:   10 years ago
After the break statement we come out from the loop that's why the answer should be 0 times.

Rakesh said:   1 decade ago
I have a doubt in this. Break statement in else block only exits from the else block or will it exits from the for loop?


Post your comments here:

Your comments will be displayed after verification.