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(5)

belokarvp 7 months, 4 weeks ago
I have to declare the subject as character string. But white spaces are not allowed. How to take input from user?
belokarvp 7 months, 2 weeks ago
where is the quiz code. please give d link
safder951 6 months, 2 weeks ago
#include<stdio.h> int main(){ int std; printf("Select 1 for passing math\n Select 2 for passing in Science\n Select 3 for passing in math and science\n"); scanf("%d",&std); if(std==1){ printf("You win a Bike"); } else if(std==2){ printf("Your win a Bicycle"); } else if(std==3){ printf("Congrates You Win a Car"); } else{ printf("You Have Not Clered In These Exam So, Better Luck Next Time"); } return 0; }
puja2025 4 months, 1 week ago
#include<stdio.h> int main() { int m1,m2; printf("enter your maths marks\n"); scanf("%d",&m1); printf("enter your science marks\n"); scanf("%d",&m2); if(m1>=50 && m2>=50) { printf("congratulations! you have receive gitf of 45"); } else if(m1>=50) { printf("congratulations! you have receive gitf of 15"); } else if(m2>=50) { printf("congratulations! you have receive gitf of 15"); } else { printf("sorry:)-Better luck next time"); } return 0; }
Prasad4295 2 months ago
#include<stdio.h> int main() { int math; int science; printf("Enter your marks of math\n"); scanf("%d", &math); printf("Enter your marks of science\n"); scanf("%d", &science); printf("\nyou have enters the marks of math and science %d %d",math, science); if(math>=40&&science>=40) printf("\nyou have passed both get reward of 100rs"); else if(math>=40) printf("\nyou are only passed in math therefore get reward of 40 rs"); else if(science>=40) printf("\nyou are only passed in science and get reward of 50"); else printf("\nsorry tray again!!!!!!"); return 0; }

Course Announcements

Any Course related announcements will be posted here

Course Content