ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP1112Conditional Statements…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP1112

    Programming Fundamentals

    Progress0 / 19 done
    Topics19
    1Introduction to Problem Solving2Von-Neumann Architecture3Introduction to Programming4Role of Compiler and Linker5Introduction to Algorithms6Basic Data Types and Variables7Input/Output Constructs8Arithmetic, Comparison and Logical Operators9Conditional Statements and Execution Flow10Repetitive Statements and Execution Flow11Lists and Memory Organization12Multi-dimensional Lists13Introduction to Modular Programming14Function Definition and Calling15Stack Rolling and Unrolling16Strings and String Operations17Pointers/References18Static and Dynamic Memory Allocation19File I/O Operations
    4 min read
    Topic 9 of 19
    Topic Study Note Illustration
    09

    Conditional Statements and Execution Flow

    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...

    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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    689words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals