Nested if else Statement In C

In this program we’ll show you nesting of if else statements. Here we are illustrating the concept by taking score/marks of 5 subject from the user. Then we calculate the percentage of it and display grade to the user.

Simple Logic

When the condition in if statement is false then only else block gets executed. In our program, we check, if percentage is greater than or equal to 60. If it is false, then only control shifts to else block. So inside else(nested if else) we need not once again check if the percentage is less than 60.

Note: We are not using else if and logical operator in this C program purposefully. We want to show nesting of if else in this program.

 
#include < stdio.h >

int main()
{
    float s1, s2, s3, s4, s5, per;

    printf("Enter marks of 5 subject\n");
    scanf("%f %f %f %f %f", &s1, &s2, &s3, &s4, &s5);

    per = (s1 + s2 + s3 + s4 + s5) / 5.0;

    if(per >= 60)
    {
        printf("Grade A\n");
    }
    else
    {
        if(per >= 50)
        {
            printf("Grade B\n");
        }
        else
        {
            if(per >= 40)
            {
                printf("Grade C\n");
            }
            else
            {
                printf("Failed!\n");
            }
        }
    }

    return 0;
}

Output 1:
Enter marks of 5 subject
59
60
62
61
63
Grade A

Output 2:
Enter marks of 5 subject
59
55
56
53
58
Grade B

Output 3:
Enter marks of 5 subject
49
45
46
49
48
Grade C

Output 4:
Enter marks of 5 subject
39
45
25
36
35
Failed!

Student Grade Calculation using Nested if else: C Program


