IF ELSE EXAMPLE IN C LANGUAGE

Simple program to understand how if(), else,else if() works.
Syntax:

if( condition ){
// Statements to be executed when condition is satisfied
}
 else if( condition ){
// Statements to be executed when first condition is not satisfied but this is satisfied

}
 else{
// Statements to be executed when both the above conditions are not satisfied.

}
  1. Condition: Here we specify that at what condition should this block be executed.This is same for if and else if.
  2. else : If all the conditions are rejected then this block is executed.
  3. In if , if else and all these conditions ,for single statements brackets are not necessary. 
EXAMPLE 1: IF CONDITION 
 Checking whether number is less than 0 or not. 
  1. #include<stdio.h>
  2. int main()
  3. {
  4. int num;
  5. printf("Enter a number to check.\n");//Display a message
  6. scanf("%d",&num); //Get input from user
  7. if(num<0) //To test if number is less than zero.
  8. /*If "num" value entered is actually less than zero, number will be displayed. */ 
  9. /*If "num"value entered is actually "not" less than zero,, number will not be displayed. */ 
  10. {
  11. printf("Number=%d",num); 
  12.  return 0;             
  13. }
Example 1 output:
  • Enter the number to check :
  •  -3
  • Number= -3
  • Enter the number to check :
  •  3
  •                               <- ( No output is displayed here )
Example 1 description:
  • In EXAMPLE 1 we are checking whether the number is less than 0 or not.
  • First we ask user to enter a number by displaying a message in line number.
  • Line number 6 get input from keyboard.
  • Line number 7 is the "if" condition. Here it is checked if value of num is less than zero or not.If condition becomes true then whatever is written in the ' {' ' } ' brackets will be executed.
  • But if the condition is  false then pointer jumps directly outside the if block.
Example 1 description:(рдорд░рд╛рдареАрдд рд╡рд░реНрдгрди)
  • рд╣реНрдпрд╛ рдЙрдзрд╛рд░рдгрд╛рдд рдЖрдкрдг рдмрдШрддреЛрдп рдХрд┐ рд▓рд┐рд╣рд┐рд▓реЗрд▓рд╛ рдЕрдВрдХ рд╢реБрдиреНрдп рдкреЗрдХреНрд╖рд╛ рд▓рд╣рд╛рди рдЖрд╣реЗ рдХрд┐ рдирд╛рд╣реА.
  • рдУрд│ рем рдордзреНрдп рдЖрдкрдг рдпреБрд╕рд░ рдХрдбреВрди рдХреЛрдгрддрд╛ рд╣рд┐ рдПрдХ рдЕрдВрдХ рдШреЗрдгрд╛рд░..
  • рдУрд│ рен рд╣рд┐ "if condition" рдЖрд╣реЗ. рдЗрдереЗ рдШреЗрддрд▓реЗрд▓рд╛ рдЕрдВрдХ рд╢реБрдиреНрдп рдкреЗрдХреНрд╖рд╛ рд▓рд╣рд╛рди рдЖрд╣реЗ рдХрд┐ рдирд╛рд╣реА рд╣реЗ рдмрдШрд┐рддрд▓рд╛ рдЬрд╛рддреЛ.
  • рдЬрд░ рдЕрд╕реЗрд▓ рддрд░ ' { ' ' } ' рд╣реНрдпрд╛ рдмреНрд░рд╛рдХреЗрдд рдордзреНрдпреЗ рдЬреЗ рдХрд╛рд╣реА рд▓реЗрд╣рд┐рд▓реЗрд▓рд╛ рдЕрд╕реЗрд▓ рддреЗ рдХреЛрдб рд░рди рд╣реЛрдгрд╛рд░.  
  • рдкрдг рдЬрд░ рдШреЗрддрд▓реЗрд▓рд╛ рдЕрдВрдХ рд╢реБрдиреНрдп рдкреЗрдХреНрд╖рд╛ рд▓рд╣рд╛рди рдирд╕реЗрд▓ рддрд░ рдордЧ рд╕реНрдХреНрд░реАрди рд╡рд░ рдХрд╛рд╣реАрдЪ рджрд┐рд╕рдгрд╛рд░ рдирд╛рд╣реА.
EXAMPLE 2: IF ELSE CONDITION
Checking whether the number is even or odd
  1. #include <stdio.h>
  2. int main()
  3. int num; 
  4. printf("Enter a number to check.\n"); 
  5. scanf("%d",&num); 
  6. if((num%2)==0) //checking whether remainder is 0 or not. 
  7.      printf("%d is even.",num); //If condition is true then this message is displayed
  8. else
  9.       printf("%d is odd.",num); //If condition is false then this message is displayed
  10. return 0; 
  11. }
