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

SAGAR RANPISE said:   1 decade ago
Here in the program, we have a integer variable. And a integer variable has the tendency to convert negative number. i.e. "signed number" into a positive no. i.e. "unsigned number". due to which the value -1 after converting into a positive no obviously becomes greater than 5 ;

And as there is a break statement after the 'else' statement the program terminates.

Nitin sagar said:   1 decade ago
Once if statement is false.

The control will move to the else block and the break statement.
Present in the else block will pass the control out of the body of the loop,

So nothing gets printed.

Bhavin Patel said:   1 decade ago
If condition of the for loop becomes false then loop will be terminated. So, INDIABIX will print 0 times.

Chirala anil kumar said:   1 decade ago
In this program increment the x value up to x = 5 after that if condition false come out of the loop. Nothing printed.

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.

Premalatha said:   1 decade ago
Guys please anyone can explain me!!!

void main()
{
int i;
i=1;
i=i+2*i++;
printf("%d",i);
}

Output = 4.

Aliimran224 said:   1 decade ago
Here,

i = i+2*i+1 [Because i++ = i+1].

Now i = 1+2*1+1.

i = 4(Ans).

Madhu Chandra said:   1 decade ago
The continue statement under if condition will keep on running the executing for loop by increment 1 and then after 6 iterations the x value becomes 5 by increment 1 and in 7th iteration it will fail, then go break out from loop.

Jayapraksah said:   1 decade ago
When the condition is true it will skip the further program by continue. If the condition getting false it will get off the block by executing break statement.

Abhishek Kumar said:   1 decade ago
The if block will execute upto x=4, when it is x=5, then the if statement become false and it will go to else block, where break statement is given, the break will terminate the program and takes the control out of the program.

Therefore, there will be no output for this program. Option (C) is correct.


Post your comments here:

Your comments will be displayed after verification.