Program Statements: Of the following if statements, which one correctly executes three instructions if the condition is true?


Of the following if statements, which one correctly executes three instructions if the condition is true?


a) if (x < 0)

a = b * 2;

y = x;

z = a – y;

b) {

if (x < 0)

a = b * 2;

y = x;

z = a – y;

}

c) if { (x < 0)

a = b * 2;

y = x;

z = a – y ;

}

d) if (x < 0)

{

a = b * 2;

y = x;

z = a – y;

}

e) b, c and d are all correct, but not a

Answer: d. Explanation: In order to have three instructions execute in the if clause when the condition is true, the three statements must be placed in a block following the condition. This is the syntax used in d. In a, there is no block. In b, the block is placed around the entire if statement such that the if clause is only a = b * 2; and the other two statements are not part of the if statement, but follow it. The syntax in c is illegal resulting in a syntax error.
Don’t forget that the structure of your code (how it lines up) is immaterial to the compiler.