1. ## If Statements

I am working on the practice multiple choice problems for the AP Computer Science exam.

I came across this question:

4. At a certain high school students receive letter grades based on the following
scale.
93 or above A
From 84 to 92 inclusive B
From 75 to 83 inclusive C
Below 75 F

Which of the following code segments will assign the correct string to grade
for a given integer score ?

I. if (score .5 93)
if (score .5 84 && score ,5 92)
if (score .5 75 && score ,5 83)
if (score , 75)

II. if (score .5 93)
if (84 ,5 score ,5 92)
if (75 ,5 score ,5 83)
if (score , 75)

III. if (score .5 93)
else if (score .5 84)
else if (score .5 75)
else

(a) II only
(b) III only
(c) I and II only
(d) I and III only
(e) I, II, and III

Letter D is the correct answer. My question is why is II wrong? Wouldn't that code execute fine or is there a problem with the if Statements?

2. ## Re: If Statements

What language is that code? it certainly isn't valid Java code.

db

3. ## Re: If Statements

well it's java. but this isn't the entire code. its just the question. its like a part of a bigger program we don't get to see i guess. We are just supposed to say whether I, II, III or all of them would complete the task. Does that clear things up?

4. ## Re: If Statements

Originally Posted by katymccl
well it's java.
No, it isn't.

but this isn't the entire code. its just the question.
If there's no code, it isn't Java. Nor any other programming language.

Heck, what you posted can't even be considered valid pseudocode.

db

5. ## Re: If Statements

At a guess, I would say you lazily copied that from a source which used images for mathematical operators, and the images didn't get pasted here. And you didn't bother to replace them with their textual equivalents either.

db

6. Computer Science Student
Join Date
Feb 2012
Location
Savannah, GA
Posts
4
Rep Power
0

## Re: If Statements

I think the problem is with the logic in the if statement parameters. Someone correct me if I'm wrong, but you can't chain logic the way it's chained in II. You have to define the left separate from the right. That is, you can't use "5 < x < 10" but have to state "5 < x" and "x < 10."

--SM

7. ## Re: If Statements

My guess is that ".5" means ">=" and ",5" means "<="; the text was probably copied some way from an image; the OP should've checked the results ...

kind regards,

Jos