ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Programming Fundamentals
    COMP1112
    Progress0 / 19 topics
    Topics
    1. Introduction to Problem Solving2. Von-Neumann Architecture3. Introduction to Programming4. Role of Compiler and Linker5. Introduction to Algorithms6. Basic Data Types and Variables7. Input/Output Constructs8. Arithmetic, Comparison and Logical Operators9. Conditional Statements and Execution Flow10. Repetitive Statements and Execution Flow11. Lists and Memory Organization12. Multi-dimensional Lists13. Introduction to Modular Programming14. Function Definition and Calling15. Stack Rolling and Unrolling16. Strings and String Operations17. Pointers/References18. Static and Dynamic Memory Allocation19. File I/O Operations
    COMP1112›Conditional Statements and Execution Flow
    Programming FundamentalsTopic 9 of 19

    Conditional Statements and Execution Flow

    4 minread
    689words
    Beginnerlevel

    Conditional Statements and Execution Flow in C++

    Conditional statements are fundamental constructs in programming that allow you to execute certain blocks of code based on specific conditions. They are essential for controlling the flow of execution in a program. Here’s a detailed overview of conditional statements in C++.

    1. The if Statement

    The if statement evaluates a condition and executes a block of code if the condition is true.

    Syntax:

    if (condition) {
        // Code to execute if condition is true
    }
    

    Example:

    #include <iostream>
    using namespace std;
    
    int main() {
        int age;
        cout << "Enter your age: ";
        cin >> age;
    
        if (age >= 18) {
            cout << "You are an adult." << endl;
        }
        return 0;
    }
    

    2. The if-else Statement

    The if-else statement allows for an alternative block of code to be executed if the condition is false.

    Syntax:

    if (condition) {
        // Code to execute if condition is true
    } else {
        // Code to execute if condition is false
    }
    

    Example:

    int main() {
        int age;
        cout << "Enter your age: ";
        cin >> age;
    
        if (age >= 18) {
            cout << "You are an adult." << endl;
        } else {
            cout << "You are not an adult." << endl;
        }
        return 0;
    }
    

    3. The else if Statement

    The else if statement allows for multiple conditions to be checked in sequence.

    Syntax:

    if (condition1) {
        // Code for condition1
    } else if (condition2) {
        // Code for condition2
    } else {
        // Code if neither condition is true
    }
    

    Example:

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

    4. The switch Statement

    The switch statement provides a way to execute one block of code from multiple options based on the value of a variable.

    Syntax:

    switch (expression) {
        case value1:
            // Code to execute if expression == value1
            break;
        case value2:
            // Code to execute if expression == value2
            break;
        default:
            // Code to execute if no case matches
    }
    

    Example:

    int main() {
        int day;
        cout << "Enter day number (1-7): ";
        cin >> day;
    
        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;
    }
    

    5. Execution Flow

    The execution flow of a program is determined by the sequence of statements executed. Conditional statements alter this flow by allowing the program to choose different paths based on conditions.

    Flow Control:

    • If a condition in an if statement evaluates to true, the corresponding block of code is executed, and control moves to the next statement after the block.
    • If the condition is false, control jumps to the else block if it exists, or to the next else if statement, if applicable.
    • In a switch statement, control jumps to the case that matches the value of the expression, executing all code in that case until a break statement is encountered or the switch ends.

    Summary

    Conditional statements are vital for controlling the execution flow of a program. By using if, else if, else, and switch statements, you can make decisions in your code and execute different actions based on varying conditions. This allows for more complex and dynamic programming, enabling programs to respond to user input and other conditions effectively.

    Previous topic 8
    Arithmetic, Comparison and Logical Operators
    Next topic 10
    Repetitive Statements and Execution Flow

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time4 min
      Word count689
      Code examples0
      DifficultyBeginner