Conditional Statements:

In C, the conditional statement is used to control the flow of a program based on specified conditions. It allows us to execute different blocks of code depending on whether the provided condition is true or false.


We use if, if-else, else-if conditional statements.

Note : Comparison operators are used to check the condition.


If statement:

If statement executes the block of code if the provided condition is true.

if(condition){...}
if-else.c
#include <stdio.h>

int main() {
  int age = 20;
  if( age > 18 ){
    printf("You are above 18\n");
  }
    
  return 0;
}

If else:

In if else statement, if the provided condition is true, if block will be executed otherwise else block is executed.

if ( condition ){
  //code
}
else{
  //code
}

Note:

  • We would have to provide condition for if statement.
  • We do not need to provide condition for else statement.


if-else.c
#include <stdio.h>

int main() {
  int num = 15;
  
  if (num % 2 == 0) {
    printf("%d is even.\n", num);
  } else {
    printf("%d is odd.\n", num);
  }
  
  return 0;
}

Else if:

We use else if statement when we want to check multiple conditions. If the particular condition is true, the relevant block of code is executed. If all the conditions are false, else block is executed.

if-else.c
#include <stdio.h>

int main() {
  int num = 15;
  
  if (num == 15) {
    printf("%d = 15.\n", num);
  } 
  else if (num < 15){
    printf("%d < 15.\n", num);
  }
  else{
    printf("%d > 15.\n", num);
  }
  
  return 0;
}

If else with logical operators:

We can use logical operators if we want to check multiple conditions under single if statement.

if-else.c
#include <stdio.h>

int main() {
  int a=10, b=20, c=30;

  if( a<b && a<c ){
    printf("a is less than b and c\n");
  }
  else{
    printf("a is not the smallest\n");
  }
  if( a==10 || b==10 ){
    printf("a = 10 or b =  10\n");
  }
  
  return 0;
}

In this way, you can use any comparison operator with conditional statements to check conditions.

Nested if else:

In nested if else, we use if statement within if statement.

if-else.c
#include <stdio.h>

int main() {
  int a = 100, b = 20, c = 30;

  if (a > b) {
    if (a > c) {
      printf("a is the largest\n");
    }
  } else {
    printf("a is not the largest\n");
  }

  return 0;
}

Dangling else Grammar:

If you are using multiple if statements and single else statement, then there is dangling else problem.
if(condition){...}
if(condition){...}
else{...}
Compiler does not know that with which if, else will go.

Ternary Operator:

It is also used to check condition like if else. It is a single lined statement. It is also called conditional operator.

type var_name = Condition ? if true : if false
if-else.c
#include <stdio.h>

int main() {
  int a = 200, b = 20;
  const char* res = a > b ? "a > b" : "a < b";
  printf("%s\n", res);
  return 0;
}

Goto Statement:

As name indicates goto statement is used to go to a particular line of code. We mark a line of code and at some point, we write "goto marker" and our control jumps to that marker.

int main() {
  marker:
    //code
  goto marker;
    //code
}
goto.c
#include <stdio.h>

int main() {
  int age;

  age_entry:
  printf("Enter your age: ");
  scanf("%d", &age);

  if (age >= 18) {
    printf("You are an adult\n");
  } else {
    printf("You are not an adult\n");
    goto age_entry;
  }

  return 0;
}

Note: We can use ternary operator when logic is simple. It makes your code more compact and easy to use.