C++ Control Flow Statements

Introduction

Control flow statements in C++ are used to control the flow of execution of the program based on certain conditions. These statements help you make decisions, repeat tasks, and execute code conditionally. Understanding control flow statements is essential for writing logical and efficient programs.

Types of Control Flow Statements

  1. Conditional Statements: Used to perform different actions based on different conditions.

    • if
    • if-else
    • if-else-if ladder
    • switch
  2. Looping Statements: Used to repeat a block of code multiple times.

    • for
    • while
    • do-while
  3. Jump Statements: Used to alter the flow of control unconditionally.

    • break
    • continue
    • goto

Conditional Statements

if Statement

The if statement executes a block of code if a specified condition is true.

Example

#include <iostream>
using namespace std;

int main() {
    int number = 10;

    if (number > 5) {
        cout << "The number is greater than 5." << endl;
    }

    return 0;
}

Output

The number is greater than 5.

if-else Statement

The if-else statement executes one block of code if a condition is true and another block if the condition is false.

Example

#include <iostream>
using namespace std;

int main() {
    int number = 3;

    if (number > 5) {
        cout << "The number is greater than 5." << endl;
    } else {
        cout << "The number is not greater than 5." << endl;
    }

    return 0;
}

Output

The number is not greater than 5.

if-else-if Ladder

The if-else-if ladder allows you to check multiple conditions sequentially. Once a condition is true, the corresponding block of code is executed, and the rest are skipped.

Example

#include <iostream>
using namespace std;

int main() {
    int number = 8;

    if (number < 5) {
        cout << "The number is less than 5." << endl;
    } else if (number == 5) {
        cout << "The number is equal to 5." << endl;
    } else {
        cout << "The number is greater than 5." << endl;
    }

    return 0;
}

Output

The number is greater than 5.

switch Statement

The switch statement allows you to select one of many code blocks to be executed based on the value of an expression.

Example

#include <iostream>
using namespace std;

int main() {
    int day = 3;

    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;
        default:
            cout << "Weekend" << endl;
    }

    return 0;
}

Output

Wednesday

Looping Statements

for Loop

The for loop repeats a block of code a specified number of times.

Example

#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 5; i++) {
        cout << "i = " << i << endl;
    }

    return 0;
}

Output

i = 0
i = 1
i = 2
i = 3
i = 4

while Loop

The while loop repeats a block of code as long as a specified condition is true.

Example

#include <iostream>
using namespace std;

int main() {
    int i = 0;

    while (i < 5) {
        cout << "i = " << i << endl;
        i++;
    }

    return 0;
}

Output

i = 0
i = 1
i = 2
i = 3
i = 4

do-while Loop

The do-while loop is similar to the while loop, but it checks the condition after executing the block of code, ensuring that the code block is executed at least once.

Example

#include <iostream>
using namespace std;

int main() {
    int i = 0;

    do {
        cout << "i = " << i << endl;
        i++;
    } while (i < 5);

    return 0;
}

Output

i = 0
i = 1
i = 2
i = 3
i = 4

Jump Statements

break Statement

The break statement terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.

Example

#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 5; i++) {
        if (i == 3) {
            break;
        }
        cout << "i = " << i << endl;
    }

    return 0;
}

Output

i = 0
i = 1
i = 2

continue Statement

The continue statement skips the current iteration of the loop and continues with the next iteration.

Example

#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 5; i++) {
        if (i == 3) {
            continue;
        }
        cout << "i = " << i << endl;
    }

    return 0;
}

Output

i = 0
i = 1
i = 2
i = 4

goto Statement

The goto statement transfers control to the labeled statement.

Example

#include <iostream>
using namespace std;

int main() {
    int i = 0;

    start:
    cout << "i = " << i << endl;
    i++;
    if (i < 5) {
        goto start;
    }

    return 0;
}

Output

i = 0
i = 1
i = 2
i = 3
i = 4

Conclusion

Control flow statements are fundamental for writing logical and efficient programs. This chapter covered conditional statements (if, if-else, if-else-if ladder, and switch), looping statements (for, while, and do-while), and jump statements (break, continue, and goto) with simple examples and their outputs. Understanding these statements will help you control the flow of your program based on various conditions and perform repetitive tasks efficiently. In the next chapter, we will delve deeper into functions in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top