If Else Control Statements In C: C Tutorial In Hindi #10

If and Else in C Language -:


C Program executes sequentially i.e. line after line but we can change this pattern by entering some condition in it. So, if we put some condition then the flow of execution changes i.e. conditions are checked and then executed accordingly.


Decision Control Instructions:

It is used to perform operations based on some condition i.e. checking condition first and then evaluating accordingly.


  • If and Else conditions are the parts of Decision Control Instructions.


Sometimes we need to check the condition and then have to make decisions.

  • For e.g. If you want to buy a pair of shoes then you will see your budget and if your budget exceeds 2k Rs. Then you will buy either Nike or Adidas Shoes otherwise you will buy normal casual shoes.
  • By, this example we can understand one thing i.e. if budget exceeds then only we can buy Nike or Adidas shoes otherwise we have to buy normal shoes under 2k Rs.

This is same as if – else i.e. if ‘if’ condition is true then ‘if’ block code will execute otherwise ‘else’ block code will execute.


Ways to implement decision control instructions or Types of If Statements -:

  • If Statement
  • If-else Statement
  • If – else if statement or If-else if ladder
  • Nested if/else


  • If Statement: In if statement condition is checked, and if it is true then ‘If’ statement is executed otherwise it is skipped and rest of the code is executed.



Syntax of If Statement :


If(condition)

{

Statement 1;

Statement 2;

}



  • If-else statement : In if-else statement first of all ‘If’ condition is checked and if the condition is true then ‘If’ statement gets executed otherwise ‘else’ statements gets executed.



Syntax of If-else statement :


If (condition)

{

Statement 1;

Statement 2;

}

else

{

Statement 1;

}



  • If-else if statement : In If-else if statements, if ‘if’ condition is true then ‘if’ statement gets executed and if it is false then ‘else if’ condition is checked and if it is true then ‘else if’ gets executed otherwise condition moves to next statement and if all conditions are false then ‘else’ statement gets executed.



Syntax of If-else if statements :


If (condition)

{ statement ; }

else if (condition)

{

Statement ; }

else if (condition)

{

Statement ; }

else

{

Statement ;

..

}



  • Nested if-else : It means we can use if or else inside if or else i.e. using if else statements inside if else statements. These are used when we want to check conditions inside any particular condition.


Syntax of Nested if-else :


If (condition)

{

Statement ;

If (condition)

{

Statement; }

}



That's all about decision control statements or If-else statements in C Language.

Code as described/written in the video



#include <stdio.h>

int main()
{
    int age;
    printf("Enter your age\n");

    scanf("%d", &age);
    printf("You have entered %d as your age\n", age);
    if (age>=18) {
        printf("You can vote!");
    }

    else if(age>=10)
    {
        printf("You are between 10 to 18 and you can vote for kids");
    }
    
    else if(age>=3)
    {
        printf("You are between 3 to 10 and you can vote for babies");
    }

    else{
        printf("You cannot vote!");
    }
    

    return 0;
}


// maths and science - 45
// science - 15
// maths - 15

// print the type of gift you are giving to them


Comments(2)

belokarvp at 2019-06-23 09:30:09 says
I have to declare the subject as character string. But white spaces are not allowed. How to take input from user?
belokarvp at 2019-06-23 09:30:09 says
where is the quiz code. please give d link