Conditional Statement in C Programming

On this page, you will learn about conditional statements in C programming. You will get a little description of if-else and switch-case statements. In addition to this, you will get information about how to write if-else and switch case statements. Further, you will get an example of if-else and switch-case statements.


What is the conditional statement in c?

A conditional statement, also known as a selection statement, is a programming construct used to execute specific code based on a specific condition. It allows a program to make decisions and execute different statements based on whether a particular condition is true or false.

In C, there are two types of conditional statements: the if statement and the switch statement. The if statement executes a block of code if a given condition is true, while the switch statement executes a block of code based on the value of a given variable.

In C, there are two types of conditional statements: if-else and switch-case.

  1. if-else statement.
  2. switch-case statement.

if-else statement:

The if-else statement is used to execute a block of code based on a certain condition. If the condition evaluates to true, the statements inside the if block are executed. Otherwise, the statements inside the else block are executed. The syntax for the if-else statement is:


    if (condition) {
        // code to be executed if condition is true
    }
    else {
        // code to be executed if condition is false
    }

The condition in the if statement is a Boolean expression, which evaluates to either true or false. If the condition is true, the block of code inside the if statement will be executed. If the condition is false, the block of code inside the else statement will be executed.

An example of if-else statement given below:


    #include<stdio.h>
    int main(){
      int age=19;
      if(a>18){//becomes ture
        printf("Age is over 18.");
      }
      else{ 
        printf("Age is not over 18.");
      }
     return 0;
    }

Output:


   Age is over 18.  


switch-case statement:

The switch-case statement is used when we want to execute a certain block of code based on the value of a variable. The syntax for the switch-case statement is as follows:


   switch (variable) {
      case value1:
        // block of code to be executed if variable equals value1
        break;
      case value2:
        // block of code to be executed if variable equals value2
        break;
      .
      .
      .
      default:
        // block of code to be executed if variable does not equal any of the values
        break;
    }

In the switch-case statement, we specify a variable to switch on, and then list the possible values for that variable using the case keyword. If the value of the variable matches one of the values listed in the cases, the corresponding block of code will be executed. If none of the values match, the block of code inside the default case will be executed. We use break to terminate the statement.

An example of switch-case statement given below:


   #include<stdio.h>
   int main(){
     int x = 2;
     switch (x) { 
     case 1: printf("Choice is 1"); 
           break; 
     case 2: printf("Choice is 2"); 
           break; 
     case 3: printf("Choice is 3"); 
           break; 
     default: printf("Choice other than 1, 2 and 3"); 
            break;   
    } 
return 0;
}

Output:


    Choice is 2



Both if-else and switch-case statements are powerful tools for controlling the flow of a program based on certain conditions. Understanding how to use these conditional statements effectively is an important skill for any programmer.