COURSE: BY:HIRA FARMAN
TOPIC: BY:HIRA FARMAN CHAPTER # 04: LET US C & TURBO C
BY:HIRA FARMAN
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
Switch(integer expression) { case constant 1: do this: case constant 2: do this: case constant 3: do this: default: do this; } BY:HIRA FARMAN
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
Case2 Case3 Case4 Default BY:HIRA FARMAN
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
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
main() { int i=2; switch (i) { case 1: printf(“I am in Case1 "); break; case 2: printf(“I am in break; case 3: printf(“I am in Case3 "); break; case 4: printf(“I am in Case4 "); break; default: printf("Default "); } } BY:HIRA FARMAN
I am in case BY:HIRA FARMAN
Few points about Switch Case BY:HIRA FARMAN
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
CaseB BY:HIRA FARMAN
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
feeling great BY:HIRA FARMAN
Write a program to accept day no(1 to 7) and print the day name. BY:HIRA FARMAN
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
In a difficult programming situation,it seems so easy to use a goto to take control where u want. BY:HIRA FARMAN
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
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
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
QUESTIONS & ANSWERS??? BY:HIRA FARMAN
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