Home Classroom C++ C++ with Barney! Class 5

C++ with Barney! Class 5


Switch – case


[You're Ted and you decide to learn some C++ from your friend Barney. This is the fifth day of the class]

You: Hi, Barney!

Barney: Oh hi, Ted. How are you doing?

You: Great! And you?

Barney: Not bad at all. Come along. Let’s continue with our C++. So, where did we stop last time?

You: Mmm, we finished loops.

Barney: Ah, okay. Did we talk about the switch-case structure?

You: No.

Barney: Alright, let’s have a look at that today. I’m a bit busy today so today’s lesson will be a bit short. The switch-case structure is used to make decisions, just like the if-else statements. Its syntax goes like this:

switch (expression) {

    case const1:



case const2;







You: Oh, that’s real greek!

Barney: You think so? Not after the next example. Let’s write a programme to illustrate this.



using namespace std;

int main()


int num;

cout<<”Enter a number: “;



switch (num) {

case 1:

cout<<”That’s one!”;


case 2:

cout<<”The smallest prime!”;


case 3:case 4:

cout<<”Less than 5″;



cout<<”That’s an odd number to think about!”;




return 0;


You: Hmm, isn’t it something like that if-else thing?

Barney: Exactly. Switch-case does exactly what the if-else does. This programme outputs “That’s one” when num is equal to one, “The smallest prime” when num is two, “Less than 5” when num is 3 or 4, and “That’s an odd number” for any other value of num.

You: Right, but I don’t think I still get how it has been done.

Barney: Alright, let’s go step by step. First the user is prompted to enter an integer. Next we ask the compiler to switch to num. What do you think that means?

You: May be we’re asking it to consider the variable num?

Barney: Exactly. We’re telling it that the following statements will have to deal with the value of num so take note of it. Then we break down the cases. How many cases do you see here?

You: Mmm, three, four?

Barney: Five, to be precise. It’s like this. The first case activates when num is equal to one, the second when num is two. When we want several cases to do the same thing we group them as in

case 3: case 4:

We can add more to this list, of course, like:

case 3: case 4: case 5: case 6:

and so on. And when none of these cases are satisfied the default case is activated.

You: I still don’t completely get it. What are these break statements for?

Barney: Suppose the user enters 2. Then when the compiler enters the switch statement it sees that the case 1 is not satisfied, so it skips it. Then it finds that case 2 is satisfied, so it starts executing the commands under it. When it lands on the break statement it exits from the switch structure. Otherwise, it will keep executing the next commands as well.

You: Ah, okay. Then we need to put a break at the end of every case statement?

Barney: In most cases, yes. But there may be situations when you want to execute the rest of the structure, too, so keep that on mind.

You: And the default case is like the else in the if-else structure?

Barney: Exactly. It is executed when no other cases are satisfied. However it is not compulsory to add a default case.

You: Okay, it’s somewhat clear now. Can we check characters with switch-case?

Barney: Of course! Let’s try an example. This will be a little tricky, so try to understand properly.



using namespace std;

int main()


char c;

int n=0;

do {

cout<<”This is a loop. Do you want to continue [Y/N]? “;


switch(c) {

case ‘Y’: case ‘y’:


case ‘N’: case ‘n’:






} while (n<1);


return 0;


You: It doesn’t seem that difficult. This will keep repeating until we press N, right?

Barney: Yeah, you’re catching up fast! Let’s run it and see.


You: Hmm, exactly as I imagined. So can we use this swich statements to compare strings?

Barney: No, it’s not a good idea to compare strings with this. When we are learning strings we will see how to handle them. And we should not compare floats and doubles also with switch.

You: Why is that? Aren’t they just numbers?

Barney: These are called floating point numbers. We usually don’t “equate” them. That’s because you can never be sure that a floating point number will be exactly some value. In most cases they are “around” and “close” that value.

You: Okay, then. Just asked.

Barney: Alright, let’s wind up for today now. Next day’s lesson will be a big one. So don’t miss it!

You: Aniwaa! Thanks a lot! Good bye, Barney!

Barney: Good bye, Ted!



Engineer. Loves tic-tac.


Leave a Reply