If...Else Statement in C - Decision Making Statements



In C there are many times when decision-making statements or decision control statements are required.
These statements execute a set of instructions based on the result of the Boolean condition inside the decision control loop used and a completely different set of instructions if the answer to the Boolean condition is different.



The chart given above shows the decision-making process of any decision control structure.
Many decision-making statements are being used in C and they are as follows.

1.) If statements
2.) If/Else statements
3.) If/Else ladder statements
4.) 
Nested If/Else statements
5.) Goto and Switch statements

In this post, we shall learn about these statements.If statement in C:

The "if" statement is a condition in the form of a Boolean expression.
This Boolean expression can be true or false.

If the condition is true then the statements/commands within the "if" block gets executed. But, If the condition is false then the statements/commands within the "if" block won't be executed and this flow control statement shall terminate and the code will go further.

The syntax for that is like the following:
if(boolean expression or condition)
{
/*If the expression is true then this block will get control meaning the statements within this if block will get executed*/
}
If/Else in C:
Ok, so to learn if-else statements we shall understand the "if" & the "else" parts of it.

If part of the if/else statements is a condition in the form of a Boolean expression.
This Boolean expression can be true or false.
If the condition is true then the statements/commands within the "if" block gets executed. But, If the condition is false then the statements/commands within the "else" block get executed.

The syntax for that is like the following:
if(boolean expression or condition)
{
/*If the expression is true then this block will get control meaning the statements within this if block will get executed*/
}
else
{ /*If the expression is false then this block will get control meaning the statements within this else block will get executed*/
}
If/Else ladder:

The if-else statement is sufficient only in the case of conditions not requiring different results but when there is the requirement of an if/else like statement that has a different set of instructions for different "if's" the if/else ladder is the option to go with.

if(boolean expression or condition)
{
/*If the expression is true then this block will get control meaning the statements within this if block will get executed otherwise this block gets skipped and the next one gets control*/
}
else if(boolean expression or condition)
{ /*If the expression above is true then statements within this else if block will get executed*/
}
else if(boolean expression or condition)
{ 
 /*If the expression above is true then statements within this else if block will get executed*/
}
else if(boolean expression or condition)
{  
/*If the expression above is true then statements within this else if block will get executed*/
}
else
{  
/*If all the expressions above are false then statements within this else block will get executed*/
}

Nested if else in C:
Nested if-else is based on having an if-else block inside another if-else block.

if(boolean expression or condition)
{
/*If the expression is true then this block will get control meaning the statements within this if block will get executed*/
    if(boolean expression or condition)
    {
    /*If the expression is true then this block will get control meaning the statements within         this if block will get executed*/
    }
    else
    { /*If the expression is false then this block will get control meaning the statements             within this else block will get executed*/
    }    
}
else
{ /*If the expression is false then this block will get control meaning the statements within this else block will get executed*/
}

Now let's take the following code for an example, I could have shown separate codes for all the different expressions but having all these inside a single code will make it easier for the learner to distinguish and understand which decision-making statements to choose from.

#include <stdio.h>
int main() {
    int a,b;

    printf("Enter integers: ");
    scanf("%d%d", &a, &b);
    
    /* ####### if statements ####### */ 
    
    if (a > 0)
    {
        printf("You entered %d.\n", a);
    }
    
    if (b > 0)
    {
        printf("You entered %d.\n", b);
    }
    
    /* ####### if/else statements ####### */
    
    if  (a%2 == 0)
    {
        printf("%d is an even integer.\n", a);
    }
    else 
    {
        printf("%d is an odd integer.\n", a);
    }
    
    /* ####### if/else ladder statements ####### */
    
    if(a == b)
    {
        printf("Result: %d = %d \n",a,b);
    }
    else if (a > b)
    {
        printf("Result: %d > %d \n", a, b);
    }
    else {
        printf("Result: %d < %d \n",a, b);
    }
    
    /* ####### nested if/else statements ####### */

    if (a >= b)
    {
      if (a == b)
      {
        printf("Result: %d = %d \n",a,b);
      }
      else
      {
        printf("Result: %d > %d \n", a, b);
      }
    }
    else
    {
        printf("Result: %d < %d \n",a, b);
    }
    
    return 0;
}
In the above program, we can see the different cases in which these different kinds of decision-making statements come into play. when only positive integer was to be checked if statement sufficed but as soon as more detailed problems arose we had to shift to if/else nested if/else and if/else ladder.

We can clearly see this and I hope the reader has got a good idea about these statements and their use cases through these examples. 

Post a Comment

Previous Post Next Post