The 'switch' statement in C++

Published on: August 24, 2010

Today's Beginner C++ Tutorial teaches you a little about the switch statement. It works as an alternative to the "if" statement and it is much cleaner and much more efficient. The tutorial explains just a little bit about how the switch statement works and how it can help you clean up your programs if you have multiple options.

I hope you've learned a little more about the switch statement from the tutorial as well as from my other tutorials that I've made over the past few months. I hope you enjoy the video and if you have any requests feel free to let me know.

Remember to checkout the Codesnip below to review the C++ code yourself! Feel free to copy the code, but I ask that you please provide credit if you leave the code unchanged when you use it.

Codesnip:

//Programmer: Nazmus
//Program:  Switch Statement
//Website:  EasyProgramming.net

#include <iostream>
using namespace std;

void main()
{
 char age;
 cout << "Pleaes enter letter associated with your age: \n"
  "(A) 17 years and below \n"
  "(B) 18 years to 64 years \n"
  "(C) 65 years and above \n" << endl;
 cin >> age;

 cout << "The ticket will cost you: ";
 switch (age){
  case 'a' :
  case 'A' : cout << "$10";
   break;
  case 'b':
  case 'B' : cout << "$20";
   break;
  case 'c':
  case 'C' : cout << "$15";
   break;
  default : cout << "Invalid entry";
   break;
 }
 cout << endl;

 system("pause");
}



Comments: