if else Statements in C.

By Shakib Ansari | Date: Wed, Apr 2, 2025

Decision Making and Branching

Programming mein decision making and branching ek dusre se related hoti hai but dono ka work thoda alag hota hai jo samjhna bahut hi important hai before understand if-else statements.  

Decision Making : Yeh ek process hota hai jaha par program ek condition ko evalutates karta hai and decide karta hai ki konsa block of code run karna hai us condition ke hit hone par. Examples: if , if-else , if-else-if , switch-case

Example :

int x = 10;
if( x > 5){
  printf("x is greater than 5");
}
else{ 
  printf("x is 5 or less");
}

Ye program decide karega ki konsi statements ko execute karna chahiye based on the condition x > 5 .

Branching : Branching ka matlab hai ki program jo ki sequentially run ho raha hai suddenly uske flow ko change kar dena jaise koi function call karke function execute karana or loops se break kar jana ya koi bhi.

Example Code:

#include<stdio.h>
void greetBeyondMan(){
printf("Hello Beyond Man");
}

int main(){
greetBeyondMan();
printf("Hello World");
return 0;
}
Output: Hello Beyond Man
Hello World

if-else Statements

if-else statements ka matlab hota hai ki koi desicion lena based on some conditions. Agar if condition pass hoti hai to if block wali statements run hogi aur else wali skip ho jaygi otherwise else block statements hongi

Example Code:

#include<stdio.h>
int main(){
int age = 18;
if(age >= 18){
  printf("Can Vote!");
}else{
printf("Can't Vote!");
}
return 0;
}
Output: Can Vote!

Explanation:

Yaha par decision age ke based par liya jaa raha hai ki agar 'age' is equal or greater to 18, person can vote otherwise person can't vote.

Agar yaha par age less than 18 hoti to if block kabhi run nhi hota aur directly else block run hota bina kisi condition ko check kare.

Nested if-else

Nested if-else ka matlab hota hai ki kisi if-else block ke ander ek aur if-else block ko place kar dena jisse ham zyada condition ko check kar sake.

Example Code:

#include<stdio.h>
int main(){
int age = 18;
if(age >= 18){
  printf("Can Vote!");
  if(age >= 21){
  printf("Eligible To Drink");
  }else{
  printf("Not Eligible To Drink");
}
}else{
printf("Can't Vote!");
}
return 0;
}
Output: Can Vote
Not Eligible To Drink

if-else Ladder

Ye bhi desicion making structure hota hai par ye tab use karte hai jab multiple conditions ko sequentially check karna hota hai. Ye normal if-else statements ka ek extension hota hai.

Syntex of if-else Ladder

if(condition1){
// Code executes if condition1 is true
}else if(condition2){
// Code executes if condition2 is true
}else if(condition3){
// Code executes if condition3 is true
}else{
// Code executes if none of the conditions are true
}

Example Code:

#include<stdio.h>
int main() {
    int marks;
    printf( "Enter your marks: ");
    scanf("%d", &marks);
    if (marks >= 90) {
        printf("Grade: A");
    } else if (marks >= 80) {
        printf("Grade: B");
    } else if (marks >= 70) {
        printf("Grade: C");
    } else if (marks >= 60) {
        printf("Grade: D");
    } else {
       printf("Grade: F (Fail)");
    }
    return 0;
}
Output: Enter you marks: 86
Grade: B

Explanation:

  • Firstly ye user se mark ko input lega aur ye first condition ko check karege
  • marks >= 90 absolutely not, marks is 86 (in this case)
  • phir ye second condition ko check karega
  • marks >= 80 yes marks is 86 jo ki is condition ko true karti hai aur is block ki statments run hogi aur remaining statements skip ho jayngi.
  • Agar koi condition true nhi hoti to else block run ho jata.
About the Author

Hi, I'm Shakib Ansari, Founder and CEO of BeyondMan. I'm a highly adaptive developer who quickly learns new programming languages and delivers innovative solutions with passion and precision.

Shakib Ansari
Programming

Comments