Simplifying Tests

Hello! I've got this one bit of code from a test my teacher gave me a while back, and after finishing the class, I still can't seem to find a solution to it. The following is the code from the problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
float warp;
cout << "Enter your current speed : ";
cin >> warp;

	// Decision structure:
if (warp < 0.0)
	cout << "Going nowhere fast.\n";
else if (warp >= 0.0 && warp < 1200.0)
	cout << "Moderate speed.\n";
else if (warp >= 1200.0 && warp < 2400)
	cout << "Moving fast!\n";
else if (warp >= 2400)
cout << "Burning up!\n";


I'm supposed to get rid of unnecessary tests in the decision structure, but I can't seem to figure out the best way to go about it. Note that we did not know arrays, vectors, class, and structs at at time, so the solution we came up with shouldn't have any of those. Any help?
Last edited on
The last decision is unnecessary.

else if (warp >= 2400)

because if it doesn't fit into any of the other ranges at the end of the test case, what else can it be? That's right, it MUST be greater than 2400, so to test it is redundant.

It should just be

1
2
else
    cout << "Burning up!\n";
Last edited on

It should just be

1
2
else
    cout << "Burning up!\n";



Thank you! I knew it had to be something simple I was missing this whole time.
That's not the only place where you have a redundant test.

Look, for example, at line 8:

else if (warp >= 0.0 && warp < 1200.0)

There's a part of that test that's unnecessary, because if execution even reaches that test, then part of the test cannot be false.

Can you figure out which part?
Topic archived. No new replies allowed.