Switch Statement
Switch Statement
The course aims to raise the programming skills of students via logic building capability
With the knowledge of C language students would be able to model real world problems
Course Outcomes
CO Course Outcome
Number
CO1 Remember the concepts related to fundamentals of C language,
draw flowcharts and write algorithm/pseudocode.
CO2 Understand the way of execution and debug programs in C
language.
CO3 Apply various constructs, loops, functions to solve mathematical
and scientific problem.
CO4 Analyze the dynamic behavior of memory by the use of pointers.
CO5 Design and develop modular programs for real world problems
using control structure and selection structure.
COURSE
SCHEME
WHY
WHEN
WHEN ?
So when you have multiple options available and you need to
decide one of all according to requirement match
HOW
HOW ?
It has its own structure where multiple switch cases are used. When
a particular match is found with a case, that particular case will
execute of all other.
WALKTHROUGH
RESOURCES
To what and why of switch statement
To the syntax representation of switch &
To the execution of the switch statement
LEARNING
LEARNING DIRECTIONS
DIRECTIONS
You would be able to apply the knowledge in solving multi-
task problems and menu driven programs
Sometimes we work with different options
Thus, switch statement can be used where multiple tasks are involved
It takes a variable and test the value of that variable with the multiple
SWITCH cases
There are 4 switch cases and suppose I gave value 4, the value will
STATEMENT matched with each switch case and the matching case will execute
( which is case 4)
Let’s cover the why and
when of switch
Switch case 1
Switch case 2
Switch case 3
Switch case 4
switch (expression)
{
case value1:
// statements
break;
SWITCH
case value2:
STUCTURE // statements
break;
Let’s go through the .
Structure of switch .
statement .
default:
// default statements
}
Default statement is
optional
switch (exp) let int exp = 2
{ switch (exp) let int exp = 2
Why case 1: {
// statement 1 case 1:
break ? break; // statement 1
case 2: case 2:
// statements 2 // statements 2
Break will stop break;
the control after case 3:
case 2 & case 3: //statement 3
statement 2 will //statement 3
be printed only default:
default: statements
statements }
}
All cases will execute after the
WITH BREAK matching case
int n=2;
switch(n+2) Output:
{ Default: value is: 2
case 1:
printf("Case1: Value is: %d", n);
case 2: Explanation: Since in the above program
printf("Case1: Value is: %d", n); example user gave value of n as 2 but the
case 3: expression inside switch is evaluated to 4 (n+2)
printf("Case1: Value is: %d", n); so, default case executed.
default:
printf("Default: Value is: %d", n);
}
QUESTIONS 5. C program to read weekday number and print weekday name using switch.
6. C program to check whether a character is VOWEL or CONSONANT using
switch statement.
7. C program to design calculator with basic operations using switch.
8. C program to check whether number is EVEN or ODD using switch.
5. Write a menu driven C program that will build a calculator.
1. Which of the following cannot be checked in a switch-case
(C programming)
A. Character
B. Integer
UTILISE YOUR C. Float
D. enum
KNOWLEDGE
TO ANSWER 2. What will be the output of following program?
#include <stdio.h>
Let us see how much you have void main ()
learned from the lecture and how {
effectively you can apply your int a=10;
knowledge…!! switch(a) {
case 5+5:
printf("Hello\n");
default:
printf("OK\n");
}
}
3. Can we use string value/variable in switch test condition?
switch(test_expression)
{
case VALUE1:
UTILISE YOUR case VALUE2:
case VALUE3:
KNOWLEDGE /*statements block 1*/
TO ANSWER break;
case VALUE4:
Let us see how much you have case VALUE5:
learned from the lecture and how /*statements block 2*/
effectively you can apply your break;
knowledge…!! }
A. Yes
B. No
DISCUSSION
FORUM
Utilize your knowledge
and think of a real-
world application that
you can build using a
switch case and also
that can contribute
something good to the
towards the real-world
problem solving. We have used and studied else if ladder which is also a multiway
decision maker than what u think why switch? what other pros we
can have with using a switch statement?
Reference Books
[1] Kanetkar, Y. (2010). Let us C. 15th ed.
[2] Thareja Reema (2014) Programming in C. 2 nd ed.
[3] Zed A. Shaw, Learn C the Hard Way’
REFERENCES
Reference Website
[1] https://www.guru99.com/c-switch-case-statement.html
[2] https://www.programiz.com/c-programming/ BOOKS
c-switch-case-statement
[3] https://www.tutorialspoint.com/cprogramming/ WEBSITES
switch_statement_in_c.html
COURSES
Online Courses
https://www.classcentral.com/course/swayam-introduction-to-
programming-in-c-2486
https://www.coursera.org/learn/c-for-everyone
THANK YOU….