1. if Statement
When we need to execute a block of statements only when a given condition is true then we use if statement. It is also called one way decision making statement, because it display the result only when given condition is true otherwise it doesn't produce any result.Syntax:
if(conditional_expression){
statements;
}
Example 1:
1 2 3 4 5 6 7 8 | #include<stdio.h> void main(){ int num = 45; if(num>=35){ printf("You are Passed !!"); } getch(); } |
Output
You are Passed !!
In the above program value of variable num is 45, which is greater then 35. So, condition became true and given set of statement is executed and it displays You are Passed !!. If we put the value of variable num is less then 35 then it will display empty terminal window. That's why if statement is called one way decision making statement.
Let's see another example of if statement
Example 2:
1 2 3 4 5 6 7 8 9 10 | #include<stdio.h> void main(){ int x, y; x=10; y=50; if(x>y){ printf("%d is greater then %d", x, y); } getch(); } |
Output
In this time the condition became false and output terminal window is empty.
2. if-else Statement
This statement allows branching depending upon the value of the condition. The statement to be executed or skipped depends on the decision. Some important form of if statements are given below:Syntax:
if(conditional_expression)
statement(s);
} else {
statement(s);
}
if-else statement is used in two way decision making. It the conditional expression is true, then the statements following the condition are executed. Otherwise, the statements within the else block will be executed.
Example 1: Write a program to check whether the given number is odd or even.
1 2 3 4 5 6 7 8 9 10 11 12 | #include<stdio.h> void main(){ int n; printf("Enter any Number : "); scanf("%d", &n); if(n%2 == 0){ printf("%d is Even Number", n); }else{ printf("%d is Odd Number", n); } getch(); } |
Output:
Enter any Number : 60
60 is Even Number
60 is Even Number
Example 2: Write a program to print the greatest number among any two numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include<stdio.h> void main(){ int x, y; printf("Enter first number : "); scanf("%d",&x); printf("Enter second number : "); scanf("%d",&y); if(x>y){ printf("Greatest number is %d", x); }else{ printf("Greatest number is %d", y); } getch(); } |
Output:
Enter first number : 10
Enter second number : 16
Greatest number is 16
Enter second number : 16
Greatest number is 16
Example 3: Write a program to print whether the given number is positive or negative.
1 2 3 4 5 6 7 8 9 10 11 12 | #include<stdio.h> void main(){ int num; printf("Enter any number : "); scanf("%d",&num); if(num>0){ printf("You entered positive number"); }else{ printf("You entered negative number"); } getch(); } |
Output:
Enter any number : -9
You entered negative number
You entered negative number
3. if-else-if Statement
In if-else-if statements also called as if-else-of ladder, the conditions and their associated statements can be arranged in a construct that the following form:Syntax:
if(condition-1){
statement-1;
} else if (condition-2){
statement-2;
} else if (condition-3){
statement-3;
} .........
.........
} else {
statement-n;
}
Example 1 : WAP a program to get the marks in computer subject and print the grade as the following condition.
if marks > 90 and <=100, Grade = A+
if marks > 80 and <=90, Grade = A
if marks > 70 and <=80, Grade = B+
if marks > 60 and <=70, Grade = B
if marks > 50 and <=60, Grade = C+
if marks > 40 and <=50, Grade = C
if marks > 30 and <=40, Grade = D+
if marks > 20 and <=30, Grade = D
if marks > 0 and <= 20, Grade = E
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include<stdio.h>
#include<string.h>
void main(){ float m; char g[2]; printf("Enter marks in computer : "); scanf("%f",&m); if(m>90 && m<=100){ strcpy(g, "A+"); }else if(m>80){ strcpy(g, "A"); }else if(m>70){ strcpy(g, "B+"); }else if(m>60){ strcpy(g, "B"); }else if(m>50){ strcpy(g, "C+"); }else if(m>40){ strcpy(g, "C"); }else if(m>30){ strcpy(g, "D+"); }else if(m>20){ strcpy(g, "D"); }else if(m>0 && m<=20){ strcpy(g, "E"); }else{ printf("Invalide Marks !!"); } printf("Grade = %s", g); getch(); } |
Output:
Enter marks in computer : 65
Grade = B
Grade = B
Example 1 : WAP a program to get the percentage and print the division;
Conditions:
if percentage >=80, Division = Distinction
if percentage >=60, Division = First
if percentage >=40, Division = Second
if percentage >=35, Division = Third
Otherwise fail ..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<stdio.h> void main(){ int per; printf("Enter your percentage : "); scanf("%d",&per); if(per>=80){ printf("Division = Distinction"); }else if(per>=60){ printf("Division = First"); }else if(per>=45){ printf("Division = Second"); }else if(per>=35){ printf("Division = Third"); }else{ printf("Division = Fail ... "); } getch(); } |
Output
Enter your percentage : 68
Division = First
Division = First
0 comments:
Post a Comment