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.
}
Checking whether number is less than 0 or not.
Checking whether the number is even or odd
Example 2 Description :
EXAMPLE 3: NESTED IF ELSE CONDITION
C program to relate two integers entered by user using = or > or < sign.
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.
}
- Condition: Here we specify that at what condition should this block be executed.This is same for if and else if.
- else : If all the conditions are rejected then this block is executed.
- In if , if else and all these conditions ,for single statements brackets are not necessary.
Checking whether number is less than 0 or not.
- #include<stdio.h>
- int main()
- {
- int num;
- printf("Enter a number to check.\n");//Display a message
- scanf("%d",&num); //Get input from user
- if(num<0) //To test if number is less than zero.
- /*If "num" value entered is actually less than zero, number will be displayed. */
- /*If "num"value entered is actually "not" less than zero,, number will not be displayed. */
- {
- printf("Number=%d",num);
- return 0;
- }
- Enter the number to check :
- -3
- Number= -3
- Enter the number to check :
- 3
- <- ( No output is displayed here )
- 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.
- рд╣реНрдпрд╛ рдЙрдзрд╛рд░рдгрд╛рдд рдЖрдкрдг рдмрдШрддреЛрдп рдХрд┐ рд▓рд┐рд╣рд┐рд▓реЗрд▓рд╛ рдЕрдВрдХ рд╢реБрдиреНрдп рдкреЗрдХреНрд╖рд╛ рд▓рд╣рд╛рди рдЖрд╣реЗ рдХрд┐ рдирд╛рд╣реА.
- рдУрд│ рем рдордзреНрдп рдЖрдкрдг рдпреБрд╕рд░ рдХрдбреВрди рдХреЛрдгрддрд╛ рд╣рд┐ рдПрдХ рдЕрдВрдХ рдШреЗрдгрд╛рд░..
- рдУрд│ рен рд╣рд┐ "if condition" рдЖрд╣реЗ. рдЗрдереЗ рдШреЗрддрд▓реЗрд▓рд╛ рдЕрдВрдХ рд╢реБрдиреНрдп рдкреЗрдХреНрд╖рд╛ рд▓рд╣рд╛рди рдЖрд╣реЗ рдХрд┐ рдирд╛рд╣реА рд╣реЗ рдмрдШрд┐рддрд▓рд╛ рдЬрд╛рддреЛ.
- рдЬрд░ рдЕрд╕реЗрд▓ рддрд░ ' { ' ' } ' рд╣реНрдпрд╛ рдмреНрд░рд╛рдХреЗрдд рдордзреНрдпреЗ рдЬреЗ рдХрд╛рд╣реА рд▓реЗрд╣рд┐рд▓реЗрд▓рд╛ рдЕрд╕реЗрд▓ рддреЗ рдХреЛрдб рд░рди рд╣реЛрдгрд╛рд░.
- рдкрдг рдЬрд░ рдШреЗрддрд▓реЗрд▓рд╛ рдЕрдВрдХ рд╢реБрдиреНрдп рдкреЗрдХреНрд╖рд╛ рд▓рд╣рд╛рди рдирд╕реЗрд▓ рддрд░ рдордЧ рд╕реНрдХреНрд░реАрди рд╡рд░ рдХрд╛рд╣реАрдЪ рджрд┐рд╕рдгрд╛рд░ рдирд╛рд╣реА.
Checking whether the number is even or odd
- #include <stdio.h>
- int main()
- {
- int num;
- printf("Enter a number to check.\n");
- scanf("%d",&num);
- if((num%2)==0) //checking whether remainder is 0 or not.
- printf("%d is even.",num); //If condition is true then this message is displayed
- else
- printf("%d is odd.",num); //If condition is false then this message is displayed
- return 0;
- }
- 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.
- рд╣реНрдпрд╛ рдЙрдзрд╛рд░реНрдирд╛рдд рдЖрдкрдг рджрд┐рд▓реЗрд▓рд╛ рд╕рдВрдЦреНрдпрд╛ рд╕рдорд╕рдВрдЦреНрдпрд╛ рдЖрд╣реЗ рдХрд┐ рд╡рд┐рд╕реНрд╢реНрдо рд╕рдВрдЦреНрдпрд╛ рдЖрд╣реЗ рддреЗ рдмрдШрдгрд╛рд░ .
- рдУрд│ рел рдЖрдгрд┐ рем рдордзреНрдпреЗ рдЖрдкрдг рдпреБрд╕рд░ рдХрдбреВрди рез рд╕рдВрдЦреНрдпрд╛ рдШреЗрддреЛрдп.
- 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.
- #include <stdio.h>
- int main(){
- int num1, num2;
- printf("Enter two integers to check:".\n);
- scanf("%d %d",&num1,&num2);
- if(num1==num2) //checking whether two integers are equal.
- printf("Result: %d=%d",num1,num2);
- else
- {
- if(num1>num2) //checking whether numb1 is greater than num2.
- printf("Result: %d>%d",num1,num2);
- else
- printf("Result: %d>%d",num2,num1);
- }
- return 0;
- }
- Enter the two numbers to check:
- 1
- 4
- Result: 4 > 1
- 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.
- рдпрд╛ рдЙрдзрд░рдгрд╛рдд тАЭ рдЗрдл,рдПрд▓реНрд╕реЗ, рдПрд▓реНрд╕реЗ рдЗрдл тАЬрдЪрд╛ рд╡рд╛рдкрд░ рдХрд╕рд╛ рдХрд░рд╛рдпрдЪрд╛ рд╣реЗ рджрд┐рд▓рд╛ рдЖрд╣реЗ. рддреНрдпрд╛ рд╕рд╛рдареА рдЖрдкрдг рджреЛрди рдЕрдВрдХ рдШреЗрддрд▓реЗрдд рдЖрдгрд┐ рддреНрдпрд╛рдд рдореЛрдард╛ рдХреЛрдгрддрд╛, рд▓рд╣рд╛рди рдХреЛрдгрддрд╛ , рдХрд┐рд╡рд╛ рджреЛрдиреНрд╣реА рдЕрдВрдХ рдмрд░реЛрдмрд░ рдЖрд╣реЗ рдХрд╛ рд╣реЗ рдХрд╕рд╛ рдмрдШрд╛рдпрдЪрд╛ рд╣реЗ рджрд┐рд▓рд╛ рдЖрд╣реЗ.
- рек рдЖрдгрд┐ рел рдУрд│ рдпреБрд╕рд░ рдХрдбреВрди реи рдЕрдВрдХ рдШреЗрдгреНрдп рд╕рд╛рдареА рд▓рд┐рд╣рд┐рд▓реЗрд▓реА рдЖрд╣реЗ.рддреЗ рджреЛрди рдЕрдВрдХ num1 рдЖрдгрд┐ num2 рдЕрд╢реЗ рджреЛрди рд╡рд░рд┐рдЕрдмреНрд▓ рдордзреНрдпреЗ рд╕реНрдЯреЛрд░реЗ рдХреЗрд▓реЗ рдЬрд╛рддрд╛рдд.
- рдУрд│ рем рдордзреНрдпреЗ рджреЛрдиреНрд╣реА рд╕рдВрдЦреНрдпрд╛рдВрдЪреА рдХрд┐рдВрдордд рдмрд░реЛрдмрд░ рдЖрд╣реЗ рдХрд┐ рдирд╛рд╣реА рд╣реЗ рддрдкрд╛рд╕рд▓реЗ рдЖрд╣реЗ.
- рдЬрд░ рдЕрд╕реЗрд▓ рддрд░ рддреЗ рдХрдиреНрд╕реЛрд▓ рд╡рд░ рд▓рд┐рд╣рд┐рд▓реЗрд▓рд╛ рдЖрд╣реЗ.
- рдЬрд░ рдУрд│ рем рдордзреНрдпреЗ рджреЛрдиреНрд╣реА рд╕рдВрдЦреНрдпрд╛рдВрдЪреА рдХрд┐рдВрдордд рдмрд░реЛрдмрд░ рдирд╕реЗрд▓ рддрд░ рдУрд│ рео рдмрдШрд┐рддрд▓реА рдЬрд╛рдпреЗрд▓.
- рдУрд│ резреж рдордзреНрдпреЗ рдкрд╣рд┐рд▓реА рд╕рдВрдЦреНрдпрд╛ рджреБрд╕рд░реНрдпрд╛ рдкреЗрдХреНрд╖рд╛ рдореЛрдареА рдЖрд╣реЗ рдХрд╛ рд╣реЗ рддрдкрд╛рд╕рд▓реЗ рдЖрд╣реЗ. рдЬрд░ рдЕрд╕реЗрд▓ рддрд░ рдХрдиреНрд╕реЛрд▓ рд╡рд░ рддреЗ рд▓рд┐рд╣рд┐рд▓реЗ рдЬрд╛рдпреЗрд▓.
- рдЬрд░ рдУрд│ резреж рдордзреНрдпреЗ рдкрд╣рд┐рд▓реА рд╕рдВрдЦреНрдпрд╛ рджреБрд╕рд░реНрдпрд╛ рдкреЗрдХреНрд╖рд╛ рдореЛрдареА рдирд╕реЗрд▓ рддрд░ рдУрд│ резреи рдЖрдгрд┐ резрей рд╡рд░рдЪреА рдУрд│ рдХрдиреНрд╕реЛрд▓ рд╡рд░ рд▓рд┐рд╣рд┐рд▓реА рдЬрд╛рдпреЗрд▓.
Comments