Ask Question
29 April, 07:23

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

+5
Answers (1)
  1. 29 April, 08:59
    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?
Get 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 & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers