C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Point Out Correct Statements (Q.No. 6)
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.
1
2, 3
3, 4
4
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
70 comments Page 7 of 7.

Siva said:   1 decade ago
Good explanation Kavya.

Mayya said:   1 decade ago
FOR loop has got no body..... so IF gets executed after the FOR loop... followed by printf

Vini said:   1 decade ago
I have a doubt. Because the if inside the for loop is not satisfying. So how could it be. ?

DAJ said:   1 decade ago
Very good kavya.

Sri said:   1 decade ago
Thanks Kavya. Very good explanation.

Kiran said:   1 decade ago
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.

Keerthana Sundaram said:   1 decade ago
Clear explanation. Thanks.

Sai said:   1 decade ago
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"..

Aswin said:   1 decade ago
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...

Kavya said:   1 decade ago
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.
(4)


Post your comments here:

Your comments will be displayed after verification.