Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 2 The Switch Case Statements LECTURE – 15

Similar presentations


Presentation on theme: "Unit 2 The Switch Case Statements LECTURE – 15"— Presentation transcript:

1 Unit 2 The Switch Case Statements LECTURE – 15
The switch () case statements:- The switch () statements is a multi-way branch statement. In the program if there is a possibility to make a choice from a number of option, this structured selection is useful. The switch statement requires only one argument of any data type, which is checked with number of case options. The switch statement evaluates expression and then looks for its value among the case constant. If the value matches with case constant this particular case statement is executed if not default is executed. Here switch case and default are reserved keyword. Every case statement terminates with : . The break statement is used to exit from the current case structure. The switch ( ) statement is useful for writing menu driven program. Basic of Computer & 'C' Programming

2 Unit 2 The Switch Case Statements LECTURE – 15
Syntax of switch () case Switch ( variable or expression) { case constant 1: Statement; break; case constant 2: case constant 3: ……………… ………………. default: } Basic of Computer & 'C' Programming

3 Unit 2 The Switch Case Statements LECTURE – 15
Q.WAP to convert year into 1 minutes 2. Hours 3. Days 4. Months 5.Seconds using switch case. #include<stdio.h> #include<conio.h>  void main() { long ch,min,hrs,ds,mon,yrs,se; printf(“\n[1] Minutes”); printf(“\n [2] Hours”); printf(“\n [3] Days”); printf(“\n [4] Months”); printf(“\n [5] Seconds”); printf(“\n [0] Exit”); printf(“\n Enter your choice”); scanf(“%d”,&ch); Basic of Computer & 'C' Programming

4 Unit 2 The Switch Case Statements LECTURE – 15
if(ch>0&&ch<6) { printf(“Enter year”); scanf(“%ld”,&yrs); } mon=yrs*12; ds=mon*30; ds=ds+yrs?*5; hrs=ds*24; se=min*60; Basic of Computer & 'C' Programming

5 Unit 2 The Switch Case Statements LECTURE – 15
switch(ch) { case 1: printf(“\nminutes %d”,min); break; case 2: printf(“\nhours %d”,hrs); case 3: printf(“\ndays %d”,ds); case 4: printf(“\nmonths %d”,mon); case 5: printf(“\nseconds %d”,sec); case 0: printf(“\nTerminated by choice”); exit(); default: printf(“\nInvalide Choice”); } getch(); } Basic of Computer & 'C' Programming

6 Unit 2 The Switch Case Statements LECTURE – 15
Q.WAP to provide multiple function such as 1. addition 2. Subtraction 3. Multiplication 4. Division 5. Remainders.6. Largest out of two no. #include<stdio.h> #include<conio.h> #include<stdlib.h> Void main() { int a,b,c,ch; clrscr(); Basic of Computer & 'C' Programming

7 Unit 2 The Switch Case Statements LECTURE – 15
printf(“\t===============”); printf(\n\t MENU”); printf(“\n\t===============”); printf(“\n\t[1] ADDITION”); printf(“\n\t[2] Subtraction”); printd(“\n\t[3] Multiplication”); printf(“\n\t[4]Division”); printf(“\n\t[5]Remainder”); printf(“\n\t[6]Larger out of two”); printf(“\n\t[0]exit); printf(“Enter your choice”); scanf(“%d”,&ch); Basic of Computer & 'C' Programming

8 Unit 2 The Switch Case Statements LECTURE – 15
if(ch<=6&ch>0) { Printf(“Enter two numbers”); Scanf(“%d%d”,&a,&b); } Switch(ch) Case 1: C=a+b; Printf(“\nAddition:%d”,c); break; Basic of Computer & 'C' Programming

9 Unit 2 The Switch Case Statements LECTURE – 15
C=a-b; Printf(“\n Subtraction :%d”,c); break; Case 3: C=a*b; Printf(“\n Multiplication :%d”,c); Case 4: C=a/b; Printf(“\n Division :%d”,c); Basic of Computer & 'C' Programming

10 Unit 2 The Switch Case Statements LECTURE – 15
C=a%b; Printf(“\n Remainder :%d”,c); Break; Case 6 : If(a>b) Printf(“\n\t %d(a) is larger than %d(b)”,a,b); else printf(“\n\t%d(a) & %d (b)are same”,a,b); break; case 0: printf(“\nTerminated choice”); Basic of Computer & 'C' Programming

11 Unit 2 The Switch Case Statements LECTURE – 15
exit(); break; default: printf(“\n Invalid Choice”); } getch(); Output: [1] ADDITION [2] Subtraction [3] Multiplication [4]Division [5]Remainder [6]Larger out of two [0]exit Enter your choice: 2 Enter Two no : 3 2 Subtraction :1 Basic of Computer & 'C' Programming

12 Unit 2 The Switch Case Statements LECTURE – 15
Thanks Basic of Computer & 'C' Programming


Download ppt "Unit 2 The Switch Case Statements LECTURE – 15"

Similar presentations


Ads by Google