EXERCISE - Switch Statements

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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