Using a conditional expression, write a statement that increments num_users if update_direction is 3, otherwise decrements num_users. Sample output with inputs: 8 3 New value is: 9

Respuesta :

Answer:

num_users = (update_direction == 3) ? (++num_users) : (--num_users);

Explanation:

A. Using the regular if..else statement, the response to the question would be like so;

if (update_direction == 3) {

  ++num_users;  // this will increment num_users by 1

}

else {

 -- num_users;    //this will decrement num_users by 1

}

Note: A conditional expression has the following format;

testcondition ? true : false;

where

a. testcondition is the condition to be tested for. In this case, if update_direction is 3 (update_direction == 3).

b. true is the statement to be executed if the testcondition is true.

c. false is the statement to be executed it the testcondition is false.

B. Converting the above code snippet (the if..else statement in A above) into a conditional expression statement we have;

num_users = (update_direction == 3) ? (++num_users) : (--num_users);

Hope this helps!

Answer:

num_users = num_users + 1 if update_direction == 3 else num_users - 1

Explanation: