Table of Contents

Control Structures: Selection examples

if

if_example.cpp
/* Using if statements */
#include <iostream>
using namespace std;
 
int main()
{
    int grade;
 
    cout << "Enter a grade and I will tell you if you passed: ";
    cin >> grade;
 
    if (grade > 60)
        cout << "Passed" << endl;
 
    return 0;
}

if/else

if_else_example.cpp
/* Using if/else statements */
#include <iostream>
using namespace std;
 
int main()
{
    int num1;
    cout << "Enter an integer, and I will tell you ";
    cout << "if the number is positive: ";
    cin >> num1;  // read an integer
 
    if (num1 > 0)
        cout << num1 << " is positive. "<< endl;
    else
        cout << num1 << " is not positive. "<< endl;
 
    return 0;
}

nested if/else

nested_if_else_example.cpp
/* Using nested if/else statements */
#include <iostream>
using namespace std;
 
int main()
{
    int grade;
    cout << "Enter a numerical grade, and I will tell you" << endl;
    cout << "how well the student performed: ";
    cin >> grade;    // read an integer
 
    cout << "The student received a letter grade of ";
    if (grade >= 90 )
        cout << "A";
    else if (grade >= 80)
        cout << "B";
    else if (grade >= 70)
        cout << "C";
    else if (grade >= 60)
        cout << "D";
    else
        cout << "F";
    cout << "." << endl;
 
    return 0;
}

validating input

validating_input_example.cpp
/* Using logical operators to validate input */
#include <iostream>
using namespace std;
 
int main()
{
    int grade;
    cout << "Enter a numerical grade, and I will tell you" << endl;
    cout << "how well the student performed: ";
    cin >> grade;    // read an integer
 
    if (grade >= 0 && grade <= 100)
    {
        cout << "The student received a letter grade of ";
        if (grade >= 90 )
            cout << "A";
        else if (grade >= 80)
            cout << "B";
        else if (grade >= 70)
            cout << "C";
        else if (grade >= 60)
            cout << "D";
        else
            cout << "F";
        cout << "." << endl;
    }
    else
        cout << "The grade must be between 0 and 100." << endl;
 
    return 0;
}

comparing characters

comparing_characters.cpp
/* Comparing characters */
#include <iostream>
using namespace std;
 
int main()
{
   char ch;
 
   // Get a character from the user.
   cout << "Enter a letter: ";
   ch = cin.get();
 
   // Did user enter a letter?
   if (ch >= 'a' && ch <= 'z')
      cout << "You entered a character between 'a' and 'z'." << endl;
   else
      cout << "You entered something else." << endl;
 
   return 0;
}

switch

simple_switch_example.cpp
/* A simple switch statement example */
#include <iostream>
using namespace std;
 
int main()
{
    int selection;
 
    // Prompt the user for a selection:
    cout << "Enter:" << endl
         << "1 for a foo" << endl
         << "2 for a bar" << endl
         << "3 for a baz" << endl
         << "> ";
    cin >> selection;
 
    // Give the user what they asked for:
    switch (selection)
    {
    case 1:
        cout << "You entered 1." << endl;
        cout << "A foo on all your houses!" << endl;
        break;
    case 2:
        cout << "You entered 2." << endl;
        cout << "A bar on all your houses!" << endl;
        break;
    case 3:
        cout << "You entered 3." << endl;
        cout << "A baz on all your houses!" << endl;
        break;
    default:
        cout << "You did not enter 1, 2, or 3." << endl;
    }
 
    return 0;
}