result ← 1
IF(score1 > 500)
{
result ← result + 1
IF(score2 > 500)
{
result ← result + 1
}
ELSE
{
result ← result + 2
}
}
ELSE
{
result ← result + 5
IF(score2 > 500)
{
result ← result + 1
}
ELSE
{
result ← result - 1
}
}
 
 

 If the value of score1 is 505 and the value of score2 is 250, what will be the value of result after the code segment is executed?

 

If the value of score1 is 350 and the value of score2 is 200, what will be the value of result after the code segment is executed?

Respuesta :

Answer:

The value of result is 4

Explanation:

Given

[tex]score1 = 505[/tex]

[tex]score2 = 250[/tex]

Required

The value of result

On line 1 of the program, result is initialized to 1;

i.e.

[tex]result = 1[/tex]

Line 2 is a conditional statement that checks if score1 is greater than 500.

In this case, the result of the condition is true because 505 > 500 and as such

The corresponding else statement is discarded and the following will be executed

if(score1 > 500){ ->This is true

       result = result + 1; -> result  is increased by 1 to 2

       if(score2 > 500){ -> This is false because score2 is 250 and 250 < 500

           result = result + 1; -> This won't be executed because the condition is false

       }

       else{ -> This will be executed since its corresponding if condition is false, the else statement will be true

           result = result + 2; -> result is incremented by 2 to give 4

       }

   }

Hence, the value of result is 4