If else Statement In C Programming Language

If else Statement In C Programming Language

The if statement (including if…else and nested if..else) in C programming will be covered in this lesson with the aid of examples.

The if-else statement is used to carry out activities depending on a particular situation. If and only if the provided condition is true, the activities described in the if block is carried out.

In the C language, the if statement has the following variations.

  • If statement
  • If-else statement
  • If else-if ladder
  • Nested if

if Statement In C

The if statement is used to examine a given condition and carry out actions based on whether or not it is true. It is frequently employed in situations where we must carry out several procedures under various circumstances.

In C programming, the if statement has the following syntax:

if (test expression) 
{
   // code
}

How is an if statement used?

The test expression inside the parentheses is evaluated by the if statement ().

  • The statements inside the body of if are carried out if the test expression is assessed as true.
  • The statements inside the body of the if are not performed if the test expression is evaluated as false.

Example 1: if..statement

// Program to display a number if it is negative

#include <stdio.h>
int main() {
    int number;

    printf("Enter an integer: ");
    scanf("%d", &number);

    // true if number is less than 0
    if (number < 0) {
        printf("You entered %d.\n", number);
    }

    printf("The if statement is easy.");

    return 0;
}

Output 1

Enter an integer: -2
You entered -2.
The if statement is easy.

The test expression number0 is evaluated to be true when the user enters -2. As a result, You entered -2 is shown on the screen.

Output 2

Enter an integer: 5
The if statement is easy.

The test expression number0 is assessed to false when the user enters 5, and the statement inside the body of if is not carried out.

if…else Condition

When using the if-else statement, two actions are taken in response to a single condition.

The if-else statement is an addition to the if statement that allows us to execute two different actions, one to check if a condition is true and the other to check whether it is false.

Here, it is important to note that the if and else blocks cannot be run concurrently.

Since every if condition always triggers an otherwise case, using an if-else statement is always preferred.

There might be an optional else block in the if statement. The if..else statement has the following syntax:

if (test expression) {
    // run code if test expression is true
}
else {
    // run code if test expression is false
}

How do if…else statements function?

Upon evaluation of the test expression as true,

  • If’s body contains statements that are carried out.
  • Statements contained in else’s body are not executed.

If the test expression yields a false evaluation,

  • Statements included inside the else body are carried out.
  • Execution of statements included within the body of if is skipped.

Example 2: if…else statement

// Check whether an integer is odd or even

#include <stdio.h>
int main() {
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);

    // True if the remainder is 0
    if  (number%2 == 0) {
        printf("%d is an even integer.",number);
    }
    else {
        printf("%d is an odd integer.",number);
    }

    return 0;
}

Output

Enter an integer: 7
7 is an odd integer.

The test expression number%2==0 is evaluated to false when the user enters 7. As a result, the assertion contained within else’s body is carried out.

C if…else Ladder

In addition to the if-else statement is the if-else-if ladder statement. It is applied in situations where several cases must be handled for various ailments.

If one of the conditions in an if-else-if ladder statement is true, the statements defined in the if block will be executed.

If another condition is true, the statements defined in the else-if block will be executed. Finally, the statements defined in the else block will be executed if none of the conditions are true.

There are numerous else-if blocks that are feasible. Similar to a switch case statement, if none of the cases match, the default is carried out rather than the else block.

Whether the test phrase returns true or false determines which of two possible codes the if…else statement will execute. There are instances when there are more than two options from which to choose.

You can check between various test expressions and run various lines using the if…else ladder.

Syntax of if…else Ladder

if (test expression1) {
   // statement(s)
}
else if(test expression2) {
   // statement(s)
}
else if (test expression3) {
   // statement(s)
}
.
.
else {
   // statement(s)
}

Example 3: C if…else Ladder

// Program to relate two integers using =, > or < symbol

#include <stdio.h>
int main() {
    int number1, number2;
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    //checks if the two integers are equal.
    if(number1 == number2) {
        printf("Result: %d = %d",number1,number2);
    }

    //checks if number1 is greater than number2.
    else if (number1 > number2) {
        printf("Result: %d > %d", number1, number2);
    }

    //checks if both test expressions are false
    else {
        printf("Result: %d < %d",number1, number2);
    }

    return 0;
}

Output

Enter two integers: 12
23
Result: 12 < 23

Nested if…else

An if…else statement may appear inside the body of another if…else statement.

Example 4: Nested if…else

This program, similar to the if…else ladder example, connects two integers using the operators either, >, and =. To address this issue, we’ll utilize a layered if…else expression.

#include <stdio.h>
int main() {
    int number1, number2;
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    if (number1 >= number2) {
      if (number1 == number2) {
        printf("Result: %d = %d",number1,number2);
      }
      else {
        printf("Result: %d > %d", number1, number2);
      }
    }
    else {
        printf("Result: %d < %d",number1, number2);
    }

    return 0;
}

You do not need to use brackets if the body of an if…else statement simply contains one sentence.

For illustration, this code

if (a > b) {
    printf("Hello");
}
printf("Hi");

is comparable to

if (a > b)
    printf("Hello");
printf("Hi");

You may like:

Java program to find the occurrence of a character in a string with Example

Leave a Reply