if-else statement

If-else statement

C uses the keyword if and else to implement the decision control statement. The general form of this statement look like this:

Syntax:  

                if(expression)

                     statement1;

                else

                    statement2;

The expression inside the parenthesis is evaluated and  if it is true(Non-zero value), then statement1 is execute and if the expression is false(Zero) then the statement2 is executed. This expression often called the if condition. The below following flow chart shows the if-else statement.


    
if(expression)
{
    statement
    .................
}
else
{
    statement
    .............
}
if(expression)
    statement1
else
{
    statement
    ..............
}
if(expression)
{
    statement
    .................
}
else
    statement2

/*Program to demonstrate If-else statement*/
#include <stdio.h>
int main()
{
    int num, s;
    printf("Enter the number:");
    scanf("%d", &num);
    s = num;
    if (s % 2 == 0)
    {
        printf("%d is an Even.\n", num);
    }
    else
    {
        printf("%d is an Odd.\n", num);
    }
    return 0;
}

Output:
Enter the number:26
26 is an Even.

If statement

In the decision control statement else part is optional, i.e. an if-else statement can be written with or without the else clause. In case of missing else the syntax would be-

Syntax:

if(expression)
statement1
if(expression)
{
statement
.............
}

If the expression is true, then statement1 is executed and if is false then control transfer to the next statement.

The below flow chart shows the working of an if statement:



/*Program to demonstrate only If statement*/
#include <stdio.h>
int main()
{
    int num, s;
    printf("Enter the number:");
    scanf("%d", &num);
    if (num < 0)
    {
        printf("Number enter is Negative.\n");
    }
    printf("Welcome to Education ki duniya!");
    return 0;
}

😃 Output:
Enter the number:-9
Number enter is Negative.
Welcome to Education ki duniya!
Enter the number:5
Welcome to Education ki duniya!

Nested if-else

We have another if-else statement in the if part or the else part. This is called nested if-else statement

Syntax:

If(expression1)

{

If(expression2)

Statement1;

else

Statement2;

}

Else

{
                 if(expression3)

Statement3;

else

statement4;

}

The following program to illustrate nesting of if-else statement.


/*Program to find the largest number from given numbers input by the user*/
#include <stdio.h>
int main()
{
    int a,b,c,large;
    printf("Input the numbers a,b and c:\n");
    scanf("%d%d%d",&a,&b,&c);
    if(a>b)
    {
        if(a>c)
            large=a;
        else
            large=c;
    }
    else // b>a
    {
        if(b>c)
            large=b;
        else
            large=c;
    }
    printf("Largest number is %d",large);
    return 0;
}

😃Output:
Input the numbers a,b and c:
365
564
695
Largest number is 695

Dangling else problem

Since the else part in an if-else statement is optional, it is possible that in the nested if-else statements an else part is omitted. In this case confusion may arise in associating else part with the appropriate if part.

now take an example:

if (a>b)
        if(a>c)
                printf("a is greater");
else
        printf("c is greater");

above program suggest that we want to match the else with the first if. But remember the indentation only for the reader not by the compiler. The compiler always associate the else part with closest unmatched if part. So in the above example else part match with the second if which is if(a>c).

if (a>b)
        if(a>c)
                printf("a is greater");
        else
                printf("c is greater");

now suppose a is greater than b but a is less than c then the output will be -
c is greater.

NOTE: We get this problem there is an else part missing in the nested if-else structure. The if(a>c) is not have any else part. We should tell the compiler about this by putting braces at proper manner. see in below :

if(a>b)
        {
                if(a>c)
                            printf("a is greater");
        }
else
    {
            printf(" a is less than b");
    }    


else-if  Ladder


This  is a type of nesting in which there is an if-else statement in every else part expect the last else part.

Syntax;

if(expression1)
        statementA;
else if(expression2
        statementB;
else if(expression3)
        statementC;
else         
        statementD;

flow chart of if-else Ladder :


now we can an example to illustrate working of if else ladder.

/*Program to find out the grate of student when the marks of 5 subjects are given.*/
#include <stdio.h>
int main()
{
    float m1, m2, m3, m4, m5, per, sum;
    printf("Marks in English:");
    scanf("%f", &m1);
    printf("Marks in Hindi:");
    scanf("%f", &m2);
    printf("Marks in Physics:");
    scanf("%f", &m3);
    printf("Marks in Maths:");
    scanf("%f", &m4);
    printf("Marks in Chemistry:");
    scanf("%f", &m5);
    sum = m1 + m2 + m3 + m4 + m5;
    per = sum / 5;
    printf("Total percentage marks:%.2f%%\n", per);
    if (per <= 100 && per >= 90)
        printf("A+ Grade");
    else if (per < 90 && per >= 80)
        printf("A Grade");
    else if (per < 80 && per >= 70)
        printf("B Grade");
    else if (per < 70 && per >= 60)
        printf("C Grade");
    else if (per < 60 && per >= 50)
        printf("D Grade");
    else if (per < 50 && per >= 40)
        printf("E grade");
    else
        printf("You are fail! ");
    return 0;
}

😃Output : Marks in English:58
Marks in Hindi:85
Marks in Physics:69
Marks in Maths:65
Marks in Chemistry:78
Total percentage marks:71.00%
B Grade

if we don't use if else ladder ,then equivalent code for this problem :


if (per <= 100 && per >= 90)
        printf("A+ Grade");
if (per < 90 && per >= 80)
        printf("A Grade");
if (per < 80 && per >= 70)
        printf("B Grade");
if (per <  70 && per > 60)
        printf("C Grade");
if (per < 60 && per >= 50)
        printf("D Grade");
if (per < 50 && per >= 40)
        printf("E Grade");

Comments

Popular posts from this blog

18 Amazing programs in C.

Loop control statement