if-else Statement in C


if-else Statement in C


Topic : if-else Statement



OBJECTIVES :

In this section we have learn about the if-else statement in C Language. and we are also show the different types of if-else Statement in C Language With their if-else Syntax and Examples.

AGENDA

  • Why we need if-else Statement ?
  • if Statement
  • if-else Statement
  • else-if Statement
  • Nested if-else Statement

1. Why we need if-else Statement ?

if-else Statement are used in program to take Decision or Diversion of programing code on given algorithm. if-else statement are given decision in if - Statement and if the if - Statement is true then the if - Statement scope are execute if the if - Statement is false then the else - Statement is execute

Example :
fig. if-else Statement (Conditional Statement)

In the above Diagram the the given Condition is A true then the program execute or cursor  move to the B and execute the part B portion. and if the Condition of A is false then the program execute or cursor move to the C and execute part C portion. and the program is terminate.

2. if Statement

The Basic if statement tests a conditional expression and, if it is non-zero (i.e TRUE ), executes the subsequent statement. This executes the body of bracketed code starting with statement1 if condition evaluates to true.

Syntax :

if (condition) {
    statement1;
    ...
}

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

Output :
Enter a Number : 12
a is greated than 10 

3. if-else Statement

The if-else Statement can also command multiple statements by wrapping them in braces. Statement so grouped are called a compound statement, or block, and they are syntactically equivalent to a single statement. This executes the body of bracketed code starting with statement1 if condition evaluates to true, or it executes the body of code starting with statement2 if condition evaluates to false. Note that only one of the bracketed code sections will ever be executed.

Syntax :
if (condition) 
  {
    statement1;
    ...
  }
else 
  {
    statement2;
    ...
  }

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 a;
 printf("Enter a Number : ");
 scanf("%d",&a);
 
 if(a%2!= 0)
 {
  printf("The Number is Odd \n");
 }
 else
 {
  printf("The Number is Even \n");
 }
 
return 0;
}

Output :
Enter a Number : 4
The Number is Even 

// test Second Condition

Enter a Number : 5
The Number is Odd 

4. else-if Statement

In the Nested if-else Statement, in the given conditional Statement have multiple else conditions. this chain is evaluated from the top and, if a particular if-conditional Statement is TRUE, then it's Statement is execute and the chain is terminated. on the other hand, if the condition is FALSE, the next if-conditional Statement is tested. if all the conditionals evaluate to FALSE, then the final else statement is executed as a default.
In this structure, dependent statements are chained together and the condition for each statement is only checked if all prior conditions in the chain are evaluated to false. Once a condition evaluates to true, the bracketed code associated with that statement is executed and the program then skips to the end of the chain of statements and continues executing. If each condition in the chain evaluates to false, then the body of bracketed code in the else block at the end is executed.

Syntax :
if(first condition) 
{
    ...
}
else if(second condition) 
{
    ...
}
.
.
.
.
else if((n-1)'th condition) 
{
    ....
}
else 
{
   // final and Default statement are execute if all condition are FALSE 
   ...
}

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    if(n == 1)
    {
        printf("one");
    }
    else if(n==2)
    {
        printf("two");
    }
    else if(n==3)
    {
        printf("three");
    }
    else if(n==4)
    {
        printf("four");
    }
    else if(n==5)
    {
        printf("five");
    }
    else if(n==6)
    {
        printf("six");
    }
    else if(n==7)
    {
        printf("seven");
    }
    else if(n==8)
    {
        printf("eight");
    }
    else if(n==9)
    {
        printf("nine");
    }
    else
    {
        printf("Greater than 9");
    }
return 0;
}

Output :
// check first condition
Enter a Number : 5
five

// check Second condition
Enter a Number : 8
eight

// check third condition
Enter a Number : 12
Greater than 9
 

5. Nested if-else Statement

In a Nested if-else Statement the. if condition are contain another if - else Condition. and the condition are look like loop. if the first if - else condition are TRUE then it check second if Condition and so on.

Syntax :
if(condition)
{
 if(condition)
 {
  if(condition)
  {
   (statement)
  }
  else
  {
   (statement)
  }
 }
 else
 {
  (Statement)
 }
}
else 
{
 (Statement)
}


To Get More Examples Related if-else Statement click hear.
Next Post Previous Post
8 Comments
  • SolutionsForCode
    SolutionsForCode Sunday, June 14, 2020

    very usefull topic

    • CodeWorld19
      CodeWorld19 Sunday, June 14, 2020

      Thank you :)

  • V.S
    V.S Saturday, July 04, 2020

    your post is very helpful for me
    learn c programming

    • CodeWorld19
      CodeWorld19 Saturday, July 04, 2020

      Thank you

  • EDM Hitz
    EDM Hitz Friday, April 01, 2022

    This comment has been removed by the author.

  • EDM Hitz
    EDM Hitz Friday, April 01, 2022

    This comment has been removed by the author.

  • EDM Hitz
    EDM Hitz Friday, April 01, 2022

    Java If Else tutorial For Beginners Learn Free

  • EDM Hitz
    EDM Hitz Friday, April 01, 2022

    what are literals in java learn literal in java Statement in free by CodeExampler website

Add Comment
comment url