3 common mistakes in if statement

if statement

Mistake No. 1

Look at the program given below

  1. int main()
  2. {
  3. char c = 'a';
  4. if ( c == 'b' );
  5. printf("%c", c);
  6. }
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:

  1. int main()
  2. {
  3. char c = 'a';
  4. if ( c = 'b' )
  5. printf("The value of c is %c", c);
  6. }
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:

  1. int main()
  2. {
  3. char c='a';
  4. if (c == 'b')
  5. printf("Statement A ");
  6. printf("Statement B ");
  7. printf("Test me ");
  8. }

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.

Comments

I think this is very

I think this is very important at basic level. These are the common mistakes students usually do.

Yogendra you have given a

Yogendra you have given a very good example and explained it nicely.
I hope students will realize the importance of learning the basics of programming in depth now.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].
  • You may use <swf file="song.mp3"> to display Flash files inline

More information about formatting options