Example 2 output:
  • Enter the number to check :
  • 2
  • 2 is even
  • Enter the number to check :
  • 3
  • 3 is odd

Example 2 Description :

  • In Example 2 we are testing whether the number is even or odd
  • Here we get input from user in line 5 and line 6.
  • Logic : Divide the number by 2, Check its remainder. 
  • If the remainder is zero then entered number is even and a message is printed  .
  • But if the remainder is not zero then  else part is executed and message is printed accordingly. 
Example 1 description:(рдорд░рд╛рдареАрдд рд╡рд░реНрдгрди)
  • рд╣реНрдпрд╛ рдЙрдзрд╛рд░реНрдирд╛рдд рдЖрдкрдг рджрд┐рд▓реЗрд▓рд╛ рд╕рдВрдЦреНрдпрд╛ рд╕рдорд╕рдВрдЦреНрдпрд╛ рдЖрд╣реЗ рдХрд┐ рд╡рд┐рд╕реНрд╢реНрдо рд╕рдВрдЦреНрдпрд╛ рдЖрд╣реЗ рддреЗ рдмрдШрдгрд╛рд░ .
  • рдУрд│ рел рдЖрдгрд┐ рем рдордзреНрдпреЗ рдЖрдкрдг рдпреБрд╕рд░ рдХрдбреВрди рез рд╕рдВрдЦреНрдпрд╛ рдШреЗрддреЛрдп.
  • LOGIC: num / 2 = some number рдЙрд░реНрд╡рд░рд┐рдд рд╕рдВрдЦреНрдпрд╛ рдЬрд░ рд╢реБрдиреНрдп рдЕрд╕реЗрд▓ рддрд░ рддреЛ num рдЪреА рд╕рдВрдЦреНрдпрд╛ рд╣рд┐ рд╕рдорд╕рдВрдЦреНрдпрд╛ рдЕрд╢реА рдореНрд╣рдВрдЯрд▓реА рдЬрд╛рдпреЗрд▓.
  • рдЬрд░ рдЙрд░реНрд╡рд░рд┐рдд рд╕рдВрдЦреНрдпрд╛ рд╢реБрдиреНрдп рдЕрд╕реЗрд▓ рддрд░ рдХрдиреНрд╕реЛрд▓ рд╡рд░ рд▓рд┐рд╣реВрди рдпреЗрдИрд▓ рдХрд┐ рд╣рд┐ рд╕рдВрдЦреНрдпрд╛ рд╕рдорд╕рдВрдЦреНрдпрд╛ рдЖрд╣реЗ.
  • рдЬрд░ рдЙрд░реНрд╡рд░рд┐рдд рд╕рдВрдЦреНрдпрд╛ рд╢реБрдиреНрдп рдирд╕реЗрд▓ рддрд░ рдХрдиреНрд╕реЛрд▓ рд╡рд░ рд▓рд┐рд╣реВрди рдпреЗрдИрд▓ рдХрд┐ рд╣рд┐ рд╕рдВрдЦреНрдпрд╛ рд╡рд┐рд╕реНрд╢реНрдо рд╕рдВрдЦреНрдпрд╛ рдЖрд╣реЗ.  

EXAMPLE 3: NESTED IF ELSE CONDITION
C program to relate two integers entered by user using = or > or < sign.

  1. #include <stdio.h>
  2. int main(){ 
  3.      int num1, num2;
  4.      printf("Enter two integers to check:".\n);
  5.      scanf("%d %d",&num1,&num2); 
  6.      if(num1==num2) //checking whether two integers are equal.
  7.           printf("Result: %d=%d",num1,num2); 
  8.      else 
  9.      {
  10.         if(num1>num2) //checking whether numb1 is greater than num2. 
  11.           printf("Result: %d>%d",num1,num2); 
  12.         else 
  13.           printf("Result: %d>%d",num2,num1); 
  14.      }
  15. return 0; 
  16. }
Example 3 output:
  • Enter the two numbers to check:
  • 1
  • 4
  • Result: 4 > 1
Description:
  • Nested if means "if block inside another if block"
  • if {  if{   if{} if{}...    }  }
  • In the above examples we have used nested if conditions to relate two numbers.
  • We check whether the number is  equal, less than or greater than other.
  • Line 4 and line 5 get input numbers from user.
  • Line 6 checks if number is equal.
  • If above condition is true Line 7 displays a message saying the yes numbers are equal and rest of the code is skipped.
  • But if the above condition is false then else part is executed.
  • Line 10 checks if first number is greater than  second one.
  • If condition is true then corresponding message is printed.
  • If condition is false then message in the else part is executed.
  • In the above example two numbers 1 and 4 is taken and else part of line number gets executed. Rest all are skipped due to false condition.

