C Programming - Control Instructions - Discussion

6. 

Which of the following statements are correct about the below C-program?

#include<stdio.h>
int main()
{
    int x = 10, y = 100%90, i;
    for(i=1; i<10; i++)
    if(x != y);
        printf("x = %d y = %d\n", x, y);
    return 0;
}
1 : The printf() function is called 10 times.
2 : The program will produce the output x = 10 y = 10
3 : The ; after the if(x!=y) will NOT produce an error.
4 : The program will not produce output.

[A]. 1
[B]. 2, 3
[C]. 3, 4
[D]. 4

Answer: Option B

Explanation:

No answer description available for this question.

Kavya said: (Oct 10, 2010)  
1. The statement 1 is wrong bcz printf is not inside for loop so printf is called 1 times.

2. Since x = 10 anad y = 10. the value is printed.which is true.

3. We can terminate conditional statement with semicolon, hence if(x!=y); is allowed in c.

4. Since the program is producing o/p, the 4th statement is wrong.

Aswin said: (Dec 13, 2010)  
As We have got a ';' after if condition, it checks for equivalence of x & y for 10 times and executes the ';'.
As ';' in C is treated as a statement thats why every statement ends with a ';'.
and values of x and y are printed as 10 as they are not getting modified any where...

Sai said: (Dec 22, 2010)  
1.Actually after "if" condition there is ;(semicolon) then it becomes an infinite loop.If condition calls itself again and again till termination i.e from 1 to 10.After terminating now control will go to printf.And printf will be called for last checking condition in "if"..

Keerthana Sundaram said: (Dec 28, 2010)  
Clear explanation. Thanks.

Kiran said: (Jan 3, 2011)  
Step1: x and y are intialized to 10 and 10 repectively. 100%90 does not affect on variable Y.

step2: the for loop condition is true and it repeats upto i<10.

Step3: if condition is statisfied in for loop statement block, It will executes without any output upto the condition in for loop is fails.

step4: printf statement is executes independently which is x =10 and y = 10. which is doesn't affect on for loop.

The option 2 and 3 are correct.

Sri said: (Jan 6, 2011)  
Thanks Kavya. Very good explanation.

Daj said: (Feb 19, 2011)  
Very good kavya.

Vini said: (Apr 20, 2011)  
I have a doubt. Because the if inside the for loop is not satisfying. So how could it be. ?

Mayya said: (Apr 21, 2011)  
FOR loop has got no body..... so IF gets executed after the FOR loop... followed by printf

Siva said: (Jun 22, 2011)  
Good explanation Kavya.

Karthik said: (Jun 24, 2011)  
Good explanation kavya.

Nitika said: (Jun 26, 2011)  
Hmmm. Right kavya.

Munni said: (Jul 13, 2011)  
Nice explanation kavya.

Syed said: (Jul 21, 2011)  
Nice explanation given by Kavya

Thanks....

Ravi said: (Jul 24, 2011)  
Good explanation sai thanx a lot.

Gopinath.Nan said: (Jul 30, 2011)  
What is the meaning of y=100%90 ?

Kans said: (Aug 7, 2011)  
@Gopi

It means only the ramainder alone will be taken into account.

Imran said: (Aug 15, 2011)  
X=10, y=10 , then how if(x != y) statement will execute ?

Pall said: (Aug 22, 2011)  
X=10, y=10 , then how if(x != y) statement will execute ?pls some 1 answer asap

Dhronan said: (Aug 22, 2011)  
@ imran and pal:.

There is no problem on executing that statement, because it is terminated at every iteration of for loop. The condition inside the if statement produces 0, but that wouldn't affect the program. Because ";" terminates at every iteration.

Sangeetha said: (Sep 29, 2011)  
Correct explanation kavya.

Dayal said: (Oct 13, 2011)  
Very good kavya.

Abbas Haider said: (Dec 12, 2011)  
Can we put ';' after for loop also?

Raj said: (Jan 2, 2012)  
As if is terminated with a semicolon the compiler feel that as end of if statement, so next line printf () is executed in the program.

Vishwas said: (Jan 6, 2012)  
Simple logic is that, in c if anything is terminated by ; then it is treated as normal statement, so in this program the 'if' is treated as normal statement and there is no side effect on the variables, in other words it neglect that statement and proceeds its execution.

(1) As printf is not inside for loop only once the printf get executed....
(2) Compiler would not generate an error, how can it generate an error for simple statement?

