Consider the following code segment: if ( Y<0 )
Consider the following code segment:
if ( Y<0 )
{X = −X; Y = − Y;}
Z = 0;
while (Y>0)
{Z = Z + X; Y = Y −1;}
Assume that X, Y and Z are inter variables, and that X and Y have be initialized. Which of the following best describes what this code segment does?
Show answerHide answer
Correct answer: (A) Sets Z to be the product X* Y
Explanation
If Y is negative, both signs are flipped. The loop then adds X to Z Y times, so Z = X × Y.