Control Structures

Conditional Statements

Conditional statements allow your program to make decisions based on different conditions.

if Statement

Basic if Statement

#include <iostream>
using namespace std;

int main() {
    int age = 18;
    
    if (age >= 18) {
        cout << "You are an adult." << endl;
    }
    
    if (age < 18) {
        cout << "You are a minor." << endl;
    }
    
    return 0;
}

if-else Statement

if-else Example

#include <iostream>
using namespace std;

int main() {
    int score = 85;
    
    if (score >= 90) {
        cout << "Grade: A" << endl;
    } else if (score >= 80) {
        cout << "Grade: B" << endl;
    } else if (score >= 70) {
        cout << "Grade: C" << endl;
    } else if (score >= 60) {
        cout << "Grade: D" << endl;
    } else {
        cout << "Grade: F" << endl;
    }
    
    return 0;
}

Switch Statement

The switch statement is useful when you need to compare a variable against many values.

Switch Statement Example

#include <iostream>
using namespace std;

int main() {
    char grade = 'B';
    
    switch (grade) {
        case 'A':
            cout << "Excellent!" << endl;
            break;
        case 'B':
            cout << "Good job!" << endl;
            break;
        case 'C':
            cout << "Well done!" << endl;
            break;
        case 'D':
            cout << "You passed!" << endl;
            break;
        case 'F':
            cout << "Better try again!" << endl;
            break;
        default:
            cout << "Invalid grade!" << endl;
            break;
    }
    
    return 0;
}

Important Notes:

  • Always use break; to prevent fall-through behavior
  • The default case is optional but recommended
  • Switch works with integers, characters, and enums

Loops

Loops allow you to repeat code multiple times efficiently.

for Loop

for Loop Example

#include <iostream>
using namespace std;

int main() {
    // Basic for loop
    cout << "Counting from 1 to 5:" << endl;
    for (int i = 1; i <= 5; i++) {
        cout << i << " ";
    }
    cout << endl;
    
    // Loop with step of 2
    cout << "Even numbers from 2 to 10:" << endl;
    for (int i = 2; i <= 10; i += 2) {
        cout << i << " ";
    }
    cout << endl;
    
    // Reverse loop
    cout << "Countdown from 5 to 1:" << endl;
    for (int i = 5; i >= 1; i--) {
        cout << i << " ";
    }
    cout << endl;
    
    return 0;
}

while Loop

while Loop Example

#include <iostream>
using namespace std;

int main() {
    int number = 1;
    
    cout << "Numbers from 1 to 5 using while:" << endl;
    while (number <= 5) {
        cout << number << " ";
        number++;  // Important: increment to avoid infinite loop
    }
    cout << endl;
    
    // User input validation
    int userInput;
    cout << "Enter a number between 1 and 10: ";
    cin >> userInput;
    
    while (userInput < 1 || userInput > 10) {
        cout << "Invalid input! Please enter a number between 1 and 10: ";
        cin >> userInput;
    }
    
    cout << "Thank you! You entered: " << userInput << endl;
    
    return 0;
}

do-while Loop

do-while Loop Example

#include <iostream>
using namespace std;

int main() {
    int choice;
    
    do {
        cout << "\n=== Menu ===" << endl;
        cout << "1. Say Hello" << endl;
        cout << "2. Say Goodbye" << endl;
        cout << "3. Exit" << endl;
        cout << "Enter your choice: ";
        cin >> choice;
        
        switch (choice) {
            case 1:
                cout << "Hello there!" << endl;
                break;
            case 2:
                cout << "Goodbye!" << endl;
                break;
            case 3:
                cout << "Exiting..." << endl;
                break;
            default:
                cout << "Invalid choice!" << endl;
                break;
        }
    } while (choice != 3);
    
    return 0;
}

Loop Control Statements

These statements allow you to control the flow of loops.

break and continue

#include <iostream>
using namespace std;

int main() {
    cout << "Using break - stop at 7:" << endl;
    for (int i = 1; i <= 10; i++) {
        if (i == 7) {
            break;  // Exit the loop when i equals 7
        }
        cout << i << " ";
    }
    cout << endl;
    
    cout << "Using continue - skip even numbers:" << endl;
    for (int i = 1; i <= 10; i++) {
        if (i % 2 == 0) {
            continue;  // Skip the rest of this iteration
        }
        cout << i << " ";
    }
    cout << endl;
    
    // Nested loops with labeled break (using goto - not recommended)
    cout << "Finding first pair that sums to 10:" << endl;
    bool found = false;
    for (int i = 1; i <= 5 && !found; i++) {
        for (int j = 1; j <= 5; j++) {
            if (i + j == 10) {
                cout << "Found: " << i << " + " << j << " = 10" << endl;
                found = true;
                break;  // Break inner loop
            }
        }
    }
    
    return 0;
}

Nested Control Structures

You can nest loops and conditional statements inside each other.

Multiplication Table

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    cout << "Multiplication Table (1-5):" << endl;
    cout << "   ";
    
    // Print header
    for (int i = 1; i <= 5; i++) {
        cout << setw(4) << i;
    }
    cout << endl;
    
    // Print table
    for (int i = 1; i <= 5; i++) {
        cout << setw(2) << i << " ";
        for (int j = 1; j <= 5; j++) {
            cout << setw(4) << (i * j);
        }
        cout << endl;
    }
    
    return 0;
}

Number Pattern

#include <iostream>
using namespace std;

int main() {
    int rows = 5;
    
    cout << "Number Triangle:" << endl;
    for (int i = 1; i <= rows; i++) {
        // Print spaces for alignment
        for (int space = 1; space <= rows - i; space++) {
            cout << " ";
        }
        
        // Print numbers
        for (int j = 1; j <= i; j++) {
            cout << j << " ";
        }
        cout << endl;
    }
    
    return 0;
}

Best Practices

Use Meaningful Conditions

Write clear, readable conditions that express intent

Avoid Deep Nesting

Use early returns or break statements to reduce nesting levels

Initialize Loop Variables

Always initialize loop counters to prevent undefined behavior

Use const When Possible

Make loop variables const if they shouldn't change