Prashant said: (Jan 24, 2012)  
1. Here x=10, y=10 because 100 % 90 means reminder after dividing 100 by 90 (that is modulo operation). So initialy x=10 and y=10.

2. Here for loop not have curly brases so default scope of for loop is next statement of for loop means for loop contains only one statement that is if(x!=y). So printf statement is not incuded in for loop. As the contol leave the for loop it comes to printf statement and it gets executed.

Gangadhar said: (Feb 2, 2012)  
You absolutely correct kavya.

Manish said: (Feb 18, 2012)  
Excellent Kavya. !

Bhushan said: (Feb 18, 2012)  
Thanks kavya. !

Tabassum said: (Feb 19, 2012)  
Initailly the value of x=10 and y=100%90 is provided
how u all can say that the value of y is also 10 can one exaplain how its possible becoz it is modulus and that will take only remainder into picture that means it is 1 right or i dont know can any one exaplain me this in more elaboration.

Aahana said: (Feb 20, 2012)  
Since its given that x=10 and y=100%90 so the value of x will be 10 and value of y will be 10 remainder is considered in case of % operator . and since for loop does not have any scope i.e it does not contain any body so sequential execution will take place and the statement that x!=y is false inspite of that the printf statement will execute because because the condition x!=y is terminated by ;

Dana said: (Feb 27, 2012)  
2,3 comes how?

Shailendra said: (Mar 8, 2012)  
Your concept is very clear Kavya

Rekha said: (Mar 18, 2012)  
Here in if(!x<y);
So program will execute sequentially i.e after if(!x<y);statement the control will goes successfully on next line which is printf statement hence it will no produce any error and produce output x=10 and y=10.

Abhay said: (Apr 2, 2012)  
Thank for best guidance and explanation

Sramona said: (Apr 15, 2012)  
X=10, y=10, then how if (x != y) statement will execute ? please some one answer ASAP.

1828 said: (May 24, 2012)  
If (x!=y) will execute but compiler will go to next line which is.
'printf ("x = %d why = %d\n", x, y) ;' here X=10 and Y=10 will remain same as they were at declaration time.

Rob said: (Jun 29, 2012)  
if(x!=y) will not execute, it just checks whether the conditions are matching or nor not. Here in this case the condition is false and if it was true then there might be an error.

Santosh Sn said: (Aug 21, 2012)  
Thank you kavya.

Prasad said: (Aug 31, 2012)  
If a condition is followed by semicolon then compiler creates a empty body with {} after if. So it becomes an infinite loop and this loop is repeated till the for loop condition fails.

Rajsri said: (Oct 7, 2012)  
The printf() statement is under for loop or if condition?

If it is under for loop, the explanation is correct but as per the spaces given in the code, it looks so that it is under the if() , if it is under the if condition as ; terminates the if, the compiler must never go to the printf().

Am I missing something? Someone please explain?

Mohan said: (Oct 9, 2012)  
@Rajsri.

As per your point it looks good, but the spaces given below the loop and starts with Printf() statement doesn't mean that it included the for statement it's just an indentation we are following for avoiding the confusion.. Since ; (semicolon)which is at the end of IF statement which will not generate the error but it keeps looping until the statement becomes false,once it becomes false it comes to the next line and continues its processing.

Amit said: (Oct 12, 2012)  
B is true.

As in the above Code,
Till the For statement, the flow is normal & variable values are(x=10, y=100%90=10)

So after the For loop The if Condition is Checked i.e.
if(x!=y)i.e (10!=10)

Which proves to be false , it puts us in dilemma that what will be the output.
But thanks to the ; So that if loop gets terminated here only.

And Controls moves to the next line & values of X & Y gets printed i.e 10 & 10.

Kapil Deol said: (Dec 13, 2012)  
Because for will execute only 1 statement that's why it will execute only if but 'if' is terminated by ;.

Then it will print 10 10.

Saikumar said: (Jan 23, 2014)  
Because that x=10 y=1 the valve is printed is true and.

The condition if(x!=y) will not produce any error it is correct.

So 2, 3 options are correct for this above question.

Srinath said: (Mar 16, 2014)  
I don't now after if condition put semicolon in C you clearly explain thank you kavya.

Nadeem Khan said: (Apr 9, 2014)  
Can anybody tell me 100%90 will give remainder or quotient.

Because in my opinion %--> sign is used for getting remainder (Modular sign).

& /--> slash sign is used for dividing.

