Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Switch, Break | Control Statements
C Basics
course content

Course Content

C Basics

C Basics

1. Introduction
2. Data
3. Operators
4. Control Statements
5. Functions
6. Pointers

bookSwitch, Break

Using the switch Statement

Imagine you're buying a soda from a vending machine. After you select your desired drink, deep within the vending machine's computer, the user_input variable takes on one of several predefined values. Each of these options is termed a case, and this is where the switch statement comes into play.

Think of the switch statement as another version of the if-else statement. It acts in response to specific values you've previously defined.

Here's how the structure of a switch statement looks:

Let's consider a vending machine example. Suppose there are three buttons to select different types of chips:

  • Cheese-flavored chips;
  • Bacon-flavored chips;
  • Chili-flavored chips.
c

Main

copy
12345678910111213141516171819202122232425
#include <stdio.h> int main() { int userInput = 2; switch (userInput) { case 1: printf("You selected cheese-flavored chips.\n"); break; case 2: printf("You selected bacon-flavored chips.\n"); break; case 3: printf("You selected chili-flavored chips.\n"); break; default: printf("You selected another item.\n"); } return 0; }

If the tested expression doesn't match any of the listed cases, the default case is executed. If there's no default case provided, the program simply continues its flow.

Note

The tested expression in a switch statement can only be of integer or char type. You can't use variables, strings, or non-integer data types as cases.

c

Main

copy
12345678910111213141516171819202122232425
#include <stdio.h> int main() { char userInput = 'y'; switch (userInput) { case 'a': printf("You entered 'a' character\n"); break; case 'b': printf("You entered 'b' character\n"); break; case 'c': printf("You entered 'c' character\n"); break; default: printf("You entered unknown character\n"); } return 0; }

The Role of break

The break command stops the current block's execution and moves on to the next segment of code. Essentially, once the relevant case is complete, you exit that block and continue with your program.

Without the break command, the switch statement would run continuously, and you'd likely end up with unintended results.

c

Main

copy
12345678910111213141516171819202122232425
#include <stdio.h> int main() { char userInput = 'b'; switch (userInput) { case 'a': printf("You entered 'a' character\n"); // without break case 'b': printf("You entered 'b' character\n"); // without break case 'c': printf("You entered 'c' character\n"); // without break default: printf("You entered unknown character\n"); } return 0; }

In the absence of the break command, the program starts executing immediately after finding a matching case and continues until it finds a break or reaches the end of the switch.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 4
We're sorry to hear that something went wrong. What happened?
some-alt