1. ## Switch Statements

Hi,

I'm a beginner and lost on my assignment already--stuck on the switch statements. I've looked at the tutorials already but I still can't find my answer so I'm here. My assignment is a quiz so I can't really ask specific questions. I'm just confused about some parts. I know that in the if/else statement, you can have eqns like y=x+3 and then enter in an x value in the if statement to calculate what y is. I've been trying to do the same for the switch statement but I can't.

Also, I want to add all the cases together, like case 1 = case 1+case2+case3, then case 2 = case 2 +case3

with the cases being numbers of course. any help is appreciated, thanks.

2. I've looked at the tutorials already
As in Lesson 5: switch case?

I know that in the if/else statement, you can have eqns like y=x+3 and then enter in an x value in the if statement to calculate what y is. I've been trying to do the same for the switch statement but I can't.
You might want to give an example of what you mean in terms of if/else.

Also, I want to add all the cases together, like case 1 = case 1+case2+case3, then case 2 = case 2 +case3
What do you mean by "add"? One feature (or 'misfeature', depending on your point of view) is that of 'fall-through', as in once a case is reached, control moves to the following case in the absence of say, a break.

3. I'm not clear on your first question. If I understand the second one, you can do
Code:
```switch (n) {
case 1:
case 2:
case 3:
// do something
break;
case 4:
case 5:
// do something else
break;
}```
You're just using switch's fallthrough.

4. Yes, tutorial 5.

Example:

if(x=5)
y=x+3;
printf("y = &#37;f\n",y);

I don't know how to insert equations into the switch statement. Is that even allowed in the switch statement? To calculate an eqn?

I tried the the fall through one (I did it without break; ), but it didn't add the numbers together. All it did was list them together.

5. I think you want "x == 5" (one equals is assignment).

7. I presume you actually mean:
Code:
```if (x == 5)
{
y = x + 3;
printf("y = &#37;f\n", y);
}```
In a switch, this might be:
Code:
```switch (x)
{
case 5:
y = x + 3;
printf("y = %f\n", y);
break;
}```
Or, if you are declaring a variable within a case:
Code:
```switch (x)
{
case 5:
{
double y = x + 3;
printf("y = %f\n", y);
}
break;
}```
I note that in C++, we usually use output streams like std::cout instead of C-style output functions like printf().

I tried the the fall through one (I did it without break; ), but it didn't add the numbers together. All it did was list them together.

8. I've only seen printf(), never std::cout, have no idea why.

Add the results from each case. Like if I calculate:

y (case 1) to be = 5
y (case 2) = 4
y (case 3) = 3

Then when I do the switch statement and call on case 1, without using break, it would give me y = 12. But instead, all it did was give me something like:

y = 5
y = 4
y = 3

rather than y = 12.

9. Perhaps you are looking for something like this:
Code:
```int y = 0;
switch (x)
{
case 1: y += 5;
case 2: y += 4;
case 3: y += 3;
}```
But honestly, if you are not cautious and prudent in your use of fall through it can get more confusing than it is worth.

10. > I've only seen printf(), never std::cout, have no idea why.
Well, this is the C++ forum, and std::cout is the C++ counterpart to printf() (you can still use printf() but it's discouraged).
I think what you're trying to do is to add something to y in each case. In that case, you would want something like "y += 5;" in case 1, "y += 4;" in case 2, etc. You also need to initialize y to 0 before the switch statement.

11. Sorry but I have another question. If I use y in the switch statement, then I'd identify it in the beginning as:

double y;

right?

Because for some reason, all my ys are zeros now.

12. Because for some reason, all my ys are zeros now.