Bala said: (Jul 10, 2014)  
Initially the value of x=10 and y=100%90 is provided, The condition if(x!=y) then x=10, y=10 then condition is true. So always print the output.

Kavi said: (Sep 9, 2014)  
If condition does not contain semicolon (;). How its not producing error? any one please explain it.

Angelica said: (Dec 1, 2014)  
Will it run even if the for loop has no opening and closing curly brace?

Madhu said: (Dec 9, 2014)  
How can it generate option B. First of all there is an error occurred since there is ";" after 'if'.

Can you justify your answer?

Deepanshu said: (Dec 10, 2014)  
If statement cannot be terminated by semi-colon.

Hare.. said: (Dec 17, 2014)  
@Deepanshu and @Kavi.

"if (x != y) ;" it will exits and doesn't show you the error in turbo-C compiler.

It is Close with semicolon means that ';' immediately terminates if statement then that if statement immediate braces or block are treated as anonymous block.

Hare.. said: (Dec 17, 2014)  
@Angelica.

Here we are using only one for loop, so it isn't necessary to write curly braces. But in case if we use more than one for loop then curly braces are must.

Sujan said: (Jan 4, 2015)  
But the condition says that i<10 rite? So how can we get x and y as 10?

Hare.. said: (Jan 6, 2015)  
@Sujan.

In above program we have one for loop is [for(i=1; i<10; i++)], In this for loop 'i' value conditioned as 'i<10'.

If, that 'i' value is using in 'printf' then only we have choice to print that 'i' value. Only 'x' and 'y' values written in printf statement, so we are simply printing x and y values as 10 and 10 respectively.

if(x != y);

printf("x = %d y = %d\n", x, y);

In above two statements if statement is closed with ';' means it will create one anonymous block (shown below) and that if statement is not related to printf statement. We have to treat that Printf statement as independent.

if(x != y); turboc compiler understand this if statement as,

if(x != y)
{

};

So the if statement doesn't have any body part simply it is empty block.

Soniya Vijay said: (Feb 10, 2015)  
x = 10, y = 10.

For loop has no statement inside it.

After the if statement there is a; so no o/p will be produced but it will not give any error.

The printf statement will print the values of x and y.

So the answer is (B) 2, 3.

Sandeep said: (Aug 12, 2015)  
X=10, Y=10 for loop statement no inside.

Sandeep said: (Aug 12, 2015)  
Please show me X and Y value correct answer.

Avijit said: (Aug 24, 2015)  
When this statement execute y = 100% 90 the y value become 10, the loop continue 9 times and the value of if either true or false its no matter because there is a semicolon after that that means the if end there then it comes to the printf statement and print the x value as 10 and y value as 10; so option B is the correct answer.

Ashish Soni said: (Feb 8, 2016)  
Program successfully compile and execute.

It produce output: x = 10; y = 10 for 9 times.

Avi said: (Mar 1, 2016)  
I don't now after if condition put semicolon in C you clearly explain.

Kiranmai said: (May 19, 2016)  
@Kavya.

Your explanation is clear and useful. Thank you.

Priyanka said: (Aug 1, 2016)  
When this statement executes why = 100% 90 the why value become 10, the loop continue 9 times and the value of if either true or false it's no matter because there is a semicolon after that that means the if end there then it comes to the printf statement and print the x value as 10 and why value as 10; so option B is the correct answer. I agree but how print output 2, 3 please explain someone.

Saikumar said: (Dec 17, 2016)  
Thank you, @Prasanth.

Sanjay said: (Jul 26, 2017)  
Thank you, for explaining this program.

Antor said: (Oct 12, 2017)  
Explain it, please.

Rohan said: (Jun 9, 2018)  
@All.

According to me, it is;

#include<stdio.h>
int main()
{
int x = 10, y = 100%90, i;
/* (i) Intialize x with the value of 10
(ii) Intialize y with the value of 10 [ 100%90 = 10 (% returns remainder)]
(iii) declare i
for(i=1; i<10; i++)
/* There is no relation between for loop and if condition */
if(x != y);
/* (i) [ 10 !=10 ] , the condition is false but
(ii) ";" (semicolon) has terminated the statement and moves to next statement.. */
printf("x = %d y = %d\n", x, y);
/* (i) x, y is assigned the value 10 which has been initialized earlier */
return 0;
}

Vivek Kumar said: (Aug 10, 2018)  
Thanks @Kavya.

S.V.Kishore said: (Sep 10, 2018)  
Good explanation @Kavya.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.