[youtube https://www.youtube.com/watch?v=I-EHRu-Nq8o]

YouTube Link: https://www.youtube.com/watch?v=I-EHRu-Nq8o [Watch the Video In Full Screen.]


Note: In next video we’ll show you how to perform the same operation as shown in above C program, using else-if and logical operators.

Also check the program C Program To Check Leap Year for a perfect nested if else example.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

if else statement in C

In our previous video tutorial we saw how to use the decision control instruction if. In this tutorial we shall see how we can use if else statement in division of 2 numbers example.

We illustrate the use of if else statement in this video tutorial with the help of a division of 2 numbers example program.

Division of 2 Numbers: if else construct in C

Single statement in both if and else block

 
#include < stdio.h >

int main()
{
    int a, b;

    printf("Enter value of a and b for division (a/b)\n");
    scanf("%d%d", &a, &b);

    if( b != 0)
        printf("Division of %d and %d is %d\n", a, b, a/b);
    else
        printf("You can't divide a number by 0\n");

    return 0;
}

Output 1:
Enter value of a and b for division (a/b)
10
2
Division of 10 and 2 is 5

Output 2:
Enter value of a and b for division (a/b)
50
0
You can’t divide a number by 0

Note: When condition for if is true, if block gets executed. When condition in if is false, else block code gets executed.

Multiple statements in both if and else block

 
#include < stdio.h >

int main()
{
    int a, b, c;

    printf("Enter value of a and b for division (a/b)\n");
    scanf("%d%d", &a, &b);

    if( b != 0)
    {
        c = a / b;
        printf("Division of %d and %d is %d\n", a, b, c);        
    }
    else
    {
       printf("You can't divide a number by 0\n");
       printf("Please run the program once again and give proper value\n");
    }


    return 0;
}

Output 1:
Enter value of a and b for division (a/b)
10
2
Division of 10 and 2 is 5

Output 2:
Enter value of a and b for division (a/b)
50
0
You can’t divide a number by 0
Please run the program once again and give proper value

In above example we are enclosing if and else block code in curly braces as both if and else has multiple lines of code or block of code to be executed.

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

if else Construct in C: Division of 2 numbers logic


[youtube https://www.youtube.com/watch?v=WLLJn3hcFa0]

YouTube Link: https://www.youtube.com/watch?v=WLLJn3hcFa0 [Watch the Video In Full Screen.]


This video was produced as building block for our simple calculator application.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Division of 2 Numbers: C

In this video tutorial you can learn the procedure followed in C programming to divide two numbers.

 
#include < stdio.h >

int main()
{
    int a, b, c;

    printf("Enter 2 numbers for division\n");
    scanf("%d %d", &a, &b);

    c = a / b;

    printf("Division of %d and %d is %d\n", a, b, c);

    return 0;
}

Output:
Enter 2 numbers for division
10
5
Division of 10 and 5 is 2

You can write same program without using third variable to calculate division of 2 numbers, as below:

 
#include < stdio.h >

int main()
{
    int a, b;

    printf("Enter 2 numbers for division\n");
    scanf("%d %d", &a, &b);

    printf("Division of %d and %d is %d\n", a, b, (a/b));

    return 0;
}

Output:
Enter 2 numbers for division
10
5
Division of 10 and 5 is 2

Note: Instead of int you can take float variables too. That would help in taking both integer as well as real values from the user as input.

Division by 0

What if a user enters 0 for variable b. Any number divided by 0 will give undefined return. So lets handle the scenario using Decision Control Instruction In C: IF and inequality operator.

 
#include < stdio.h >

int main()
{
    int a, b;

    printf("Enter value for a and b, for division (a/b)\n");
    scanf("%d%d", &a, &b);

    if( b != 0 )
       printf("Division of %d and %d is %d\n", a, b, a/b);

    if( b == 0 )
       printf("You can't divide a number by 0\n");

    return 0;
}

Output 1:
Enter value for a and b, for division (a/b)
10
2
Division of 10 and 2 is 5

Output 2:
Enter value for a and b, for division (a/b)
10
0
You can’t divide a number by 0

Output 3:
Enter value for a and b, for division (a/b)
0
5
Division of 0 and 5 is 0

In above program, != is inequality operator. It returns true(non-zero number) if the operands are not equal. == is equality operator. It returns true(non-zero number) if the operands are equal.

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Division of Two Numbers: C Programming


[youtube https://www.youtube.com/watch?v=QOhDxu0xbY8]

YouTube Link: https://www.youtube.com/watch?v=QOhDxu0xbY8 [Watch the Video In Full Screen.]


This video was produced as building block for our simple calculator application.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Decision Control Instruction In C: IF

So far we’ve seen that the code in C program gets executed in sequential order i.e., as and when the code appears in the source code. But today lets see how we can change the sequence of or order of execution using decision control instruction like if statement.

General form of if statement

if( some_condition )
statement;

By default, if statement executes only the next instruction present under it. But if you want to execute some block of code, then you must enclose it inside curly braces as shown below.

if( some_condition )
{
statement_1;
statement_2;
some_logic;
function_calls;
etc;
}

in above code, some_condition can be anything from a simple true(non-zero number) or false(0) or any expression returning 0 or any non-zero number.

Statements inside if statements curly braces can be anything from multi-line statements, function calls, some arithmetic operations etc.

 
#include < stdio.h >

int main()
{
    int a;
    printf("IBM\n");
    printf("Enter value for a \n");
    scanf("%d", &a);

    if(a > 10)
    {
        printf("Microsoft\n");
        printf("Oracle\n");
    }

    printf("Amazon\n");
    printf("Apple\n");    

    return 0;
}

Output:
IBM
Enter value for a
5
Amazon
Apple

Here user entered value for variable a is less than 10. So the code inside if block doesn’t get executed. So printing of Microsoft and Oracle gets skipped.

 
#include < stdio.h >

int main()
{
    int a;
    printf("IBM\n");
    printf("Enter value for a \n");
    scanf("%d", &a);

    if(a > 10)
    {
        printf("Microsoft\n");
        printf("Oracle\n");
    }

    printf("Amazon\n");
    printf("Apple\n");    

    return 0;
}

Output:
IBM
Enter value for a
14
Microsoft
Oracle
Amazon
Apple

For the same program, user entered value for variable a as 14, which is greater than 10. So all the company names gets printed on the console window as shown in above output section.

We can even remove condition statement inside if statement

 
#include < stdio.h >

int main()
{
    int a;
    printf("IBM\n");
    printf("Enter value for a \n");
    scanf("%d", &a);

    if(a)
    {
        printf("Microsoft\n");
        printf("Oracle\n");
    }

    printf("Amazon\n");
    printf("Apple\n");    

    return 0;
}

Output 1:
IBM
Enter value for a
1
Microsoft
Oracle
Amazon
Apple

Output 2:
IBM
Enter value for a
0
Amazon
Apple

Output 3:
IBM
Enter value for a
-5
Microsoft
Oracle
Amazon
Apple

In above example, when user enters 0 the condition becomes false, so the block of code inside if statement doesn’t get executed. When the user enters any number other than 0, its considered as true, so the code inside if statement gets executed.

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Decision Control Instruction In C: IF


[youtube https://www.youtube.com/watch?v=ih-gHCBoQ0U]

YouTube Link: https://www.youtube.com/watch?v=ih-gHCBoQ0U [Watch the Video In Full Screen.]


For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Relational Operators In C

Relational Operators in C programming language return true(non-zero number) or false(0) value. They operate on 2 operands.

Relational Operators

== equality operator.
!= inequality operator.
> greater than operator.
< less than operator.
>= greater than or equal to operator.
<= less than or equal to operator.

== equality operator

 
#include < stdio.h >

int main()
{
    //Relational Operators

    int a = 10, b = 10;
    int temp;

    temp = (a == b);

    printf("value of temp is %d\n", temp);

    return 0;
}

Output:
value of temp is 1

 
#include < stdio.h >

int main()
{
    //Relational Operators

    int a = 10, b = 20;
    int temp;

    temp = (a == b);

    printf("value of temp is %d\n", temp);

    return 0;
}

Output:
value of temp is 0

!= inequality operator

 
#include < stdio.h >

int main()
{
    //Relational Operators

    int a = 10, b = 10;
    int temp;

    temp = (a != b);

    printf("value of temp is %d\n", temp);

    return 0;
}

Output:
value of temp is 0

 
#include < stdio.h >

int main()
{
    //Relational Operators

    int a = 10, b = 20;
    int temp;

    temp = (a != b);

    printf("value of temp is %d\n", temp);

    return 0;
}

Output:
value of temp is 1

> operator

 
#include < stdio.h >

int main()
{
    //Relational Operators

    int a = 20, b = 10;
    int temp;

    temp = (a > b);

    printf("value of temp is %d\n", temp);

    return 0;
}

Output:
value of temp is 1

< operator

 
#include < stdio.h >

int main()
{
    //Relational Operators

    int a = 20, b = 10;
    int temp;

    temp = (a < b);

    printf("value of temp is %d\n", temp);

    return 0;
}

Output:
value of temp is 0

>= operator

 
#include < stdio.h >

int main()
{
    //Relational Operators

    int a = 20, b = 10;
    int temp;

    temp = (a >= b);

    printf("value of temp is %d\n", temp);

    return 0;
}

Output:
value of temp is 1

<= operator

 
#include < stdio.h >

int main()
{
    //Relational Operators

    int a = 20, b = 10;
    int temp;

    temp = (a <= b);

    printf("value of temp is %d\n", temp);

    return 0;
}

Output:
value of temp is 0

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Relational Operators In C


[youtube https://www.youtube.com/watch?v=8wlJ4bo110Y]

YouTube Link: https://www.youtube.com/watch?v=8wlJ4bo110Y [Watch the Video In Full Screen.]


Note: = is assignment operator. == is equality operator.

This video was produced as building block for our simple calculator application.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert