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

Cs112 - Programming Fundamental: Lecture # 17 - Selection Control in C Syed Shahrooz Shamim

The document discusses the switch-case statement in C programming, which provides an alternative to long if-else statements by allowing a variable to be compared to multiple constant values, with different code blocks executing depending on which case matches the variable's value; it outlines the basic format and syntax of switch-case statements, including the use of break to prevent fall-through between cases; and it notes some limitations of switch compared to if-else statements as well as advantages like improved clarity and potential for faster execution through compiler optimizations.

Uploaded by

Ghazan Aqeel
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Cs112 - Programming Fundamental: Lecture # 17 - Selection Control in C Syed Shahrooz Shamim

The document discusses the switch-case statement in C programming, which provides an alternative to long if-else statements by allowing a variable to be compared to multiple constant values, with different code blocks executing depending on which case matches the variable's value; it outlines the basic format and syntax of switch-case statements, including the use of break to prevent fall-through between cases; and it notes some limitations of switch compared to if-else statements as well as advantages like improved clarity and potential for faster execution through compiler optimizations.

Uploaded by

Ghazan Aqeel
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

CS112 - PROGRAMMING FUNDAMENTAL

Lecture # 17 – Selection Control in C


Syed Shahrooz Shamim
Junior Lecturer,
CS Department, UIT
SWITCH - CASE
Switch Case
• In programming,
a switch, case, select or inspect statement is a type
of selection control mechanism that exists in most
imperative programming languages such
as Pascal, Ada, C/C++, C#, Java, and so on.
• The main reasons for using a switch include
improving clarity, by reducing otherwise repetitive
coding, and (if the heuristics permit) also offering
the potential for faster execution through
easier compiler optimization in many cases.
Switch Case
• The switch-case statement is a multi-way decision
statement. Unlike the multiple decision statement
that can be created using if-else,
the switch statement evaluates the
conditional expression and tests it against
numerous constant values.
Switch Case
switch ( <variable> ) {
case this-value:
Code to execute if <variable> == this-value
break;
case that-value:
Code to execute if <variable> == that-value\
break;
...
default:
Code to execute if <variable> does not equal the value
following any of the cases
break;
}

Switch case statements are a substitute for long if statements that compare a
variable to several "integral" values ("integral" values are simply values that can be
expressed as an integer, such as the value of a char). The basic format for using
switch case is outlined below. The value of the variable given into switch is compared
to the value following each of the cases, and when one value matches the value of
the variable, the computer continues executing the program from that point.
Switch Case
SYNTAX
switch(variable||constant||character)
{
case 1:
statement1;
statement2;
break;
case 2:
statements;
break;
.
.
default:
statements;
}
Switch Case
• The condition of a switch statement is a value. The case says that if it
has the value of whatever is after that case then do whatever follows
the colon.

• The break is used to break out of the case statements. Break is a


keyword that breaks out of the code block, usually surrounded by
braces, which it is in. In this case, break prevents the program from
falling through and executing the code in all the other case
statements. An important thing to note about the switch statement is
that the case values may only be constant integral expressions.
int a = 10;
int b = 10;
int c = 20;

switch ( a ) {
case b:
/* Code */
break;
case c:
/* Code */
break;
default:
/* Code */
break;
}
Switch Case
• The default case is optional, but it is wise to include it as it
handles any unexpected cases. It can be useful to put some
kind of output to alert you to the code entering the default case
if you don't expect it to. Switch statements serve as a simple
way to write long if statements when the requirements are met.
Often it can be used to process input from a user.
Switch Case
Example.. default:
void main() printf(“I am in
{ default\n”);
char ch=‘x’; }
switch(ch) }
{
case ’v’:
printf(“In case v ”);
break;
case ’a’:
printf(“In case a ”);
break;
case ’x’:
printf(“In case x ”);
break;
Switch Case
• Switch statement can also contain Expressions
eg switch(i+j*k)
switch(45*j)

• Only constant expressions can be evaluated in case


eg case 5+9 is correct
case a+b is incorrect
Limitation of switch
• A float Expression cannot be tested using a switch.
• Multiple cases cannot use same expression values.
eg
switch(var)
{
case 6:
…………..
.
.
case 3*2:
}
It is an Illegal Expression
• case a>2: is an illegal expression
Limitations of switch over if-else ladder
Advantages of switch over if-else ladder

You might also like