One of the more fundamental aspects of C programming language in particular and of programming in general is
the conditional statements' block. The reason for this, is that it allows a programmer to set up tests.
Results of these tests, in turn, determine the flow of the information.
In this article, you will learn how to set up these test and control the flow of the information in your C
programs. Before we dive deeper into if, else if and else statement
blocks, we would benefit from considering two constructs first, namely, relational and logical operators.
1. What are relational operators in C programming
language?
Relational operators allow us to compare two values and return true or false based on the results. Take
a glimpse at the table below:
Relational operators in C
Symbol
Operator's Name
<
Less than
>
Greater than
==
Equal to
>=
Greater Than or Equal To
<=
Lesser Than or Equal To
!=
Not Equal To
As you can see above, there are 6 relational operators in C. They are used by placing one value to the
left side and one value to the right side of the operator.
Note: Be careful not to use assignment operator (=) in
place of relational operator (==). Assignment
operator assigns values, while relational operator compares them.
2. What are logical operators in C programming
language?
Logical operators allow us to compare two or more relational tests. There is a total of 3 logical
operators, as seen below:
Logical operators in C
Symbol
Operator's Name
&&
AND
||
OR
!
NOT
The logical AND operator (&&) checks if two values return true.
In case of both values being true, the condition with logical AND operator (&&) returns true. The logical AND operator returns false if one
or both of the values return false.
The 1st example returns true because 8 is not equal to 7 AND 8 is less or equal to 8. The 2nd example
returns false because 8 is equal to 8, this condition would return true only if 8 would not be equal to
8. Which as we know is not the case.
The logical OR operator (||) checks if, at least, one of the
values returns true. In which case, the logical OR operators returns true as well. If none of the values
return true, the logical OR operator returns false.
Example with the logical OR operator:
3rd example: (8 != 8) || (8 <= 8) // returns true
In the 3rd example that you can see above, the logical condition is true because one of the values is
true. That is, 8 is less or equal to 8.
Lastly, the logical NOT operator (!) changes the true logical
conditions to false and the false logical conditions to true. Let's modify the already discussed example
with this logical operator:
In the 2nd example the condition returns false, however with the logical NOT operator (!) as is shown in the 4th example, the condition returns true,
because the logical NOT operator changes false values to true and true values to false.
Note: In C language, 0 represents false and 1 represents
true.
3. How do conditional statements work in C?
Now that you are familiar with the logical and relational operators, we can start by inserting
conditions into our statements' blocks.
Example 1: if statement in C.
#include<stdio.h>
intmain(void)
{
int a, b;
a = 10;
b = 0;
if(a > b)
{
printf("a is more than b");
}
if(b > a)
{
printf("b is more than a");
}
return(0);
}
In the code snippet above, we have one if statement (on line 7). With this if statement, we are
checking if a is more than b. Given that a, which
is (10) is more than b, which is 0, the condition returns true
and
we get to see the message "a is more than b" printed in the
terminal.
With the current variable values, print function on line 13 is not called, because if condition (on
line 11) returns
false and the information inside the curly braces is not executed.
Example 2: if, else if and else statements' block in C.
#include<stdio.h>
intmain(void)
{
inta, b;
a = 10;
b = 10;
if(a > b)
{
printf("a is more than b");
}
else if(b > a)
{
printf("b is more than a");
}
else
{
printf("a and b values are equal");
}
return(0);
}
In the code snippet above that we have an else
statement (on line 15), we know that one of the printing functions inside the curly braces will
be called. That is
the rule. Which part of the code will be executed to call the printing functions will depend on the
return values of conditions inside this statements' block.
Given that we have set the a and b
variables to be 10, we know that the first if condition on line 7 will return false (a is not more than
b). The b variable is not more than a, either. Thus, else if
statements on line 11 returns false as well. And so we
descend to the very last part of our conditional statements' block and can expect that the print
function on line 17
will be executed: "a and b values are equal".
Note: With else if and else statements, you can add more
complexity and possible outcomes to your program.