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 เค เคถे เคฆोเคจ เคตเคฐिเค เคฌ्เคฒ เคฎเคง्เคฏे เคธ्เคोเคฐे เคेเคฒे เคाเคคाเคค.
- เคเคณ เฅฌ เคฎเคง्เคฏे เคฆोเคจ्เคนी เคธंเค्เคฏांเคी เคिंเคฎเคค เคฌเคฐोเคฌเคฐ เคเคนे เคि เคจाเคนी เคนे เคคเคชाเคธเคฒे เคเคนे.
- เคเคฐ เค เคธेเคฒ เคคเคฐ เคคे เคเคจ्เคธोเคฒ เคตเคฐ เคฒिเคนिเคฒेเคฒा เคเคนे.
- เคเคฐ เคเคณ เฅฌ เคฎเคง्เคฏे เคฆोเคจ्เคนी เคธंเค्เคฏांเคी เคिंเคฎเคค เคฌเคฐोเคฌเคฐ เคจเคธेเคฒ เคคเคฐ เคเคณ เฅฎ เคฌเคिเคคเคฒी เคाเคฏेเคฒ.
- เคเคณ เฅงเฅฆ เคฎเคง्เคฏे เคชเคนिเคฒी เคธंเค्เคฏा เคฆुเคธเคฐ्เคฏा เคชेเค्เคทा เคฎोเค ी เคเคนे เคा เคนे เคคเคชाเคธเคฒे เคเคนे. เคเคฐ เค เคธेเคฒ เคคเคฐ เคเคจ्เคธोเคฒ เคตเคฐ เคคे เคฒिเคนिเคฒे เคाเคฏेเคฒ.
- เคเคฐ เคเคณ เฅงเฅฆ เคฎเคง्เคฏे เคชเคนिเคฒी เคธंเค्เคฏा เคฆुเคธเคฐ्เคฏा เคชेเค्เคทा เคฎोเค ी เคจเคธेเคฒ เคคเคฐ เคเคณ เฅงเฅจ เคเคฃि เฅงเฅฉ เคตเคฐเคी เคเคณ เคเคจ्เคธोเคฒ เคตเคฐ เคฒिเคนिเคฒी เคाเคฏेเคฒ.
- Get link
- X
- Other Apps
Labels
C PROGRAMMING TUTORIAL
Labels:
C PROGRAMMING TUTORIAL
- Get link
- X
- Other Apps
Comments