<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 6 Conditional Processing, Part 2 - Switch Statements and Logical Operators_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 6: Conditional Processing, Part 2
Switch Statements and Logical Operators
 More of this Feature
• Conditional Processing, Part 2, Page 2
 Related Resources
• Programming Tip: Assignment vs Equality in If Statements
• Use of Braces in C/C++
• All C++ Tutorials
 

by John Kopp

Welcome to About.com's C++ tutorial. This second lesson on conditional processing introduces both the switch statement and logical operators. The switch statement is a construct that is used to replace deeply nested or chained if/else statements. Nested if/else statements arise when there are multiple alternative threads of execution based on some condition. Here's an example. Suppose that an ice cream store has asked us to write a program that will automate the taking of orders. We will need to present a menu and then based on the customer's choice take an appropriate action.

#include <iostream>
using namespace std;

int main()
{
    int choice;

    cout << "What flavor ice cream do want?" << endl;
    cout << "Enter 1 for chocolate" << endl;
    cout << "Enter 2 for vanilla" << endl;
    cout << "Enter 3 for strawberry" << endl;
    cout << "Enter 4 for green tea flavor, yuck" << endl;
    cout << "Enter you choice: ";

    cin >> choice;

   if (choice == 1) {
        cout << "Chocolate, good choice" << endl;
    }
    else if (choice == 2) {
        cout << "Vanillarific" << endl;
    }
    else if (choice == 3) {
        cout << "Berry Good" << endl;
    }
    else if (choice == 4) {
        cout << "Big Mistake" << endl;
    }
    else {
       cout << "We don't have any" << endl;
        cout << "Make another selection" << endl;
    }

    return 0;
}

This program will work fine, but the if/else block is cumbersome. It would be easy, particularly if there were more choices and maybe sub choices involving more if/else's to end up with program that doesn't perform the actions intended. Here's the same program with a switch.

#include <iostream>
using namespace std;

int main()
{
    int choice;

    cout << "What flavor ice cream do want?" << endl;
    cout << "Enter 1 for chocolate" << endl;
    cout << "Enter 2 for vanilla" << endl;
    cout << "Enter 3 for strawberry" << endl;
    cout << "Enter 4 for green tea flavor, yuck" << endl;
    cout << "Enter you choice: ";

    cin >> choice;

    switch (choice) {
    case 1:
        cout << "Chocolate, good choice" << endl;
        break;
    case 2:
        cout << "Vanillarific" << endl;
        break;
    case 3:
        cout << "Berry Good" << endl;
        break;
    case 4:
        cout << "Big Mistake" << endl;
        break;
    default:
        cout << "We don't have any" << endl;
        cout << "Make another selection" << endl;
    }

    return 0;
}

Next page > Page 2 of Tutorial > Page 1, 2, 3




Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.