Presentation is loading. Please wait.

Presentation is loading. Please wait.

COURSE: BY:HIRA FARMAN. TOPIC: BY:HIRA FARMAN CHAPTER # 04: LET US C & TURBO C.

Similar presentations


Presentation on theme: "COURSE: BY:HIRA FARMAN. TOPIC: BY:HIRA FARMAN CHAPTER # 04: LET US C & TURBO C."— Presentation transcript:

1 COURSE: BY:HIRA FARMAN

2 TOPIC: BY:HIRA FARMAN CHAPTER # 04: LET US C & TURBO C

3 BY:HIRA FARMAN

4

5  Switch case statements mostly used when we have number of options (or choices) and we may need to perform a different task for each choice.  The control statement that allows us to make a decision from the number of choices is called a switch. BY:HIRA FARMAN

6

7

8

9 Switch(integer expression) { case constant 1: do this: case constant 2: do this: case constant 3: do this: default: do this; } BY:HIRA FARMAN

10

11

12

13 main() { int i=2; switch (i) { case 1: printf(“I am in Case1 "); case 2: printf(“I am in Case2 "); case 3: printf(“I am in Case3 "); case 4: printf(“I am in Case4 "); default: printf("Default "); } getch(); } BY:HIRA FARMAN

14  Case2 Case3 Case4 Default BY:HIRA FARMAN

15 The output is definitely not what we expected! we did not expect the second and third line in the previous output. The program prints case 2 & case 3 and the default case. Well, yes,we said the switch executes the case where a match is found and all the subsequent cases and the default as well BY:HIRA FARMAN

16

17

18

19  Break statement is used to terminate the execution of a statement block.The next statement which follows this statement block will be executed the keyword is break;  When a break statement is executed in a loop, the repetition of the loop will be terminated.  The break statement is a jump instruction and can be used inside a switch construct, for loop, while loop and do-while loop.  The syntax of a break statement is very simple:  break; BY:HIRA FARMAN

20 main() { int i=2; switch (i) { case 1: printf(“I am in Case1 "); break; case 2: printf(“I am in Case2@"); break; case 3: printf(“I am in Case3 "); break; case 4: printf(“I am in Case4 "); break; default: printf("Default "); } } BY:HIRA FARMAN

21 I am in case 2@ BY:HIRA FARMAN

22 Few points about Switch Case BY:HIRA FARMAN

23

24 void main() { char ch='b'; switch (ch) { case 'd': printf("CaseD "); break; case 'b': printf("CaseB"); break; case 'c': printf("CaseC"); break; case 'z': printf("CaseZ "); break; default: printf("Default "); } getch(); } BY:HIRA FARMAN

25  CaseB BY:HIRA FARMAN

26 main() { int i=1; switch(i-2) { case -1; printf(“\nfeeling great”); break; case 0: printf(“\n feeling happy”); break; case 1: printf(“\feeling excited”); break; default: printf(“felling sad because default condition"); } getch(); } BY:HIRA FARMAN

27  feeling great BY:HIRA FARMAN

28 Write a program to accept day no(1 to 7) and print the day name. BY:HIRA FARMAN

29

30

31

32

33

34

35

36

37

38  Avoid goto keyword!They make a C programmer’s life miserable. There is seldom a legitimate reason for using goto and use is one of the reasons that programs become unreliable, unreadable,& hard to debug.And yet many programmers find goto. BY:HIRA FARMAN

39  In a difficult programming situation,it seems so easy to use a goto to take control where u want. BY:HIRA FARMAN

40  The big problem with goto’s is that when we do use them we can never be sure how we got a certain point in our code. They obscure the flow of control.(so as far as possible skip them)you can always get the job done without them. BY:HIRA FARMAN

41

42 main() { int goals; printf(“Enter the number of goals scored against India\n”); If(goals<=5) goto ter; else { printf(“About time soccer players learnt C\n”); printf(“and said goodbye!); } ter: printf(“data jump”); getch(); } BY:HIRA FARMAN

43 Enter the number of goals scored against India 3 data jump (But) Enter the number of goals scored against India 7 About time soccer players learnt C and said goodbye BY:HIRA FARMAN

44 QUESTIONS & ANSWERS??? BY:HIRA FARMAN

45 QNO1:Using goto keyword make any program. QNO2:Using switch statement make a program on your choice. (it should be different from lecture) BY:HIRA FARMAN


Download ppt "COURSE: BY:HIRA FARMAN. TOPIC: BY:HIRA FARMAN CHAPTER # 04: LET US C & TURBO C."

Similar presentations


Ads by Google