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

C++ with Barney! Class 5

1064

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:

    statements;

    break;

case const2;

    statements;

    break;

    …

    default:

    statements;

}

You: Oh, that’s real greek!

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

 

#include<iostream>

using namespace std;

int main()

{

int num;

cout<<”Enter a number: “;

cin>>num;

 

switch (num) {

case 1:

cout<<”That’s one!”;

break;

case 2:

cout<<”The smallest prime!”;

break;

case 3:case 4:

cout<<”Less than 5″;

break;

default:

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

}

cout<<endl;

system(“pause”);

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.

 

#include<iostream>

using namespace std;

int main()

{

char c;

int n=0;

do {

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

cin>>c;

switch(c) {

case ‘Y’: case ‘y’:

break;

case ‘N’: case ‘n’:

n=1;

break;

default:

cout<<”Error\n”;

}

} while (n<1);

system(“pause”);

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!

Comments

comments

Engineer. Loves tic-tac.

NO COMMENTS

Leave a Reply