# C - Switch on integer and choose three winning numbers

## Introduction

The following code chooses winning numbers.

Users are required to guess a winning number, and the switch statement tells them about any valuable prizes they may have won:

## Demo

```#include <stdio.h>

int main(void)
{
int choice = 0;            // The number chosen

// Get the choice input
printf("Pick a number between 1 and 10! ");
scanf("%d", &choice);

// Check for an invalid selection
if ((choice > 10) || (choice < 1))
choice = 11;         // Selects invalid choice message
switch (choice)
{//from  ww  w  .  j  a v a  2  s.  c  o m
case 7:
printf("Congratulations!\n");
printf("You win :seven.\n");
break;                 // Jumps to the end of the block

case 2:
printf("You win :two.\n");
break;                 // Jumps to the end of the block

case 8:
printf("You win :eight.\n");
break;                 // Jumps to the end of the block

case 11:
printf("Try between 1 and 10. You wasted your guess.\n");
// No break - so continue with the next statement

default:
printf("Sorry, you lose.\n");
break;                 // Defensive break - in case of new cases
}
return 0;
}
```

## how It Works

We have the switch statement, which will select the value of choice:

```switch(choice)
{
. . .
}
```

if choice has the value 7, the case corresponding to that value will be executed:

```case 7:
printf("Congratulations!\n");
printf("You win :seven.\n");
break;                        // Jumps to the end of the block
```

The two printf() calls are executed, and the break will jump to the statement following the closing brace for the block.

The next case is a little different:

```case 11:
printf("Try between 1 and 10. You wasted your guess.\n");
// No break - so continue with the next statement
```

There's no break statement, so execution continues with the printf() for the default case after displaying the message.

The default case is:

```default:
printf("Sorry, you lose.\n");
break;                 // Defensive break - in case of new cases
```

This will be selected if the value of choice doesn't correspond to any of the other case values.