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

Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️

Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️ Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️ A load cell is a transducer that converts force (weight) into an electrical signal. The HX711 is a precision 24-bit analog-to-digital converter (ADC) designed for weigh scales. Today we’ll connect a load cell to Raspberry Pi 3 using the HX711 module. ๐Ÿงช ๐Ÿ”ง Components Required Component Quantity Raspberry Pi 3 1 Load Cell 1 HX711 Module 1 Jumper Wires 6 Breadboard (optional) 1 ๐Ÿ”Œ Pin Connections HX711 Pin Raspberry Pi Pin Pin Number VCC 5V Pin 2 GND Ground Pin 6 DT GPIO 5 Pin 29 SCK GPIO 6 Pin 31 Figure: Load Cell connected to Raspberry Pi 3 via HX711 ๐Ÿ’ป Python Code from hx711 import HX711 import RPi.GPIO as GPIO import time hx = HX711(dout_pin=5, pd_sck_pin=6) hx.set_reading_format("MSB", "MSB") hx.set_reference_unit(1) hx.reset()...

Interfacing Water Flow Sensor with Raspberry Pi 3 ๐Ÿšฟ

Interfacing Water Flow Sensor with Raspberry Pi 3 ๐Ÿšฟ ๐ŸŽฏ Objective To measure the flow rate of water using a Water Flow Sensor (YF-S201) and Raspberry Pi 3. Useful in smart irrigation and water management systems. ๐Ÿงฐ Components Required Component Quantity Raspberry Pi 3 1 YF-S201 Water Flow Sensor 1 10K Pull-down Resistor 1 Jumper Wires As required Breadboard 1 ⚡ Circuit Connections Sensor Pin Connect To Red (VCC) 5V (Raspberry Pi) Black (GND) GND (Raspberry Pi) Yellow (Pulse Out) GPIO18 (Pin 12) with pull-down resistor ๐Ÿง  Python Code import RPi.GPIO as GPIO import time FLOW_SENSOR = 18 pulse_count = 0 def countPulse(channel): global pulse_count pulse_count += 1 GPIO.setmode(GPIO.BCM) GPIO.setup(FLOW_SENSOR, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.add_event_detect(FLOW_SENSOR, GPIO.FALLING, callback=countPulse) try: while True: pulse_count = 0 time.sleep(1) flow_rate = (pulse_count / 7.5) ...

Interfacing Sound Sensor with Raspberry Pi 3

๐Ÿ”น Overview The KY-037 is a high-sensitivity sound detection sensor that can detect noise levels in the environment. It provides both analog and digital outputs. In this tutorial, we’ll interface the digital output of KY-037 with Raspberry Pi 3 Model B+ (without using an ADC like MCP3008) and detect sound events.