Description in marathi:(рдорд░рд╛рдареАрдд рд╡рд░реНрдгрди)

  • рдпрд╛ рдЙрдзрд░рдгрд╛рдд тАЭ рдЗрдл,рдПрд▓реНрд╕реЗ, рдПрд▓реНрд╕реЗ рдЗрдл тАЬрдЪрд╛ рд╡рд╛рдкрд░ рдХрд╕рд╛ рдХрд░рд╛рдпрдЪрд╛ рд╣реЗ рджрд┐рд▓рд╛ рдЖрд╣реЗ. рддреНрдпрд╛ рд╕рд╛рдареА рдЖрдкрдг рджреЛрди рдЕрдВрдХ рдШреЗрддрд▓реЗрдд рдЖрдгрд┐ рддреНрдпрд╛рдд рдореЛрдард╛ рдХреЛрдгрддрд╛, рд▓рд╣рд╛рди рдХреЛрдгрддрд╛ , рдХрд┐рд╡рд╛ рджреЛрдиреНрд╣реА рдЕрдВрдХ рдмрд░реЛрдмрд░ рдЖрд╣реЗ рдХрд╛ рд╣реЗ рдХрд╕рд╛ рдмрдШрд╛рдпрдЪрд╛ рд╣реЗ рджрд┐рд▓рд╛ рдЖрд╣реЗ.

  • рек рдЖрдгрд┐ рел рдУрд│  рдпреБрд╕рд░ рдХрдбреВрди реи рдЕрдВрдХ рдШреЗрдгреНрдп рд╕рд╛рдареА рд▓рд┐рд╣рд┐рд▓реЗрд▓реА рдЖрд╣реЗ.рддреЗ рджреЛрди рдЕрдВрдХ num1 рдЖрдгрд┐ num2 рдЕрд╢реЗ рджреЛрди рд╡рд░рд┐рдЕрдмреНрд▓ рдордзреНрдпреЗ рд╕реНрдЯреЛрд░реЗ рдХреЗрд▓реЗ рдЬрд╛рддрд╛рдд.

  • рдУрд│ рем рдордзреНрдпреЗ рджреЛрдиреНрд╣реА рд╕рдВрдЦреНрдпрд╛рдВрдЪреА рдХрд┐рдВрдордд рдмрд░реЛрдмрд░ рдЖрд╣реЗ рдХрд┐ рдирд╛рд╣реА рд╣реЗ рддрдкрд╛рд╕рд▓реЗ рдЖрд╣реЗ.
    • рдЬрд░ рдЕрд╕реЗрд▓ рддрд░ рддреЗ рдХрдиреНрд╕реЛрд▓ рд╡рд░ рд▓рд┐рд╣рд┐рд▓реЗрд▓рд╛ рдЖрд╣реЗ.
  • рдЬрд░ рдУрд│ рем рдордзреНрдпреЗ рджреЛрдиреНрд╣реА рд╕рдВрдЦреНрдпрд╛рдВрдЪреА рдХрд┐рдВрдордд рдмрд░реЛрдмрд░ рдирд╕реЗрд▓ рддрд░ рдУрд│ рео рдмрдШрд┐рддрд▓реА рдЬрд╛рдпреЗрд▓.
  • рдУрд│ резреж рдордзреНрдпреЗ рдкрд╣рд┐рд▓реА рд╕рдВрдЦреНрдпрд╛ рджреБрд╕рд░реНрдпрд╛ рдкреЗрдХреНрд╖рд╛ рдореЛрдареА рдЖрд╣реЗ рдХрд╛ рд╣реЗ рддрдкрд╛рд╕рд▓реЗ рдЖрд╣реЗ. рдЬрд░ рдЕрд╕реЗрд▓ рддрд░ рдХрдиреНрд╕реЛрд▓ рд╡рд░ рддреЗ рд▓рд┐рд╣рд┐рд▓реЗ рдЬрд╛рдпреЗрд▓.
  • рдЬрд░ рдУрд│ резреж рдордзреНрдпреЗ рдкрд╣рд┐рд▓реА рд╕рдВрдЦреНрдпрд╛ рджреБрд╕рд░реНрдпрд╛ рдкреЗрдХреНрд╖рд╛ рдореЛрдареА рдирд╕реЗрд▓ рддрд░ рдУрд│ резреи рдЖрдгрд┐ резрей рд╡рд░рдЪреА  рдУрд│ рдХрдиреНрд╕реЛрд▓ рд╡рд░ рд▓рд┐рд╣рд┐рд▓реА рдЬрд╛рдпреЗрд▓.

Comments

Popular Posts