Topic 6: Selection Structures

Selection structures allow you to execute different code based on conditions. The primary selection structures in C++ are:

  • if statement
  • if-else statement
  • else-if Chain
  • switch statement

if Statement

The if statement allows you to execute a block of code only if a specified condition is true.

Syntax:
if (condition) {
    // code to be executed if condition is true
}
                    
Example: if Statement
  • #include <iostream>
    using namespace std;
    
    int main() 
    {
        int number = 10;
    
        if (number > 5) 
        {
            cout << "Number is greater than 5" << endl;
        }
    
        return 0;
    }
                                        

    Output:

    Number is greater than 5
                                        

if-else Statement

The if-else statement allows you to execute one block of code if a condition is true and another block of code if the condition is false.

Syntax:
if (condition) {
    // code to be executed if condition is true
} else {
    // code to be executed if condition is false
}
                    
Example: if-else Statement
  • #include <iostream>
    using namespace std;
    
    int main() 
    {
        int number = 3;
    
        if (number > 5) 
        {
            cout << "Number is greater than 5" << endl;
        } else 
        {
            cout << "Number is not greater than 5" << endl;
        }
    
        return 0;
    }
                                        

    Output:

    Number is not greater than 5
                                        

else-if Chain

The else-if chain is used to test multiple conditions. If one of the conditions is true, the corresponding block of code is executed, and the rest of the Chain is skipped.

Syntax:
if (condition1) 
{
    // code to be executed if condition1 is true
} else if (condition2) 
{
    // code to be executed if condition2 is true
} else {
    // code to be executed if none of the conditions are true
}
                    
Example: else-if chain
  • #include <iostream>
    using namespace std;
    
    int main() 
    {
        int number = 7;
    
        if (number > 10) 
        {
            cout << "Number is greater than 10" << endl;
        } else if (number > 5) 
        {
            cout << "Number is greater than 5" << endl;
        } else 
        {
            cout << "Number is 5 or less" << endl;
        }
    
        return 0;
    }
                                        

    Output:

    Number is greater than 5
                                        

switch Statement

The switch statement allows you to select one of many code blocks to be executed.

Syntax:
switch (expression) 
{
    case constant1:
        // code to be executed if expression equals constant1
        break;
    case constant2:
        // code to be executed if expression equals constant2
        break;
    // you can have any number of case statements
    default:
        // code to be executed if expression doesn't match any case
}
                    
Example: switch Statement
  • #include <iostream>
    using namespace std;
    
    int main() 
    {
        int day = 4;
    
        switch (day) 
        {
            case 1:
                cout << "Monday" << endl;
                break;
            case 2:
                cout << "Tuesday" << endl;
                break;
            case 3:
                cout << "Wednesday" << endl;
                break;
            case 4:
                cout << "Thursday" << endl;
                break;
            case 5:
                cout << "Friday" << endl;
                break;
            case 6:
                cout << "Saturday" << endl;
                break;
            case 7:
                cout << "Sunday" << endl;
                break;
            default:
                cout << "Invalid day" << endl;
        }
    
        return 0;
    }
                                        

    Output:

    Thursday 


  1. What is C++ and who created it?
  2. Write a simple C++ program that prints "Welcome to C++ Programming!" on the screen.
  3. Explain the use of the #include <iostream> directive in a C++ program.
  4. What is the purpose of the main() function in a C++ program?
  5. Describe the purpose of the return 0; statement in the main() function.
  1. Write a C++ program to display the message "PROCEED WITH TAKEOFF" or "ABORT TAKEOFF" depending on the input. If the character g is entered in the variable code, the first message should be displayed, otherwise, the second message should be displayed.
  2. Create a program that asks the user to guess a pre-set number. If the number has been guessed correctly then display "Congratulations", otherwise display "incorrect".
  3. Write a program that reads an integer and determines and prints whether it is odd or even. [Hint: use the modulus operator. an even number is a multiple of two. Any multiple of two leaves a reminder of zero when divided by 2]
  4. Create a program that checks if a given year is a leap year. A year is a leap year if it is divisible by 4 but not divisible by 100, or it is divisible by 400.
  5. Write a program that accepts three integers from the user and determines which one is the largest.
  6. Write a program to calculate the discount based on the total shopping amount:
    • If the total amount is greater than or equal to $1000, apply a 10% discount.
    • Otherwise, apply a 5% discount.
  7. Write a program that checks the type of triangle based on the lengths of its three sides:
    • If all sides are equal, it's an equilateral triangle.
    • If two sides are equal, it's an isosceles triangle.
    • If no sides are equal, it's a scalene triangle.
  8. Create a program that takes the marks of a student as input and displays their grade based on the following criteria:
    • 90 and above: Grade A
    • 80 to 89: Grade B
    • 70 to 79: Grade C
    • 60 to 69: Grade D
    • Below 60: Grade F
  9. Write a program that acts as a simple calculator. The user will input two numbers and an operator (+, -, *, /), and the program will perform the appropriate operation.
    • Use a switch statement to check the operator entered by the user.
    • Perform the arithmetic operation based on the operator.
    • Handle cases for addition, subtraction, multiplication, and division.
Coming Soon...