Skip to main content

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 from this blog

IOT : GARDEN WATER SPRINKLER SYSTEM

IoT projects have gained immense popularity due to their ability to leverage the power of interconnected devices and data to create innovative solutions across various domains. IoT projects involve integrating sensors, actuators, and communication technologies to enable the seamless exchange of data between physical devices and the internet. These projects range from simple DIY experiments to complex industrial applications, each offering unique opportunities to transform our lives and improve efficiency. Whether it's creating a smart home automation system, monitoring environmental conditions, optimizing energy usage, or developing intelligent transportation systems, applications of IoT open up a world of possibilities for innovation and connectivity. By harnessing the potential of IoT, we can build intelligent and interconnected systems that revolutionize industries, enhance daily life, and pave the way for a more efficient and sustainable future. In this project we will be using...

POWER BI - THE ARCHITECTURE

Power BI Architecture: A Comprehensive Guide to Building Effective Data Analytics Solutions     Understanding Power BI Architecture: Power BI's architecture comprises four main components, each playing a pivotal role in the data analytics process: Data Sources:   Power BI connects to a wide range of data sources, including databases, cloud services, Excel files, web services, and more. This versatility enables organizations to consolidate their data from various systems into a single dashboard. Data Transformation:   Once the data is sourced, it undergoes transformation and cleaning processes to make it suitable for analysis. Power BI's Power Query Editor provides a user-friendly interface to manipulate, filter, and shape data according to specific requirements. Data Model:   The heart of Power BI's architecture lies in its data model, whic...

TOUCH PLATE BASED DOOR BELL

Title:  Touch plate based door bell  Circuit:  Components: IC 555 Resistors: 1 M, 100k, 330 ohms Transistor: BC547  PN2222A Capacitor: 10n 1 Copper plate : as touch plate. A 6v battery An LED / Ic UM66 Description: This is the simple circuit for touch plate based security system. In this project what basically done is, circuit detects stray voltages produced by mains voltage and electrostatic built  up in the room.If sufficient static voltage is detected by the plate then chip will charge up. Transistor BC 547 or PN2222A is used basically to increase the sensitivity.In place of led just connect IC um 66(melody IC). Applications: In homes, of course. This can be specially used in places like hospitals, when patients need to call doctor by himself.