0% found this document useful (0 votes)
25 views5 pages

Switch

The switch statement compares a variable to multiple case values and executes the block of code associated with the matching case. It allows executing different blocks of code based on different conditions. The variable is compared to each case label until a match is found. When a match occurs, the associated block of code executes and control jumps to the end of the switch statement. A break statement at the end of each case block prevents fall-through to the next case. An optional default case handles the scenario when no other cases match.

Uploaded by

Piron Live
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)
25 views5 pages

Switch

The switch statement compares a variable to multiple case values and executes the block of code associated with the matching case. It allows executing different blocks of code based on different conditions. The variable is compared to each case label until a match is found. When a match occurs, the associated block of code executes and control jumps to the end of the switch statement. A break statement at the end of each case block prevents fall-through to the next case. An optional default case handles the scenario when no other cases match.

Uploaded by

Piron Live
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/ 5

SWITCH statement

The switch statement tests the value of a variable and compares it with multiple
cases. Once the case match is found, a block of statements associated with that
particular case is executed.
Each case in a block of a switch has a different name / number which is referred to
as an identifier. The value provided by the user is compared with all the cases inside the
switch block until the match is found.
If a case match is found, then the default statement is executed, and the control
goes out of the switch block.

A general syntax of how switch-case is implemented in a 'C' program is as follows:


switch (expression)
{
case value1:
Block1;
Break;
case value2:
Block2;
Break;
case valuen:
Blockn;
Break;
default:
Block;
Break;
}
Statement - x;

 The expression can be integer expression or a character expression.


 value1, 2, n are case labels which are used to identify each case individually.
Remember that case labels should not be same as it may create a problem
while executing a program. Suppose we have two cases with the same label
as '1'. Then while executing the program, the case that appears first will be
executed even though you want the program to execute a second case. This
creates problems in the program and does not provide the desired output.
 Case labels always end with a colon ( : ). Each of these cases is associated
with a block.
 A block is nothing but multiple statements which are grouped for a
particular case.
 Whenever the switch is executed, the value of test-expression is compared
with all the cases which we have defined inside the switch. Suppose the test
expression contains value 4. This value is compared with all the cases until
case whose label four is found in the program. As soon as a case is found the
block of statements associated with that particular case is executed and
control goes out of the switch.
 The break keyword in each case indicates the end of a particular case. If we
do not put the break in each case then even though the specific case is
executed, the switch will continue to execute all the cases until the end is
reached. This should not happen; hence we always have to put break
keyword in each case. Break will terminate the case once it is executed and
the control will fall out of the switch.
 The default case is an optional one. Whenever the value of test-expression is
not matched with any of the cases inside the switch, then the default will be
executed. Otherwise, it is not necessary to write default in the switch.
 Once the switch is executed the control will go to the statement-x, and the
execution of a program will continue.

Following diagram illustrates how a case is selected in switch case:

switch

break +
case 1 BLOCK 1
-

break +
case 2 BLOCK 2

break +
default default

statement-x

Rules for switch statement


 An expression must always execute to a result.
 Case labels must be constants and unique.
 Case labels must end with a colon ( : ).
 A break keyword must be present in each case.
 There can be only one default label.
 We can nest multiple switch statements.

E-OLYMP 8371. Even or Odd Determine if number n is even or odd.


► Use switch statement to solve the problem.
#include <stdio.h>

int n;

int main(void)
{
scanf("%d", &n);
switch (n % 2 == 0)
{
case 1:
puts("EVEN");
break;
case 0:
puts("ODD");
}
return 0;
}

E-OLYMP 902. The level of educational achievements Determine the level of


educational achievements for the pupil (elementary, average, sufficient, high) according
to the given grade (from 1 to 12).
► Let n (1 ≤ n ≤ 12) be the pupil’s grade. Use switch statement to solve the
problem.

#include <stdio.h>

int n;

int main(void)
{
scanf("%d", &n);

switch (n)
{
case 1:
case 2:
case 3:
printf("Initial\n");
break;
case 4:
case 5:
case 6:
printf("Average\n");
break;
case 7:
case 8:
case 9:
printf("Sufficient\n");
break;
default:
printf("High\n");
}

return 0;
}

E-OLYMP 923. Season Determine the season name by the month number.
► Let n (1 ≤ n ≤ 12) be the month number. Use switch statement to solve the
problem.
#include <stdio.h>
int n;

int main(void)
{
scanf("%d", &n);

switch (n)
{
case 1:
case 2:
case 12:
puts("Winter");
break;
case 3:
case 4:
case 5:
puts("Spring");
break;
case 6:
case 7:
case 8:
puts("Summer");
break;
default:
puts("Autumn");
}

return 0;
}

E-OLYMP 2044. Month Determine the month name by its number.


► Let n (1 ≤ n ≤ 12) be the month number. Use switch statement to solve the
problem.

E-OLYMP 8242. Positive negative or zero Given one integer n. Print is it


positive, negative or equals to 0.
► Use switch statement to solve the problem.
#include <stdio.h>

int n;

int main(void)
{
scanf("%d", &n);
switch (n > 0)
{
case 1:
puts("Positive");
break;
case 0:
switch (n < 0)
{
case 1:
puts("Negative");
break;
default:
puts("Zero");
}
}
return 0;
}

E-OLYMP 8319. Simple Calculator Implement a simple calculator with +, -, *


and /.
► Let c is a sign character (‘+’, ‘-‘, ‘*’, ‘/’). Use switch statement to solve the
problem.
#include <stdio.h>

int a, b, res;
char c;

int main(void)
{
scanf("%d %c %d", &a, &c, &b);

switch (c)
{
case '+':
res = a + b;
break;
case '-':
res = a - b;
break;
case '*':
res = a * b;
break;
case '/':
res = a / b;
}

printf("%d\n", res);
return 0;
}

You might also like