If Statement in C

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.

Related Posts

  • 78
    Decision !!! we all need to alter our actions in the face of changing circumstances. If the weather is fine, then I will go for a stroll. If the highway is busy I would take a diversion. If the pitch takes spin, we would win the match. If she says…
    Tags: executed, statement, decision, control, program, instruction, operators, programming, write, condition
  • 76
    It may so happen that in a program we want more than one statement to be executed if the expression following if is satisfied. If such multiple statements are to be executed then they must be placed within a pair of braces as illustrated in the below example. Example: The current year…
    Tags: executed, statement, program, condition, example, number, control, decision, learn, write
  • 74
    C allows usage of three logical operators, namely, &&, || and !. These are to be read as ‘AND’ ‘OR’ and ‘NOT’ respectively. There are several things to note about these logical operators. Most obviously, two of them are composed of double symbols: || and &&. Don’t use the single…
    Tags: program, printf, operators, statement, operator, learn, write, programming, tutorial
  • 72
    In this article we will learn about the NOT operator, written as !. This operator reverses the result of the expression it operates on. For example, if the expression evaluates to a non-zero value, then applying ! operator to it results into a 0. Vice versa, if the expression evaluates…
    Tags: operator, example, true, non-zero, program, learn, programming, write, tutorial
  • 70
    It is perfectly all right if we write an entire if-else construct within either the body of the if statement or the body of an else statement. This is called ‘nesting’of ifs. Let's have an example: /* A quick demo of nested if-else */ main( ) { int i ;…
    Tags: statement, program, printf, condition, write, learn, tutorial, programming

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: