Friday, December 1, 2017

If - else Statement in C

if else statements are used in C for making decision from one or more conditions.

In this section we will talk about
  • Single if Statement
  • If else Statement
  • Nested if else Statement


Single if Statement

Single if statement is used in C to execute the body( a block of statements ) if the condition is true. The structure of an if statement is given bellow

1
2
3
4
if( condition )
{
     // This is the body of the if statement.
}
Note
  • if statement will be executed when the condition is true.
  • Between the braces { } is the statement body/block, whatever we write between these braces will be executed if the condition is true.

Let’s look at the following program which will identify either a given number is greater than 0 or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
 
int main()
{
    int var;
    printf(" Enter a number: ");
    scanf(" %d", &var);
 
    if(var > 0)
    {
        printf(" Given number is greater than 0.");
    }
    return 0;
}
Output




If - else Statement

An if - else statement is used in C to make a decision from a list of condition. The structure of an if - else statement is given bellow

1
2
3
4
5
6
7
8
if( condition1 )
{
     // This body statement will be executed when condition1 is true.
}
else
{
     // This body statement will be executed when condition1 is false.
}
Note
  • if statement will be executed if condition1 is true otherwise else statement will be executed.
  • Both if and else statement can’t be executed at a time.

Let’s look at the following example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
 
int main()
{
    int num;
    printf(" Enter a number: ");
    scanf(" %d", &num);
 
    if(num % 2 == 0)
    {
        printf(" Given number is Even.");
    }
    else
    {
        printf(" Given number is Odd.");
    }
    return 0;
}
Sample Output


Sample Output

Note It is not necessary that you have to check only two condition .You can add multiple condition Let’s look into another example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
 
int main()
{
    int var;
    printf(" Enter a positive integer : ");
    scanf(" %d", &var);
 
 
    if(var == 1)
    {
        printf(" Given number is 1.");
    }
    else if( var == 2)
    {
        printf(" Given number is 2.");
    }
    else if( var == 3)
    {
        printf(" Given number is 3.");
    }
    else
    {
        printf(" Given number is greater than 3.");
    }
 
    return 0;
}
Sample Output




Nested if - else Statement

You can use if - else statement into another if - else statement. Let’s look at the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
 
int main()
{
    int var;
    printf(" Enter a positive number : ");
    scanf(" %d", &var);
 
 
    if(var < 10)
    {
        if(var < = 5)
        {
            printf(" Given number is less than or equal 5.");
        }
        else
        {
            printf(" Given number is greater than 5 and less than 10.");
        }
    }
    else if( var < 20)
    {
        if(var < = 15)
        {
            printf(" Given number is less than or equal to 15.");
        }
        else
        {
            printf(" Given number is greater than 15 and less than 20.");
        }
    }
    else
    {
        printf(" Given number is greater than or equal to 20.");
    }
 
    return 0;
}
Sample Output



Previous Topic

Next Topic



No comments:

Post a Comment