In programming, we things called control structure to influence the flow of our program, Without control structure the machine would read and execute the first statement, then step down to the next and execute it and no on until each line is done. However, there are times when we don't want certain lines of code to be executed or we want them executed over and over again, That in where control structure in comes, They let us choose how our program is executed. The control structure are of the three type they are:


Decision Making and Branching | if-else


Decision Control Structure
The decision control structure are also called as bi-directional control condition. In c we can instruct the computer to execute certain lines of code if a given condition is true that condition is not true then we can either skip that code or go on with our program

There are the following variants of if statement in C language:

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

If Statement

One of the most powerful statements in C is the if statement. The if command is multi-line statement called the if statement whose format is like this:

if (test expression) 
{
   // statements which will be executed if the test expression is true
}
The condition is any expression which returns a true or false result. The condition may be a Boolean value or a longer expression that includes conditional and possibly one or more logical

Flowchart of if statement in C

C Flow Control Statement

Example 1: Program to find a number if it is negative. ( if statement )

// Program to Find 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:

Enter an integer: 5
The if statement is easy.

If-else Statement

A variation of the if-then statement is the if then else statement, which executes one block of  statements if the condition is true and another if the condition is false. Here is the general format for the if-else combination:

if (test condition) {
    // statements to be executed if the test condition true
}
else {
    // statements to be executed if the test condition is false
}

Flowchart of if-else statement in C

Flowchart of if-else statement in C

Example 2: Find an integer is odd or even (if-else statement )

// Find 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.


If...else Ladder Statement  

if..else statement executes two types of code depends on whether the test condition is true or false. Sometimes, a choice has to be made from more than 2  true possibilities. the if...else ladder allows you to check between multiple test condition and different statements.

To add more power to data comparison, you can user if..else ladder statements. statements, each else and end if always goes with the most recent if. The indentation of each embedded if helps to show where one if begins and where another ends. When you embed if..else

if (test condition1) {
   // statement(s)
}
else if(test condition2) {
// statement(s) } else if (test condition3) {
// statement(s) } . . else { // statement(s) }
Flowchart of if-else ladder statement in C

Example 3: Program to relate two integers values using = > or < symbol. (if...else Ladder )

// Program to relate two integers values 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