C Tutorial (11) : if-else & Relational operators

C provides an extremely useful statement called if. if lets your programs make decisions and execute certain statements based on the results of those decisions.

The C if statement works just like it does in spoken language: If something is true, do one thing; otherwise, do something else. Consider these statements:

If it’s hot outside, water the lawn.

Table lists the C relational operators, which permit testing of data. Notice that some of the relational operators consist of two symbols.


Note : Relational operators compare two values. You always put a variable, literal, or expression—or a combination of any two of them—on either side of a relational operator.

A regular operator produces a mathematical result. A relational operator produces a true or false result. When you compare two data values, the data values either produce a true comparison or they don’t. For example, given the following values:

int i = 5; 
int j = 10; 
int k = 15; 
int l = 5;

the following statements are true:
i == l; 
j < k; 
k > i; 
j != l;

The following statements are not true, so they are false:
i > j; 
k < j; 
k == l

Tip : To tell the difference between = and ==, remember that you need two equals signs to double-check whether something is equal. 🙂

Note : Only like values should go on either side of the relational operator. In other words, don’t compare a character to a float. If you have to compare two unlike data values, use a typecast to keep the values the same data type.

Every time C evaluates a relational operator, a value of 1 or 0 is produced. True always results in 1, and false always results in 0. The following statements assign a 1 to the variable a and a 0 to the variable b:

a = (4 < 10);  // (4 < 10) is true, so a 1 is put in a
b = (8 == 9);  // (8 == 9) is false, so a 0 is put in b


The if statement uses relational operators to perform data testing. Here’s the format of the if statement:

if (condition)
  block of one or more C statements; 

The parentheses around the condition are required. The condition is a relational test like those described in the preceding section. The block of one or more C statements is called the body of the if statement. The braces around the block of one or more C statements are required if the body of the if contains more than a single statement.

Note : Even though braces aren’t required, if an if contains just one statement, always use the braces. If you later add statements to the body of the if, the braces will be there. If the braces enclose more than one statement, the braces enclose what is known as a compound statement.


In the preceding section, you saw how to write a course of action that executes if the relational test is true. If the relational test is false, nothing happens. This section explains the else statement that you can add to if. Using else, you can specify exactly what happens when the relational test is false.

Here is the format of the combined if…else:

if (condition)
   block of one or more C statements;
    block of one or more C statements; 

So in the case of if…else, one of the two segments of code will run, depending on whether the condition tested is true (in which case, the if code will run) or false (in which case, the else code will run). This is perfect if you have two possible outcomes and need to run different code for each.

Note : Put semicolons only at the end of executable statements in the body of the if or the else. Never put a semicolon after the if or the else; semicolons go only at the end of complete statements.

Note : As with the body of the if, the body of the else doesn’t require braces if it consists of a single statement—but it’s a good idea to use braces anyway.

This program demonstrates another way to use if and else, but this time you can test for four different conditions:

#include <stdio.h>

int prefer;

printf("On a scale of 1 to 10, how happy are you?\n"); 
scanf(" %d", &prefer);

/* Once the user's level of happiness is entered,
   a series of if statements test the number against decreasing numbers. 
   Only one of the four will be executed */

if (prefer >= 8)
printf("Great for you!\n");
printf("Things are going well for you!\n");
else if (prefer >= 5)
printf("Better than average, right?\n");
printf("Maybe things will get even better soon!\n");
else if (prefer >= 3)
printf("Sorry you're feeling not so great.\n"); 
printf("Hope things turn around soon...\n");
printf("Hang in there--things have to improve, right?\n"); 
printf("Always darkest before the dawn.\n");
return 0;

Here are two different runs of this program:

On a scale of 1 to 10, how happy are you?
Better than average, right?
Maybe things will get better soon!

On a scale of 1 to 10, how happy are you? 
Great for you!
Things are going well for you!

The goal of this program is to demonstrate that if…else statements do not have to be limited to two choices. Frankly, you can set as many if…else if…else if…else conditions as you’d like.

Compound operators & Typecasting < Prev                              Next > Logical operators



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s