Loading...
C if-else Statements

C If-else Statements

In this tutorial, you will learn about if statement, if-else statement and if-elseif statements with the help of suitable examples.


What is Decision making?

The ability to change the behavior of a piece of code which is based on certain information in the environment is known as conditional code flow.


Introduction

Any programming language provides the programmers the ability to perform choices.
We want to do X in some cases, and Y in other cases.
We want to check data, and do choices based on the state of that data.
C provides us 2 ways to do so.

The first is the if statement, with its else helper, and the second is the switch statement.


If Statement

The if statement allows us to control if a program enters a section of code or not based on whether a given condition is true or false.

In simple words, an if statement, If statement is used to test a condition, if condition is true then the block provided in the curly brackets is executed otherwise that code is not executed:

Syntax :

if (condition) {
  // Statements execute if the condition is true
}

How if statement works?

The if statement evaluates the given condition inside the parenthesis ().

  • If the condition is evaluated to true, statements inside the parenthesis of if are executed.
  • If the condition is evaluated to false, statements inside the parenthesis of if are not executed.

Example : if statement

// Program to find that the given number is greater or smaller that 10
#include <stdio.h>

int main() {
    int num;
    printf("Enter an integer : ");
    scanf("%d", &num);
    if (num > 10) {
      printf("%d is greater than 10.\n", num);
    }
    printf("Here, the execution of program completed.");
    return 0;
}

First Output :

Enter an integer: 45
45 is greater than 10.
Here, the execution of program completed.

In First output user input integer 45 , the condition in if statement is num > 10 is evaluated to true. Hence the statements inside parenthesis () are executed.

Second Output :

Enter an integer: 5
Here, the execution of program completed.

In Second output user input an integer 5 , the condition in if statement is num > 10 is evaluated to false. Hence the statements inside parenthesis () are not executed.


If...else Statement

In simple words, the If-else is also used to test a condition, if condition is true then the code inside the if statement is executed otherwise else part is executed.

It also allows the program to select an action based upon the user's input.

The if statement may have an optional else block. The syntax of the if..else statement is:

Syntax :

if (condition) {
  //statements to be executed of the given condition is True
}
else {
  // statements to be executed if the given condition is False
}

How if-else statement works?

The if statement evaluates the given condition inside the parenthesis ().

  • If the condition is evaluated to true,
    1. statements inside the parenthesis{} of if are executed.
    2. statements inside the parenthesis{} of else are skipped from execution.
  • If the condition is evaluated to false,
    1. statements inside the parenthesis{} of else are executed.
    2. statements inside the parenthesis{} of if are skipped from execution.

Example : if...else statement

// Check whether an integer is positive or negative
#include <stdio.h>

int main() {
  int num;
  printf("Enter an integer: ");
  scanf("%d", &num);
  if (num >= 0) {
    printf("%d is a positive integer.",num);
  }
  else {
    printf("%d is an negative integer.",num);
  }
  return 0;
}

First Output :

Enter an integer : 12 
12 is a positive integer.

In first output, user input an integer 12 , the condition in if statement is num > 0 is evaluated to true. Hence, the statement inside if are executed and statements inside the else are skipped from execution.

Second Output :

Enter an integer : -3 
-3 is a negative integer.

In second output, user input an integer -3 , the condition in if statement is num > 0 is evaluated to false. Hence, the statement inside else are executed and statements inside the if are skipped from execution.


If...else if Ladder

The if...else ladder allows you to check multiple condition and execute different part of code based on whether the given condition is true or false.

We have to provide different condition in if and else-if part of the if-else if ladder.

Note : We can use one or more else if statements in our if-else if ladder.

Syntax :

if (condition 1){
  // statements
}
else if(condition 2) {
  // statements
}
else if (condition 3) {
  // statements
}
.
.
else {
  // statements
}

How if...else if ladder works?

  • If the condition of if or any else if is evaluated to true,
    1. statements inside the parenthesis{} of if or else if are executed.
    2. statements inside the parenthesis{} of remaining if, else if or else if are not executed .
  • If the condition of if and all the else if is evaluated to false,
    1. statements inside the parenthesis{} of else are executed.
    2. statements inside the parenthesis{} of if and all else if are not executed.

Example : if...else Ladder

// Program to compare two integers
#include <stdio.h>

int main() {
  int num1, num2;
  printf("Enter two integers: ");
  scanf("%d %d", &num1, &num2);
  if(num1 > num2) {
    printf("%d is greater than %d",num1,num2);
  }
  else if (num1 < num2) {
    printf(" %d is greater than %d", num2, num1);
  }
  else if (num1 == num2) {
    printf("%d is equal to %d", num1, num2);
  }
  else {
    printf("Enter a valid number.");
  }
  return 0;
}

Output

Enter two integers: 12 34 
34 is greater than 12

Nested if...else

We can also include one or more if-else statements inside another if-else statement

It contains multiple if else condition. It is used to check the multiple conditions. This statement is like executing an if statement inside an else statement.

Syntax :

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

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

}

Example : Nested if...else

// Program to compare two integers using nested if-else statements
#include <stdio.h>

int main() {
  int num1, num2;
  printf("Enter two integers: ");
  scanf("%d %d", &num1, &num2);
  if (num1 != num2) {
     if (num1 > num2) {
       printf("%d is greater than %d",num1,num2);
     }
  else {
       printf("%d is greater than %d",num2,num1);
     }
  }
  else {
     printf("Both the inputs are equal.",num1, num2);
  }
  return 0;
}

Output

Enter two integers: 54 76 
76 is greater than 54

Point to Remember :

If any if has only statement than it is not necessary to use parenthesis{}.

Example :

if (x > y) {
  printf("x is greater than y");
}
printf("y is greater than x");

or

if (x > y)
  printf("x is greater than y");
printf("y is greater than x");

Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of If else Statement in C.

Keep Learning : )

In the next tutorial, you'll learn about C Ternary Operator.

- Related Topics