From my experience I have seen student’s making these three common mistakes in if statement. So, I decided to tell you about these mistakes so you don’t repeat them. Hope this will be beneficial to you.

Mistake no. 1

Look at the program given below

int main() 
char c = 'a'; 
if ( c == 'b' ); 
printf("%c", c); 
Now guess the output of the above program?
  1. a
  2. b
  3. nothing will print

Most of you will think of the 3rd option because you can see the condition is not true and hence the statement under if will not execute. Your concept is not wrong, you are right here but probably you miss the semicolon ( ; ) that is placed just after if statement in the programs. if statement ends here and the followed lines are not under the if statement, it’s a free line and it’s execution does not depend on any conditional statement.

option 1 is the correct answer.

Mistake No 2

Look at the program given below:

int main() 
char c = 'a'; 
if ( c = 'b' ) 
printf("The value of c is %c", c); 

What will be the output of this program ?
  1. The value of c is a.
  2. The value of c is b.
  3. Nothing will print

If your answer is 3, probably you missed the ‘ = ‘ assignment operator in the if statement. it’s not conditional operator, so it will not compare the values but it will assign the constant at right hand side to the variable at left hand side and hence the value that come in ‘ if statement ‘ is a non-zero value hence this program will print something.

If your answer is 1, I would like to congratulate you because you noticed the ‘=’ assignment operator in the if statement but there is one thing that you forget that assignment operator not only make the if statement un-effective but also update the value of c from ‘a’ to ‘b’ and hence the option 2 is the correct answer.

Mistake No. 3

Look at the program given below:

int main() 
char c='a'; 
if (c == 'b') 
printf("Statement A "); 
printf("Statement B "); 
printf("Test me "); 

What will be the output of this program ?

  1. Statement A Statement B Test me
  2. Statement B Test me
  3. Test me

if your answer is 1. probably you do not know anything about if statement otherwise you will never select this option, condition is false so how can “Statement A” will print?

if your answer is 3. obviously you know about the if statement but you just choosen a wrong answer because you just beleive in indent (space) that is given to the 6th statement with the intention to make you fool.

if statement takes more then one statement ‘ if and only if ‘ statements are surrounded by the ‘ { ‘ ‘ } ‘ curly braces. so 6th statement is out of the block of if statement and do not depend on if statement. So the correct answer is 2.

How's this tutorial?
Ph.D. (IIT Bombay), M.Tech., B.Tech. (CSE) Dr. Yogendra Pal finished his Ph.D. from Educational Technology department at IIT Bombay. Mr. Pal holds B.Tech. and M.Tech. Degrees in Computer Science and Engineering. His interests include programming, web development, graphic design and educational technology.


Please enter your comment!
Please enter your name here