C28. switch statement | switch vs if else

Опубликовано: 05 Май 2026
на канале: Tech Courses
132
27

0:00 switch statement
3:52 Important points
10:08 switch vs if else

In this video , we are going to discuss about switch statement in C.
It is also known as switch-case-default.
Summary:
switch allows us to choose from multiple choices
It is used as an alternative for if else statements

General Syntax :
switch(expression){
case constant1:
do some task;
break;
case constant2:
do some task;
break;
case constant3:
do some task;
break;
default:
do some task;
break;
}

expression should output some constant value
case is followed by constant value.
Each constant must be different from others.

Imp Points :
-Can put cases in any order
-Can use char value in case and switch
-Can combine cases
-Every statement must belong to some case
-default block is optional
-continue is not available in switch
-case continue to execute until break is encountered.

switch vs if else:
switch case can not use statements like case x less than 10
Similarly, expressions like 2+3 is allowed but a+b is not allowed
float is also not allowed in switch case
Switch is either more or as efficient as if else
Compiler generates a jump table for switch during compilation
If else are slower because conditions are evaluated at run time.
For multiway branching i.e. more cases with constant values, switch is better
For variable boolean results, if else is better.
In real projects, switch is always preferred over if else when possible.