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.

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.

Pes university said:   1 year ago
#include<stdio.h>
void main()
{
int s=0;
while(s++<10)
{
if(s>3&&s<10)
continue;
printf("\n%d\t",s);
}
}

*/Here it is usually you're giving s=0 and then s++<10;means s++=1;

So, condition true go to the while loop that will print 1 2 3 and then you're giving that s>3 and s<10 continue it will continue until the last element that is 9 if you are given as here then it will print the 1 2 3 9 but you given s++.

So, the output is;
1
2
3
10
*/

Sanjeev kumar said:   1 decade ago
Basically x is initialized with -1. As x<5 (since x is -1) it will came with continue statement.

Continue means "stop the current iteration and go to the next iteration". So x becomes 0 now. This will be happened until x becomes 5. Now if x=5, it will enter the else part where it encounters break statement, as a result it will come out of for loop. As a result it will not go to printf statement. So IndiaBIX will be printed 0 times.

Manish NIITian said:   1 decade ago
First you know that how to work Break and Continue statement :.

If you use 'continue' statement then that after particular condition loop will be continue in same loop and another break statement, if you use 'break' statement then terminate of that loop and go to out of loop and program will be exit.

So in this program, println ("IndiaBix") ; is used after break statement so this program is terminated. So no display any output.

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.

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") ;.

Arnav Bijalwan said:   1 decade ago
Continue statement transfers control to next iteration. Hence when condition (x<5) gets true, control jumps to next iteration. When x becomes 5, condition in if statement becomes false and it executes else part where it encounters break statement and jumps out of loop.

Hence control never encounters the printf statement and thus, it is executed 0 times. Hence the option C is correct.

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.

Srinivasa.S said:   2 decades ago
The condition x<5 is true until x=5, so the true block executed. The code after the continue statement(including printf("IndiaBIX");) will not executed and go to increment step(x++). Again the loop is continued.

When x=5, then else block executed, it has break statement, So loop is terminated.

Here the control never go to printf("IndiaBIX"); statement.
(1)

Seghrajeshwar said:   7 years ago
Hello ,please give me the suggestion for this.

#include<stdio.h>
int a,b,z,c;
float x=0.00,y;
float value=200;
void main()
{
x=value/200;
a=x;
y=x-a;
b=23*y;


for(z=0;z<23*a;z+=23)
{
printf("\n Z:%d ",z);
}
if(z<=z*a)
{
c=z;
z=c+b;

In a above code, I want a 23 value print on printf line. Now I am getting the 0 value. How to predict it? please tell me.
(5)


Post your comments here:

Your comments will be displayed after verification.