Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 2 The if-else-if LECTURE – 14

Similar presentations


Presentation on theme: "Unit 2 The if-else-if LECTURE – 14"— Presentation transcript:

1 Unit 2 The if-else-if LECTURE – 14
The if-else-if (nested if) ladder statements:-In this kind of statements numbers of logical condition are checked for executing various statements. Here if any logical condition is true the compiler executes the block followed by if condition otherwise if skips and executes else block. In if-else statements else block is executed by default after failure of condition. In order to execute the else block depending upon certain condition we can add repetitively if statements in else block. This kind of nesting will be unlimited. Syntax of if-else-if statement if(condition is true) { statements; } else if (condition) { statements; } else { statement; } Basic oc Computer & 'C' Programming

2 Unit 2 The if-else-if LECTURE – 14
Expression:- This Expression Is true if x==y x is equal to y x!=y x is not equal to y x<y x is less than y x>y x is greater than y x<=y x is less than or equal to y x>=y x is greater than or equal to y Basic oc Computer & 'C' Programming

3 Unit 2 The if-else-if LECTURE – 14
Q. WAP to find the Average of six subjects and display the results as follows Average Results >34 & < Third Division >49 & < Second Division >60 & < First Division >75 & <= Distinction If marks in any subject less than 35 Print fail. Basic oc Computer & 'C' Programming

4 Unit 2 The if-else-if LECTURE – 14
#include<stdio.h> void main() { int sum=0,a,b,c,d,e,f; float avg; printf(“ Enter Marks”); printf(“P C B M E H”); scanf(“%d%d%d%d%d%d”, &a,&b,&c,&d,&e,&f); sum= a+b+c+d+e+f; avg=sum/6; printf(“Total: %d Average: %f”, sum,avg); if(a<35||b<35||c<35|| d<35||e<35||f<35) { printf(“Results: fail”); exit(); } if (avg>34 && avg<50) printf(“Result: Third Division”); else if (avg>49 && avg<60) printf(“Result: Second Division”); else if (avg>60 && avg<75) printf(“Result: First Division”); else if (avg>75 && avg<=100) printf(“Result: Distinction”); } Basic oc Computer & 'C' Programming

5 Unit 2 The if-else-if LECTURE – 14
Programs using if-else-if :-  Q. WAP to find the smallest out of the three No. int a,b,c, smallest; printf(“Enter Three No”); scanf(“%d%d%d”, &a,&b,&c); if(a<b) { if(a<c) smallest=a; else smallest=c; } Else { if (b<c) smallest=b; smallest=c;} printf(“The Smallest of %d %d %d is %d”, a,b,c);} Basic oc Computer & 'C' Programming

6 Unit 2 The if-else-if LECTURE – 14
Q. WAP to find the Largest out of the three No. int x,y,z ; printf(“Enter Three No x,y,z”); scanf(“%d%d%d”, &x,&y,&z); printf(“Largest out of three no is”); if(x>y) { if(x>z) printf(“x=%d”,x); else printf(“z=%d”,z); } Else { if (z>y) printf(“z=%d”,z); printf(“y=%d”,y);}} Basic oc Computer & 'C' Programming

7 Unit 2 The if-else-if LECTURE – 14
The break statement:- The keyword break allows the programmers to terminate the loop . The break skips from the loop or block in which it is defined. The control automatically goes to the first statements after the loop or block. The break can be associated with all conditional statements. Basic oc Computer & 'C' Programming

8 Unit 2 The if-else-if LECTURE – 14
The continue statements:- The continue statements is exactly opposite to break. The continue statements is used for continuing next iteration of the loop statements, when it occurs in the loop it does not terminate, but it skips the statements after this statement. It is useful when we want to continue the program without executing any part of the program. Basic oc Computer & 'C' Programming

9 Unit 2 The if-else-if LECTURE – 14
The goto statements:- This statements does not requires any condition. This statement passes control anywhere in the program i.e control is transferred to another part of the program without testing any condition. The user has to define goto statement as follows…….. goto label; Where, the label name must start with any character. Basic oc Computer & 'C' Programming

10 Unit 2 The if-else-if LECTURE – 14
Q.WAP to check whether the entered year is a leap year or not. Ues goto. #include<stdio.h> void main() { int year; printf(“Enter year”); scanf(“%d”, &year); if(year%4= =0) goto leap: else goto noleap: leap: printf(“%d is a leap year”); return; noleap: printf(“%d is not a leap year”); } Basic oc Computer & 'C' Programming

11 Unit 2 The if-else-if LECTURE – 14
Thanks Basic oc Computer & 'C' Programming


Download ppt "Unit 2 The if-else-if LECTURE – 14"

Similar presentations


Ads by Google