Answer to Question #11247 in C++ for Chad Dylan Cooper

Question #11247
What is the value of val after the following C++ code executes?

How can I do this?

val = 7;
switch (val % 4)
{
case 3:
val = val + 3;
case 1:
val++;
break;
case 5:
val = val + 5;
case 4:
val = val + 4;
default;
val--;
1
Expert's answer
2012-07-05T07:46:41-0400
#include <iostream>
using namespace std;
int main()
{
int val
= 7;
switch (val % 4)
{
case 3:
val = val + 3; // this,
so getting val = 7 + 3 = 10
case 1:
val++; // this ('cause
there were no break in case 3), so
getting val = 10 + 1 = 11

break;
case 5:
val = val + 5;
case 4:
val
= val + 4;
default:
val--;
}
cout << val <<
endl;
return 0;
}

Result: 11.

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS