0% found this document useful (0 votes)
8 views16 pages

Lecture 17

The document explains the switch statement in programming, which allows for multiple branching based on a condition. It details the structure of the switch statement, including the use of case labels and the requirement for constant values. Several examples illustrate how to implement switch statements to handle different scenarios and outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views16 pages

Lecture 17

The document explains the switch statement in programming, which allows for multiple branching based on a condition. It details the structure of the switch statement, including the use of case labels and the requirement for constant values. Several examples illustrate how to implement switch statements to handle different scenarios and outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Decision Making,

Branching
Switch and Break
The switch Statement

➢Switch is multiple–branching statement where based on a


condition, the control is transferred to one of the many possible
points.

➢ Enables the program to execute different statements based on


an expression that can have more than two values. Also called
multiple choice statements.

➢The expression provided in the switch should result in a constant


value otherwise it would not be valid.

10/2/2023 2
The switch statement
switch ( expression )
{ The expression is
case value1: successively compared
program statement against the values value1,
program statement value2, ..., valuen. If a case is
... found whose value is equal
break; to the value of expression,
case value2: the program statements that
program statement follow the case are
program statement executed.
...
break;
case value n: The switch test expression must be one
program statement with an integer value (including type
program statement char) (No float !).
...
break; The case values must be integer-type
default: constants or integer constant
program statement expressions (You can't use a variable for
program statement a case label !)
...
}
10/2/2023 3
switch- control flow

10/2/2023 4
switch- example 1
#include<stdio.h>
int main()
{
int choice;
printf(“Enter your choice: 1-yes, 2-no\n”);
scanf(“%d”,&choice);
switch(choice)
{
case 1: printf(“YESSSSSSS……”);
break;
case 2: printf(“NOOOOOO……”);
break;
default: printf(“DEFAULT CASE…….”);
} printf(“The choice is %d”,choice);
return 0;
}
10/2/2023 5
switch- example 2
scanf(“%d”,&mark);

case 50:
switch (mark)
grade=‘C’
{
break;
case 100: case 40:
case 90: grade=‘D’
case 80: grade=‘A’; break;
break;
default: grade=‘F’;
case 70: break;
case 60: }
printf(“%c”,grade);
grade=‘B’;
break;

10/2/2023 6
An Example – switch case
char ch;
scanf)(“%c”,&ch);

switch(ch)
{
case ‘a’ : printf(“Vowel”);
break;
case ‘e’ : printf(“Vowel”);
break;
case ‘i’ : printf(“Vowel”);
break;
case ‘o’ : printf(“Vowel”);
break;
case ‘u’ : printf(“Vowel”);
break;
default: printf(“Not a Vowel”); }

10/2/2023 7
An Example – switch case
char ch;
scanf(“%c”,&ch);

switch(ch)
{
case ‘a’:
case ‘e’:
case ‘i’ :
case ‘o’ :
case ‘u’ : printf(“Vowel”);
break;
default: printf(“Not a Vowel”); }

10/2/2023 8
Example - switch
/* Program to evaluate simple expressions case ‘*':
of the form value operator value */ result=value1*value2;
#include <stdio.h> printf(“%f”,result);
int main (void) break;
{ float value1, value2; case ‘/':
char operator; if ( value2 == 0 )
float result; printf(“Division by
printf("Type in your expression.\n“); zero.\n“);
scanf(“%f %c %f”, else result=value1 / value2;
&value1,&operator,&value2); printf(“%f”,result);
switch (operator) break;
{case '+': default;
result=value1+value2; printf(“Unknown Operator”);
printf(“%f”,result);
}
break;
return 0;
case '-':
result=value1-value2; }
printf(“%f”,result);
break;
10/2/2023 9
What is the output of the following code snippet?

int iNum = 2;
switch(iNum)
{
case 1:
printf(“ONE”);
break;
case 2:
printf(“TWO”);
break;
case 3:
printf(“THREE”);
break;
default:
printf(“INVALID”);
break;
}

10/2/2023 10
What is the output of the following code snippet?

iNum = 2;
switch(iNum)
{
default:
printf(“INVALID”);
case 1:
printf(“ONE”);
case 2:
printf(“TWO”);
break;
case 3:
printf(“THREE”;)
}

10/2/2023 11
What is the output of the following code snippet?

switch (iDepartmentCode)
{
case 110 : printf(“HRD ”);
case 115 : printf(“IVS ”);
case 125 : printf(“E&R ”);
case 135 : printf(“CCD ”);
IVS E&R CCD
}

Assume iDepartmentCode is 115


find the output ?

10/2/2023 12
What is the output of the following code snippet?
int iNum = 2;
switch(iNum)
{
case 1.5:
printf(“ONE AND HALF”);
break;
case 2:
printf(“TWO”);
case ‘A’ :
printf(“A character”);
}

10/2/2023 13
Check Validity:
• switch(1+2+23)
• switch(1*2+3%4)
• switch(a*b+c*d)
• switch(a+b+c)

Important Points:
• Duplicate case values are not allowed
• Nesting of switch statements is allowed

10/2/2023 14
int main()
{
int x = 1;
switch (x)
{
x = x + 1;
case 1: printf("Choice is 1");
break;
case 2: printf("Choice is 2");
break;
default: printf("Choice other than 1 and 2");
break;
}
return 0;
}

Output:: Choice is 1
10/2/2023 15
int main()
{
int x = 1;
switch (x)
{
case 2: printf("Choice is 1");
break;
case 1+1: printf("Choice is 2");
break;
}
return 0;
}

Output::

Compiler Error: duplicate case value

10/2/2023 16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy