0% found this document useful (0 votes)
54 views12 pages

EXERCISE - Switch Statements

The document contains code examples that use switch statements to evaluate different scores and output corresponding level labels. The examples demonstrate how the switch statement works and how changing the conditions evaluated (e.g. using different score/level calculations or switch variables) impacts the output. Bugs are illustrated when conditions are incorrect, like missing breaks or invalid syntax.

Uploaded by

Tc Umt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views12 pages

EXERCISE - Switch Statements

The document contains code examples that use switch statements to evaluate different scores and output corresponding level labels. The examples demonstrate how the switch statement works and how changing the conditions evaluated (e.g. using different score/level calculations or switch variables) impacts the output. Bugs are illustrated when conditions are incorrect, like missing breaks or invalid syntax.

Uploaded by

Tc Umt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

SWITCH STATEMENTS

Trace the below codes and find out why these outputs are produced!

#include <stdio.h>
#define SUCCESS_LEVEL 20

void main ()
{
int score=100, level;

level = score / SUCCESS_LEVEL;


printf("Your level is %d", level);

switch(level)
{
case 100:
printf(" Very Successful!");
break;
case 80:
printf(" Successful!");
break;
case 60:
printf(" Moderate!");
break;
case 40:
printf(" Not Satisfactory!");
break;
case 20:
printf(" Too Bad!");
break;

OUTPUT:
Your level is 5
#include <stdio.h>
#define SUCCESS_LEVEL 20

void main ()
{
int score=100, level;

level = score / SUCCESS_LEVEL;


printf("Your score is %d and your calculated level is %d\n", score, level);

switch(level)
{
case 1:
printf(" Very Successful!");
break;
case 2:
printf(" Successful!");
break;
case 3:
printf(" Moderate!");
break;
case 4:
printf(" Not Satisfactory!");
break;
case 5:
printf(" Too Bad!");
break;
default:
printf(" There must be some error in your score!");
break;

OUTPUT:
Your score is 100 and your calculated level is 5
Too Bad!
#include <stdio.h>
#define SUCCESS_LEVEL 20

void main ()
{
int score=100, level;

level = score / SUCCESS_LEVEL;


printf("Your score is %d and your calculated level is %d \n", score, level);

switch(5-level)
{
case 1:
printf(" Very Successful!");
break;
case 2:
printf(" Successful!");
break;
case 3:
printf(" Moderate!");
break;
case 4:
printf(" Not Satisfactory!");
break;
case 5:
printf(" Too Bad!");
break;
default:
printf(" There must be some error in your score or level!");
break;

OUTPUT:
Your score is 100 and your calculated level is 5
There must be some error in your score or level!
#include <stdio.h>
#define SUCCESS_LEVEL 20

void main ()
{
int score=100, level;

level = score / SUCCESS_LEVEL;


printf("Your score is %d and your calculated level is %d \n", score, level);

switch(6-level)
{
case 1:
printf(" Very Successful!");
break;
case 2:
printf(" Successful!");
break;
case 3:
printf(" Moderate!");
break;
case 4:
printf(" Not Satisfactory!");
break;
case 5:
printf(" Too Bad!");
break;
default:
printf(" There must be some error in your score or level!");
break;

OUTPUT:
Your score is 100 and your calculated level is 5
Very Successful!
#include <stdio.h>
#define SUCCESS_LEVEL 20

void main ()
{
int score=100, level;

level = score / SUCCESS_LEVEL;


printf("Your score is %d and your calculated level is %d \n", score, level);

switch(6-level)
{
case 1:
printf(" Very Successful!");

case 2:
printf(" Successful!");
break;
case 3:
printf(" Moderate!");
break;
case 4:
printf(" Not Satisfactory!");
break;
case 5:
printf(" Too Bad!");
break;
default:
printf(" There must be some error in your score or level!");
break;

OUTPUT:
Your score is 100 and your calculated level is 5
Very Successful! Successful!
#include <stdio.h>
#define SUCCESS_LEVEL 5

void main ()
{
int score=100, level;

level = score % SUCCESS_LEVEL;


printf("Your score is %d and your calculated level is %d \n", score, level);

switch(level)
{
case 1:
printf(" Very Successful!");
break;
case 2:
printf(" Successful!");
break;
case 3:
printf(" Moderate!");
break;
case 4:
printf(" Not Satisfactory!");
break;
case 5:
printf(" Too Bad!");
break;
default:
printf(" There must be some error in your score or level!");
break;

OUTPUT:
Your score is 100 and your calculated level is 0
There must be some error in your score or level!
#include <stdio.h>
#define SUCCESS_LEVEL 5

void main ()
{
int score=5, level;

level = score % SUCCESS_LEVEL;


printf("Your score is %d and your calculated level is %d \n", score, level);

switch(level)
{
case 0:
printf(" Very Successful!");
break;
case 1:
printf(" Successful!");
break;
case 2:
printf(" Moderate!");
break;
case 3:
printf(" Not Satisfactory!");
break;
case 4:
printf(" Too Bad!");
break;
default:
printf(" There must be some error in your score or level!");
break;

OUTPUT:
Your score is 5 and your calculated level is 0
Very Successful!
#include <stdio.h>
#define SUCCESS_LEVEL 5

void main ()
{
int score=5, level;

level = score % SUCCESS_LEVEL;


printf("Your score is %d and your calculated level is %d \n", score, level);

switch(level-1)
{

default:
printf(" There must be some error in your score or level!");

case 0:
printf(" Very Successful!");
break;
case 1:
printf(" Successful!");
break;
case 2:
printf(" Moderate!");
break;
case 3:
printf(" Not Satisfactory!");
break;
case 4:
printf(" Too Bad!");
break;

OUTPUT:
Your score is 5 and your calculated level is 0
There must be some error in your score or level! Very Successful!
#include <stdio.h>
#define SUCCESS_LEVEL 5

void main ()
{
int score=5, level;

level = score % SUCCESS_LEVEL;


printf("Your score is %d and your calculated level is %d \n", score, level);

switch(level-1)
{

default:
printf(" There must be some error in your score or level!");

case 0:
printf(" Very Successful!");

case 1:
printf(" Successful!");
break;
case 2:
printf(" Moderate!");
break;
case 3:
printf(" Not Satisfactory!");
break;
case 4:
printf(" Too Bad!");
break;

OUTPUT:
Your score is 5 and your calculated level is 0
There must be some error in your score or level! Very Successful! Successful!
#include <stdio.h>
#define SUCCESS_LEVEL 5

void main ()
{
int score=5, level;

level = score % SUCCESS_LEVEL;


printf("Your score is %d and your calculated level is %d \n", score, level);

switch(level)
{

default:
printf(" There must be some error in your score or level!");
break;
case 0:
printf(" Very Successful!");
break;
case 1:
printf(" Successful!");
break;
case 2:
printf(" Moderate!");
break;
case 3:
printf(" Not Satisfactory!");
break;
case 4,5:
printf(" Too Bad!");
break;

OUTPUT:
SYNTAX ERROR AT “case 4,5:”
|30|error: expected ':' or '...' before ',' token|
#include <stdio.h>
#define SUCCESS_LEVEL 5

void main ()
{
int score=3, level;

level = score % SUCCESS_LEVEL;


printf("Your score is %d and your calculated level is %d \n", score, level);

switch(level)
{

default:
printf(" There must be some error in your score or level!");
break;
case 0:
printf(" Very Successful!");
break;
case 1:
printf(" Successful!");
break;
case 2:
printf(" Moderate!");
break;
case 3:

case 4:
printf(" Too Bad!");
break;

OUTPUT:
Your score is 3 and your calculated level is 3
Too Bad!
#include <stdio.h>
#define SUCCESS_LEVEL 5

void main ()
{
int score=4, level;

level = score % SUCCESS_LEVEL;


printf("Your score is %d and your calculated level is %d \n", score, level);

switch(level)
{

default:
printf(" There must be some error in your score or level!");
break;
case 0:
printf(" Very Successful!");
break;
case 1:
printf(" Successful!");
break;
case 2:
printf(" Moderate!");
break;
case 3:

case 4:
printf(" Too Bad!");
break;

OUTPUT:
Your score is 4 and your calculated level is 4
Too Bad!

You might also like