Conditional Programming in C — Part 1
So hello friends and welcome back to my blog series where we learn the C programming language. In the last chapter, we discussed the operators in C which you can click here to read if you missed it. Now, today we will be discussing one method of conditional programming or branching in C.
What is Conditional Programming?
There might come a time when we must choose one out of many choices according to some condition. That is exactly when we need branching or conditional programming. Conditional Programming or Branching is a way to create multiple branches in your program that can be accessed when a certain condition is met.
For example, if we need to check whether a number is odd or even, we need to see the remainder of the number divided by 2. If the remainder is 1 then it is an odd number, otherwise it is an even number.
Mainly, there are three ways of branching in C and today we will be learning the first one i.e., if…else.
- If…Else
- Switch…Case
- Ternary Operator
If…Else
The first method is known as if…else. How it works is that if a certain condition is true, the statements in the if block get executed, and if the condition is false, the block of code inside is not executed.
Let’s see the example mentioned above in the C program now.
#include <stdio.h>
void main()
{
int num = 7;
if (num % 2 == 0)
{
printf("The number is even.");
}
else
{
printf("The number is odd.");
}
}
Let’s break down the code now. As we learned from the blogs of datatypes and operators, “num % 2” gives the remainder of num divided by 2, and “==” is the operator to check if the two operands are equal. So, within the parenthesis after the “if” keyword, we put the condition of the remainder of num divided by 2 being equal to 0. If the condition is true, the statement enclosed within the curly braces after the “if” keyword runs and if the condition is false, the code enclosed within the curly braces of “else” runs.
Note: — If we notice the condition inside the parenthesis i.e., “num%2 == 0”, it is a boolean value. Thus, we need to put a boolean value inside the parenthesis of the if statement for condition checking.
Now, what if we need more than one condition to check? Let’s say we have two numbers and we need to check if the first number is less than, greater than, or equal to the second number. That’s where if…else if…else comes in place. Let’s see an example first.
#include <stdio.h>
void main()
{
int num1 = 7, num2 = 9;
if(num1 > num2)
{
printf("%d is greater than %d", num1, num2);
}
else if(num1 < num2)
{
printf("%d is less than %d", num1, num2);
}
else{
printf("%d is equal to %d", num1, num2);
}
}
Now, we have 3 branches instead of 2 as in the first example. However, there are no limitations on how many conditions we may use. Thus, we can create as many conditional branches as we require. One thing we must always remember is that the C program runs line by line from top to bottom. So, first, the if condition is checked, if it is true then all the other conditions related to that branch are not executed. If the condition is false, it checks the other conditions one by one until any one of the conditions is true. If none of the conditions are met then the statement in the else blocks is executed.
So, we get the general syntax of “if…else” as the following: —
if (condition 1) {
statement 1;
}
else if (condition 2) {
statement 2;
}
else if (condition 3) {
statement 3;
}
.
.
.
else if (condition n) {
statement n;
}
else {
statement n+1;
}
Here, only the “if ” is mandatory and everything else can also not be used if not necessary. We must remember that the spaces and line gaps are to make the code neat and clean in most cases and it is the semi-colons, curly braces, and small braces that matter the most. Also, never forget that there is no condition for “else” since it is meant to execute when all the above conditions fail.
Finally, let’s see an example of a single if only.
#include <stdio.h>
void main()
{
char alphabet = 'X';
if(alphabet == 'A'){
printf("The alphabet is A");
}
}
This code prints the output if the value of the alphabet variable is ‘A’ otherwise does nothing.
Note: — Two “if” keywords are treated as two different branches which means that if one condition is false, another is also checked. As mentioned earlier, when a condition in a branch is true, none other conditions are checked but in the case of two if, it is different as they are treated as two different branches and thus unrelated conditions.
So, now we learnt what conditional branching is and how to use if…else in C. However, there are many more things to explore in if…else that we must do with more and more practice. In the next blog, we will continue the concepts of conditional programming with the second method i.e., switch case.
Peace out guys!! ✌️
Ps: — Click here to check out the next blog i.e., Conditional Programming in C — Part 2 in which we learn about the second method of branching i.e., switch…case.