Ask Question
15 September, 02:26

Rewrite the following nested if-else statement as a switch statement that accomplishes exactly the same thing. Assume that num is an integer variable that has been initialized, and that there are functions f1, f2, f3, and f4. Do not use any if or if-else statements in the actions in the switch statement, only calls to the four functions.

+2
Answers (1)
  1. 15 September, 02:36
    0
    Answer explained below

    Explanation:

    The following is the nested if-else statement:

    % if-based statement

    if num 4

    f1 (num)

    else

    if num <=2

    if num > = 0

    f2 (num)

    else

    f3 (num)

    end

    else

    f4 (num)

    end

    end

    NOTE: the num is an integer variable that has been initialized and that there are functions f1, f2, f3 and f4.

    The nested if-else statement can be replaced by switch statement as shown below:

    switch num

    case (0, 1, 2)

    f2 (num)

    case (-2, - 1)

    f3 (num)

    case (3, 4)

    f4 (num)

    otherwise

    f1 (num)

    In this case, the switch based code is easier to write and understand because the cases are single valued or a few discrete values (not ranges of values)
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Rewrite the following nested if-else statement as a switch statement that accomplishes exactly the same thing. Assume that num is an ...” in 📗 Engineering if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers