Ask Question
23 February, 17:23

Give an example of an if/else statment and a case statment that are equivalent. Your example should contain at least three choices.

+2
Answers (1)
  1. 23 February, 18:13
    0
    The if - else and equivalent switch case statements in c+ + language are given below.

    if (ch = = 1)

    cout << " sum is " << x + y << endl;

    else if (ch = = 2)

    cout << " difference is " << x - y << endl;

    else if (ch = = 3)

    {

    cout << " Quitting " << endl;

    exit;

    }

    switch (ch)

    {

    case 1 : cout << " Sum is " << x + y << endl;

    break;

    case 2 : cout << " Difference is " << x - y << endl;

    break;

    case 3 : cout << " Quitting " << endl;

    break;

    }

    Explanation:

    The above statements use three variables which are declared and initialized as shown.

    int x = 10;

    int y = 25;

    int ch;

    The above statements execute for of addition, subtraction or to quit the program.

    cout << " 1. Addition " << endl;

    cout << " 2. Subtraction. " << endl;

    cout << " 3. Quit " << endl;

    cout << " Enter your choice " << endl;

    The variable ch holds the numerical option entered by the user.

    cin >> ch;

    For if - else statements, the if statement is executed based on the value of variable ch entered by the user. After execution, the compiler goes out of the statements automatically.

    Following the if - else, if any statement is present, it will be executed.

    For switch case, every case ends with break keyword. After the required case is executed based on the value of variable ch entered by the user, the break statement is executed which causes the switch case to terminate the control goes out of the statements.

    Following the switch case, if any statement is present, it will be executed.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Give an example of an if/else statment and a case statment that are equivalent. Your example should contain at least three choices. ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers