Introduction
In C, the if
statement is a conditional control statement for decision-making. It allows you to execute a block of code if certain condition evaluates to true
or false
. We can define a if
statement with optional else
in this general format:
if (condition)
{
/* Execute code here if "condition" is true */
}
else if (another condition) /* "else if" block is optional */
{
/* Execute code here if "condition" is false but "another condition" is true */
}
else /* "else" block is also optional */
{
/* Execute code here if condition is false */
}
Comparison and Equality Check with If Statement in C
Comparison Operators
if
and else
statements can perform comparison and equality checks in the order they are defined. You can use these operators to build your conditions:
Operator | Meaning |
---|---|
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
== | equal to |
!= | not equal to |
For examples:
int a = 5; if (a > 2) { printf("%d is greater than 2.\n", a); } else if (a < 2) { printf("%d is less than 2.\n", a); }
Output:
5 is greater than 2.
int a = 2; if (a > 2) { printf("%d is greater than 2.\n", a); } else if (a < 2) { printf("%d is less than 2.\n", a); }
Output:
[No output]
The second example above produces no output because none of the condition matches. If we modify the code like this:
int a = 2; if (a >= 2) { printf("%d is greater than or equal to 2.\n", a); } else if (a <= 2) { printf("%d is less than or equal to 2.\n", a); }
The variable (a = 2) satisfies both “a >= 2” and “a <= 2”, the program will only execute the first condition that evaluates to true, so it will always print:
2 is greater than or equal to 2.
This is still not good enough as we want to know specifically about variable a’s equality. The best way to do this is to add the final else
condition block without any condition, so if “a” is not greater than or less than 2, it must be equal to it because we have exhausted all other conditions:
int a = 2; if (a > 2) { printf("%d is greater than 2.\n", a); } else if (a < 2) { printf("%d is less than 2.\n", a); } else { printf("%d equals to 2.\n", a); }
which will output:
2 equals to 2.
Skip the Brackets
If a if
statement only executes one line of code when it evaluates the true, you can optionally skip writing the brackets. For example, the previous example can be rewritten as:
int a = 2; if (a > 2) printf("%d is greater than 2.\n", a); else if (a < 2) printf("%d is less than 2.\n", a); else printf("%d equals to 2.\n", a);
which is exactly the same. Be very careful here, if your if
statement executes more than 1 line of code, you must
enclose them in brackets.
Equality Check with If Statement in C
We can use the equality operator ==
to do a equality check:
if (a == 99) printf("a equals to 99\n");
NULL and Zero Check Without Any Operator
NULL and Zero checks are two of the very common logics in a typical C program. For example, we would want to do a NULL pointer check to make sure the attempt to allocate some memory is successful; we would want to do a zero check on a function’s return code to make sure the function executes successfully and handles any errors if any. Here’s some examples:
Zero Check Example:
int ret = -1; /* someFunctionCall: return 0 on success, -1 on failure */ ret = someFunctionCall(); /* evaluates to true if ret is anything but zero */ if (ret) { printf("someFunctionCall() failed with return code %d", ret); /* we can have more if statement here to handle different kinds of error indicated by different ret values */ }
NULL Check Example:
int * myint = NULL; /* try to allocate memory for myint pointer */ myint = (int *) malloc (sizeof(int)); if (!myint) printf("failed to allocate memory for myint pointer"); else { printf("successfully allocated memory for myint pointer"); *myint = 99; }
Conditional Operators
We can concatenate multiple conditions together by using conditional operators below:
Operator | Description |
---|---|
&& | conditional AND – evaluates to TRUE only when left and right operands evaluate to TRUE |
|| | conditional OR – evaluates to TRUE when one of the left or right operands evaluates to TRUE |
For example:
/* * evaluate to true when all 3 conditions evaluate to true. * For example, when a = 1 ~ 9. */ if (a > 0 && a < 10) { printf("a is between 0 and 10.\n"); } /* * evaluate to true when one of the 2 conditions evaluate to true. * For example, when a = -99 or 0. */ if (a < 0 || a == 0) { printf("a is either 0 or less than 0.\n"); }
Other Examples
Nested If Statements:
if (age > 18) { if (age < 65) { printf("You are of working age.\n"); } else { printf("You are past retirement age.\n"); } }
If-Else Ladder:
if (number == 0) { printf("The number is zero.\n"); } else if (number % 2 == 0) { printf("The number is even.\n"); } else { printf("The number is odd.\n"); }
Using Brackets for Clarity:
if (num < 0) { printf("The number is negative.\n"); } else { if (num > 0) { printf("The number is positive.\n"); } else { printf("The number is zero.\n"); } }
Related Pages

Hi, this is Cary, your friendly tech enthusiast, educator and author. Currently working as a software architect at Highgo Software Canada. I enjoy simplifying complex concepts, diving into coding challenges, unraveling the mysteries of software. Most importantly, I like sharing and teaching others about all things tech. Find more blogs from me at highgo.ca
Pingback: Common Usages of The C Switch Statement - techbuddies.io
Pingback: How Not To Exploit The C Loops - techbuddies.io
Pingback: Is The Goto in C still a Harmful Practice? - techbuddies.io
Pingback: The C Scope is More Important than a Variable - techbuddies.io
Pingback: 5 Deadly Mistakes to Avoid in C Programming - techbuddies.io