0% found this document useful (0 votes)
45 views

C Language (Day-05)

The document provides information about the National Institute of Electronics & Information Technology Gorakhpur Center. It includes the center's website address and social media handles. It also indicates that the contents to be covered include an explanation of switch statements in C programming with examples.

Uploaded by

bhumika_khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

C Language (Day-05)

The document provides information about the National Institute of Electronics & Information Technology Gorakhpur Center. It includes the center's website address and social media handles. It also indicates that the contents to be covered include an explanation of switch statements in C programming with examples.

Uploaded by

bhumika_khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

National Institute of Electronics & Information Technology

Gorakhpur Center
Ministry of Electronics & Information Technology (MeitY), Government of India

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Contents to be covered
• Switch Statement with examples

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Switch Statement
 This is a multiple or multi-way branching decision making statement.
 When we use nested if-else statement to check more than 1 conditions then
the complexity of a program increases in case of a lot of conditions.
 So to overcome this problem, C provides ‘switch case’.
 Switch case checks the value of a expression against a case values, if
condition matches the case values then the control is transferred to that point.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Switch Statement Contd..

In this syntax, switch, case, break are keywords.


expr1, expr2 are known as 'case labels.'
Break statement causes an exit from switch statement.
Default case is optional case. When neither any match found, it executes

Note: The break statement is needed so that once a


case has been executed, it will skip all the other cases
and go outside the switch statement.
If the break statement is omitted, the execution will be
carried out to the next alternatives until the next break
statement is found.
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Important points regarding Switch Case
 Case doesn’t always need to have order 1, 2, 3 and so on. They can have any
integer value after case keyword.
 Also, case doesn’t need to be in an ascending order always, you can specify
them in any order as per the need of the program.
 You can also use characters in switch case. for example –
#include <stdio.h>
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 ");
}
}
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Important points regarding Switch Case
 The expression provided in the switch should result in a constant value
otherwise it would not be valid.

For example:
Valid expressions for switch –
switch(1+2+23)
switch(1*2+3%4)

Invalid switch expressions –


switch(ab+cd)
switch(a+b+c)

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Important points regarding Switch Case
 Nesting of switch statements are allowed, which means you can have switch
statements inside another switch.
 However nested switch statements should be avoided as it makes program
more complex and less readable.
 Duplicate case values are not allowed.
 The default statement is optional, if you don’t have a default in the program,
it would run just fine without any issues.
 However it is a good practice to have a default statement so that the default
executes if no case is matched.
 This is especially useful when we are taking input from user for the case
choices, since user can sometime enter wrong value, we can remind the user
with a proper error message that we can set in the default statement.
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Example of switch case Statement
#include <stdio.h>
void main()
{
char operator;
int x,y;
printf(“\n Enter the operator (+, -, *, /):”);
scanf(“%c”,&operator);
printf(“\n Enter the Two numbers:”);
scanf(“%d%d”,&x,&y);
switch (operator)
{
case ‘+’: printf(“%d+%d=%d”,x,y,x+y);
break;
case ‘-‘: printf(“%d-%d=%d”,x,y,x-y);
break;
case ‘*’: printf(“%d*%d=%d”,x,y,x*y);
break;
case ‘/’: printf(“%d / %d = %d”,x,y,x/y);
break;
default: printf(“\n Enter the operator only”);
break;
}
}

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Example: 1
Write a program to input any number b/w 1 to 7 and print appropriate day
name. (Assume 1 is Sunday)
#include<stdio.h>
#include<conio.h> case 3: printf("Tuesday");
void main() break;
{ case 4: printf("Wednesday");
int day; break;
clrscr(); case 5: printf("Thursday");
printf("enter the day from 1 to 7\n"); break;
scanf("%d",&day); case 6: printf("Friday");
switch(day) break;
{ case 7: printf("Saturday");
case 1: printf("Sunday"); break;
break; default: printf("You have entered wrong input");
case 2: printf("Monday"); }
break; getch();
}

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

1. What will be the output of the following C


code? (Assuming that we have entered the value 2. What will be the output of the following C code?
1 in the standard input) (Assuming that we have entered the value 1 in the
#include <stdio.h> standard input)
void main() #include <stdio.h>
{ void main()
double ch; {
printf("enter a value between 1 to 2:"); int ch;
scanf("%lf", &ch); printf("enter a value between 1 to 2:");
switch (ch) scanf("%d", &ch);
{ switch (ch)
case 1: {
printf("1"); case 1:
break; printf("1\n");
case 2: default:
printf("2"); printf("2\n");
break; }
} }
} a) 1 b) 2
a) Compile time error b) 1 c) 1 2 d) Run time error
c) 2 d) Varies Answer: c
Answer: a
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Quiz
Time

3. What will be the output of the following C


code? (Assuming that we have entered the value 2 4. What will be the output of the following C code?
in the standard input) (Assuming that we have entered the value 1 in the
#include <stdio.h> standard input)
void main() #include <stdio.h>
{ void main()
int ch; {
printf("enter a value between 1 to 2:"); int ch;
scanf("%d", &ch); printf("enter a value between 1 to 2:");
switch (ch) scanf("%d", &ch);
{ switch (ch, ch + 1)
case 1: {
printf("1\n"); case 1:
break; printf("1\n");
printf("hi"); break;
default: case 2:
printf("2\n"); printf("2");
} break;
} }
a) 1 b) hi 2 }
c) Run time error d) 2 Answer: d a) 1 b) 2
c) 3 d) Run time error Answer: b
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Quiz
Time

5. What will be the output of the following C code?


#include <stdio.h> 6. What will be the output of the following C code?
int main() #include <stdio.h>
{ int main()
int a = 1, b = 1; {
switch (a) int x = 97;
{ switch (x)
case a*b: {
printf("yes "); case 'a':
case a-b: printf("yes ");
printf("no\n"); break;
break; case 97:
} printf("no\n");
} break;
a) Yes b) no }
c) Compile time error d) yes no }
Answer: c a) Yes
b) yes no
c) Duplicate case value error
d) Character case value error
Answer: c
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Quiz
Time

7. What will be the output of the following C code?


#include <stdio.h> 8. What will be the output of the following C code?
int main() #include <stdio.h>
{ #define max(a) a
float f = 1; int main()
switch (f) {
{ int x = 1;
case 1.0: switch (x)
printf("yes\n"); {
break; case max(2):
default: printf("yes\n");
printf("default\n"); case max(1):
} printf("no\n");
} break;
a) yes }
b) yes default }
c) Undefined behaviour a) yes no
d) Compile time error b) yes
Answer: d c) no
d) Compile time error
Answer: c
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Quiz
Time

9. What will be the output of the following C code?


#include <stdio.h> 10. What will be the output of the following C
int main() code?
{ #include <stdio.h>
switch (printf("Do")) int main()
{ {
case 1: int a = 1;
printf("First\n"); switch (a)
break; {
case 2: case a:
printf("Second\n"); printf("Case A ");
break; default:
default: printf("Default");
printf("Default\n"); }
break; }
} a) Output: Case A
} b) Output: Default
a) Do c) Output: Case A Default
b) DoFirst d) Compile time error
c) DoSecond Answer: d
d) DoDefault Answer: c
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Assignments
 Write a menu driven program using switch case which has following option:
1. Addition of numbers 2. Multiplication of numbers
3. Subtraction of numbers 4. Division of numbers
5. Modulus of numbers
 Write a program to input any number b/w 1 to 12 and print appropriate month name.
(Assume 1 is January)

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


References

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Thank You
Any Query

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia

You might also like