CPP 06 - Code Execution Control with Switch - Learn Cisco

Post Top Ad

CPP 06 - Code Execution Control with Switch



The 'case' statement is another way of running certain piece of code depending on user's choice. It is a nice way of creating quick menu. 

Let's write a program which converts the temperatures. Our formulas will be as follows :

Celsius-to-Fahrenheit
F = C * 9/5 + 32

Fahrenheit-to-Celsius
C = (F - 32) * 5/9

Type this code into your text editor, compile and run it.

Source Code - 6.1

/* Program Converting Temperatures 
version: 1.0
name: ex006.cpp
compilation: g++ -o ex006 ex006.cpp
author: Jarek Rek
*/

#include <iostream>

using namespace std;

int main () {

// Variables
float temperature;
float result;
int choice;

/* Here the Code Begins */

// Banner
cout << endl << endl;
cout << "      *******************************************\n";
cout << "             Program Converting Temperatures         \n";
cout << "      *******************************************\n";
cout << endl;

// User's interface
     cout << "\tYour options:\n";
cout << "\t---------------\n";
cout << "\t[1]\tConvert Celsius to Fahrenheit\n";
cout << "\t[2]\tConvert Fahrenheit to Celsius\n";
cout << "\nPlease choose the option [1]|[2]: ";

// Accept user's option
cin >> choice;

// Run code according to user's choice
switch (choice)
{
case 1: 
cout << "\nProvide number of degrees in Celsius: ";
cin >> temperature;
result = temperature * 9/5 + 32;
cout << temperature << " degrees of Celsius is "
<< result << " degrees of Fahrenheit.\n";
break;
case 2:
cout << "\nProvide number of degrees in Fahrenheit: ";
cin >> temperature;
result = (temperature - 32) * 5/9;
cout << temperature << " degrees of Fahrenheit is "
<< result << " degrees of Celsius.\n";
break;
default:
cout << "\nYou have provided wrong option.\n";
cout << "Program terminates.\n\n";
break;
}
return 0;
}

You can download this source code here but I insist you type it in.

Explanation
The main protagonists in this short story of mine are 'switch' and 'break' statements. 
The syntax is as follows:

switch (variable_number_or_character)
{
   case number_or_character:
      code_to_be_executed;
      break;

   case another_number_or_character;
      code_to_be_executed;
      break;


   default:
      code_to_be_executed_if_previous_options_did_not_work;
      break;
}

The 'break' statement will leave the block of code (jumps out of the braces '{}') upon executing the code for a lesson given case (user's choice).

If the user chooses something other than option '1', or option '2', the code under 'default' will be executed. Test it yourself.


You could also use 'continue' key word, but that I will explain next time. You may have noticed I added some comments in the code.

If you run the program you should have similar results:

Pic. 6.1 - Program Execution Option 1.


Pic. 6.1 - Program Execution Option 2.

Homework
1. Add comments to each of the cases used by the 'switch' statement.

2. Write a code that presents the banner Iike in my example), that converts currency:
a). Dollar to Euro
b). Euro to Dollar

Post Top Ad