C - If Else decision making statements

C Programming

Share

In this tutorial we will learn about if-else decision making statements in C programming language.

If statement

We use the if statement to execute a block of code if a given condition is satisfied.

Format of an if statement.

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

So, if the condition of the if statement evaluates to non-zero (true) value then the code inside the if block is executed otherwise, it is ignored.

The if block is within the opening curly bracket { and the closing curly bracket }.

In the following example we take an integer value from the user. If the value is greater than 100 then we print "Entered number is greater than 100." otherwise we don't.

#include <stdio.h>
int main(void)
{
  //declare variable
  int x;
  
  //take user input
  printf("Enter an integer number: ");
  scanf("%d", &x);
  
  //check condition
  if (x > 100) {
    printf("Entered number is greater than 100.\n");
  }

  printf("End of code\n");
  
  return 0;
}

Output

Enter an integer number: 200
Entered number is greater than 100.
End of code

In the above output we entered an integer number greater than 100 so, we got the output from the if-block.

In the following example we entered an integer number less than 100 so, the if-block is not executed.

Enter an integer number: 99
End of code

If Else statement

If we have two options and we want to execute any one of the option depending on a condition then we use the if-else statement.

Following is the format of an if-else statement.

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

So, when the condition evaluates to a non-zero (true) value then the if-block code is executed. If the condition evaluates to zero (false) value then the else-block code is executed.

In the following example we are checking if user entered a value greater than 10.

#include <stdio.h>
int main(void)
{
  //declare variable
  int x;
  
  //take user input
  printf("Enter an integer number: ");
  scanf("%d", &x);
  
  //check condition
  if (x > 10) {
    printf("Entered number is greater than 10.\n");
  }
  else {
    printf("Entered number is less than or equal to 10.\n");
  }
  
  printf("End of code\n");
  
  return 0;
}

Output

Enter an integer number: 20
Entered number is greater than 10.
End of code

In the above output the if-block code was executed because the entered integer number was greater than 10.

In the following output we will enter a number less than or equal to 10 and that will result in the execution of the else-block code.

Enter an integer number: 10
Entered number is less than or equal to 10.
End of code

Else If statement

If we want to have more than two options then we use the else if statement.

Format of the else if statement.

if (condition1) {
  //if block code...
}
else if (condition2) {
  //else if block code...
}
else {
  //else block code...
}

In the following example we are checking if the entered integer number is greater than, or less than, or equal to 0.

#include <stdio.h>
int main(void)
{
  //declare variable
  int x;
  
  //take user input
  printf("Enter an integer number: ");
  scanf("%d", &x);
  
  //check condition
  if (x > 0) {
    printf("Entered number is greater than 0.\n");
  }
  else if (x < 0) {
    printf("Entered number is less than 0.\n");
  }
  else {
    printf("Entered number is equal to 0.\n");
  }
  
  printf("End of code\n");
  
  return 0;
}

Output: When number is greater than 0.

Enter an integer number: 10
Entered number is greater than 0.
End of code

Output: When number is less than 0.

Enter an integer number: -100
Entered number is less than 0.
End of code

Output: When number is equal to 0.

Enter an integer number: 0
Entered number is equal to 0.
End of code
Share