Conditional Statements in C

By | May 10, 2020

In this tutorial we will learn about Conditional Statements in C. There are many situations in the program when we required to run a specific piece of code only in certain if a certain condition is true. In this kind of situation, Conditional statements in c language can be so useful. There are a total of four conditional statements available in C language. Which are as follow:

Types of Conditional Statements in C:

  • if statement
  • if-else statement
  • else-if statement
  • switch statement

if statement:

In case you want to run a piece of code only if a certain condition is true in your code. In this kind of situation, you can use if statement. Before going to the example program. Here you can check syntax to define if statement in a C program.

Syntax to define if statement in C:

if(//condition to check){
  //body of the if statement
}

To understand the flow of the if statement. Please checkout the following flow chart diagram.

Branching statements in C

Example Program:

#include<stdio.h>
int main()
{
	int first_num=10;
	int second_num=5;
	printf("Check if %d is greater than %d\n",first_num,second_num);
	if(first_num>second_num) //if statement
	{
		printf("Yes %d is greater than %d",first_num,second_num);
	}
	return 0;
}

Output:

Check if 10 is greater than 5                                                                                                 
Yes 10 is greater than 5

if-else statement:

Above we learned about if statement. As you can see in if statement body of if will only run if condition in if-statement is true. But we do not have any control in case there is a false condition inside if. In case you want to control working in both conditions (true and false condition). Then you can use the if-else statement. Because in the if-else statement we can write code in the inside body of if in case there is a condition inside if the statement is true and code in the inside body of else in case there is a condition inside if the statement is true. Please check out the following syntax to define if-else statement in c:

Syntax to define if-else statement in C:

if(//condition to check){
   //body of if statements
}else{
   //body of else
}

To understand the flow of the if-else statement. Please check out the following flow chart diagram.

Branching Statements in C

Example Program:

#include<stdio.h>
int main()
{
	int first_num=5;
	int second_num=15;
	printf("Check if %d is greater than %d or Not\n",first_num,second_num);
	if(first_num>second_num) //if statement
	{
		printf("Yes %d is greater than %d",first_num,second_num);
	}else{
		printf("No %d is smaller than %d",first_num,second_num);
	}
	return 0;
}

Output:

Check if 5 is greater than 15 or Not                                                                                          
No 5 is smaller than 15

else-if statement:

The else-if statement is also known as an else-if ladder. This else-if statement is used in case there is are multiple statements. In else-if ladder, conditions check from top to bottom and where it finds true condition it runs the following body code. Please check the following syntax to define else-if in C language.

Syntax of the else-if statement in c:

if(//condtion inside if){
}else if(//condtion inside else if){
}else if(//condtion inside else if){
}else{
}

To understand the flow of the else-if statement. Please check out the following flow chart diagram.

Branching Statements in C

Example Program:

#include<stdio.h>
int main()
{
	int marks=75;
	if(marks>80){
		printf("Grade A");
	}
	else if(marks>60){
		printf("Grade B");
	}
	else if(marks>40){
		printf("Grade B");
	}
	else{
		printf("Fail");
	}
	return 0;
}

Output:

Grade B    

switch statement:

Switch statement is used in case you want to user choose from multiple choices. In switch statement we add choice instead of a condition and we create case to show write code for every particular choice. Please check the following syntax to add switch statement in C.

Syntax to add switch statement in c:

switch (//choice)
{
    case 1: // case 1 will run if choice = 1;
        break;
    case 2: // case 2 will run if choice = 2;
        break;
    default: // default case will run if choice doesn't match to any case
}

To understand the flow of the switch statement. Please check out the following flow chart diagram.

Branching Statements in C

Example Program:

#include <stdio.h>

int main()
{
    int choice=2; //you can change this value to see different result
    switch (choice)
    {
    case 1: // case 1 will run if choice = 1;
        printf("You have choosed 1");
        break;
    case 2: // case 2 will run if choice = 2;
        printf("You have choosed 2");
        break;
    default: // default case will run if choice doesn't match to any case
        printf("Out of Range");
    }
}

Output:

You have choosed 2