Find the value of x!

Hello everyone!

is there any easy way to solve this ?!

using Switch Statement find the value of x whereas :

X = A + B if j = 1
X = A - b if j = 2
X = A * B if J = 3
X = A / B if j = 4
Yes
possible to show me how .. little bit confused lol
switch(j) //j must be of an integer type
{
case 1: //what j equals. if j ==1, do things...
x=a+b;
break; //if you leave breaks off it does the next one. so if j=1 you would get x - a-b here without...
case 2:
x = a-b;
// etc...

}
Note the use of a break statement at the end of each case. Once the switch variable (j) matches a case, all statements in all subsequent cases are executed. Remove one or two of the break statements to see what happens.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main(void)
{

int j;
double a=7, b=11, x;

for( j=1; j<=4; j++ )
	{
	switch (j)
		{
		case 1: x=a+b; break;
		case 2: x=a-b; break;
		case 3: x=a*b; break;
		case 4: x=a/b; break;
		};
	std::cout << j << " " << x << std::endl;
	}

return 0;
}
Topic archived. No new replies allowed.