If Statement in C

Rate this post

Like most languages, C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this:

if ( this condition is true )
      execute this statement ;

  • The keyword if tells the compiler that what follows is a decision control instruction.
  • The condition following the keyword if is always enclosed within a pair of parentheses.
  • If the condition, whatever it is, is true, then the statement is executed.
  • If the condition is not true then the statement is not executed; instead the program skips past it. But how do we express the condition itself in C? And how do we evaluate its truth or falsity?
  • As a general rule, we express a condition using C’s ‘relational’ operators. The relational operators allow us to compare two values to see whether they are equal to each other, unequal, or whether one is greater than the other.

Here’s how they look and how they are evaluated in C.

if stattement
if stattement
  • The relational operators should be familiar to you except for the equality operator == and the inequality operator !=.
  • Please note that = is used for assignment, whereas, == is used for comparison of two quantities.

Let’s have an example:

/* Example of if statement */


int num;

printf(“Enter a number less than 10”);

printf(“What an obedient servant you are !”);


On execution of this program, if you type a number less than or equal to 10, you get a message on the screen through printf( ). If you type some other number the program doesn’t do anything.

The following flowchart would help you understand the flow of control in the program.

flowchart if ilse
flowchart if ilse
  • We can even use arithmetic expressions in the if statement.

For example:

if ( 3 + 2 % 5 )
printf ( “This works” ) ;

if ( a = 10 )
printf ( “Even this works” ) ;

if ( -5 )
printf ( “Surprisingly even this works” ) ;

  • Note that in C a non-zero value is considered to be true, whereas a 0 is considered to be false.
  • In the first if, the expression evaluates to 5 and since 5 is non-zero it is considered to be true. Hence the printf() gets executed.
  • In the second if, 10 gets assigned to a so the if is now reduced to if ( a ) or if ( 10 ). Since 10 is non-zero, it is true hence again printf( ) goes to work.
  • In the third if, -5 is a non-zero number, hence true. So again printf( ) goes to work. In place of -5 even if a float like 3.14,  it would be considered to be true. So the issue is not whether the number is integer or float, or whether it is positive or negative. Issue is whether it is zero or non-zero.

For any query or issue, feel free to discuss on http://discuss.eduguru.in

Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: