Control Statements in C Programming Language: If- Else

Control flow or Flow of Control is the sequence or the order in which the code statements are executed. This can be explicitly controlled in an imperative programming language which distinguishes it from a declarative programming language. Here the statements in itself ask the compiler to make a choice as to which statement should be executed next. There are many control flow statements in C Programming language. The flow chart below shows the flow of control in a simple program which checks which number is greater.

max number flowchart

Read More :- Operators in C Language

The control flow depends on the condition which checks whether a is greater than b. There are basically three ways of flow of control:

  • Branching: Branching statements are those which require the compiler to choose the next statement from two or more choices based on the condition provided by the programmer.
  • Jump statements: Jump statements makes the control jump to another section of the program unconditionally when encountered.
  • Looping: They allow the same block of statements to be executed more than once as per programmers’ choice.

Branching statements are of following types:

  1. if-else
  2. Switch statements
  3. Ternary Operator

Jump statements are of following types:

  1. Break
  2. Continue
  3. Goto
  4. Return

Also, looping statements are of following types:

  1. for loop
  2. while loop
  3. do-while loop

In this tutorial, we shall see more about if-else statements and their use in programming.


If-Else Statements

If-else statements, also known as decision-making statements, are used to compile a particular block of statements according to the condition provided. The keyword used by C is if, which instructs the compiler that the following statement or block of statements are decision control instructions. if is followed by the condition enclosed in parentheses. If the condition is true the next statement or the next block of statements are executed. In case the condition is false it goes to the else block/or next if block of statements. Now, C uses another keyword else which follows the if block of statements. As mentioned above, the else block of statements are executed when the if condition is false. Let us see the syntax of a simple if-else program:

if(condition)
  statement;
else
  statement;

Let us see a simple program where we find which is the greater number using if-else:

#include <stdio.h>

int main()
{
    int a=5,b=16;
    if(a>b){
        printf("The greater number is : %d",a);
    }
    else{
        printf("The greater number is : %d",b);
    }
    return 0;
}

Output:-
The greater number is : 16

In this program, we see that we get the output as b which is equal to 16. The compiler checks the condition following if. This condition is false because a<b. So the compiler goes to the else statement and then executes the else block statements.


If-else if- else statements

In case we have more than one condition,i.e., we have several blocks of statements depending on different conditions, we use if-else if- else condition. else if is another keyword in C which is followed by a condition in parentheses.

The compiler checks the else if condition in case the if condition is false. And if both the if and the else if conditions are false it goes to the else condition. The syntax is given below:

 
if(condition1) 
  statement1; 
else if(condition2) 
  statement2; 
else statement3; 

Let us see a simple program to compare two numbers:

#include <stdio.h>

int main()
{
    int a=5,b=16;
    if(a==b){
        printf("Both the numbers are equal");
    }else if(a>b){
        printf("The greater number is : %d",a);
    }else{
        printf("The greater number is : %d",b);
    }
return 0;
}

Output:-
The greater number is : 16

In this program, the compiler first check whether a is equal to b or not. Since it turns out to be false, the control flows to the next else if statement. It checks whether the condition a<b  is true or not. It again turns out to be false and the control flows to the next else statement which is then executed and we get the given output.


Nested if

Nested if means having an if or if-else or if-else if-else statement inside the block of another if statement. An if can have more than one if statements inside its block of statements. The compiler first checks whether the if condition is true or not. If it is true the control flows to the next if inside the first if and checks whether the condition is true or not. In case it is true the if block of statements are executed otherwise the else block of statements are executed. Now if the first if condition turns out to be false, the else block of statement is executed.Here we have the syntax:

if(condition1)
{
  if(condition2)
    statement1;
  else
    statement2;
}
else
  statement;

Let us see a simple program to find the greater number between three numbers:

#include <stdio.h>

int main()
{
    int a=5,b=16,c=20;
    if(a>b)
    {
        if(a>c){
         printf("The greater number is : %d",a);
        }
        else{
         printf("The greater number is : %d",c);
        }
    }
    else
    {
      if(b>c)
      	printf("The greater number is : %d",b);
      else
      	printf("The greater number is : %d",c);
    }

    return 0;
}

Output:-
The greater number is : 20

In this program, the compiler checks the first if condition and returns false so the control flows to the else block of the statement where it checks the next if condition and again returns false. Now the next else block of statement is executed which gives us the above output.

An investment in knowledge always pays the best interest. Hope you like the tutorial. Do come back for more because learning paves way for a better understanding.

Do not forget to share and Subscribe.

Happy coding!! 🙂

Recommended -