C++ Control Flow Programs

03/04/2024 0 By indiafreenotes

Control flow in C++ encompasses the mechanisms that enable the execution of code blocks conditionally or repeatedly through various constructs like if, else, switch, for, while, and do-while. These constructs are fundamental to programming, allowing developers to write flexible and dynamic programs that can make decisions, execute code multiple times, and handle different scenarios based on user input or computational results. Understanding and effectively utilizing control flow is essential for solving problems that require conditional logic and repetitive tasks.

if and else Statements

The if statement allows you to execute a block of code only if a specified condition is true. The else clause can be added to execute a code block when the condition is false.

Example: Determine if a Number is Positive or Negative

#include <iostream>

using namespace std;

int main() {

    int number;

    cout << “Enter a number: “;

    cin >> number;

    if (number > 0) {

        cout << “The number is positive.” << endl;

    } else if (number < 0) {

        cout << “The number is negative.” << endl;

    } else {

        cout << “The number is zero.” << endl;

    }

    return 0;

}

This program uses if, else if, and else to categorize a number as positive, negative, or zero based on user input.

switch Statement

switch statement allows you to execute one code block among many alternatives based on the value of a variable or expression. It’s particularly useful when you have multiple conditions to check against the same expression.

Example: Simple Calculator

#include <iostream>

using namespace std;

int main() {

    char operation;

    double num1, num2;

    cout << “Enter an operator (+, -, *, /): “;

    cin >> operation;

    cout << “Enter two numbers: “;

    cin >> num1 >> num2;

    switch (operation) {

        case ‘+’:

            cout << “Result: ” << (num1 + num2);

            break;

        case ‘-‘:

            cout << “Result: ” << (num1 – num2);

            break;

        case ‘*’:

            cout << “Result: ” << (num1 * num2);

            break;

        case ‘/’:

            if(num2 != 0.0)

                cout << “Result: ” << (num1 / num2);

            else

                cout << “Cannot divide by zero!”;

            break;

        default:

            cout << “Invalid operator!”;

    }

    return 0;

}

This program demonstrates a simple calculator using switch, handling basic arithmetic operations.

Loops

Loops are used to execute a block of code repeatedly. C++ provides several loop constructs: for, while, and do-while.

for Loop Example: Print Numbers from 1 to 10

#include <iostream>

using namespace std;

int main() {

    for (int i = 1; i <= 10; i++) {

        cout << i << ” “;

    }

    return 0;

}

This program uses a for loop to print numbers from 1 to 10, demonstrating how to execute a code block a specific number of times.

while Loop Example: Guess the Number Game

#include <iostream>

using namespace std;

int main() {

    int secretNumber = 7, guess;

    cout << “Guess the secret number (1-10): “;

    cin >> guess;

    while (guess != secretNumber) {

        cout << “Incorrect. Try again: “;

        cin >> guess;

    }

    cout << “Congratulations! You guessed the right number.”;

    return 0;

}

This example uses a while loop to repeatedly prompt the user to guess a secret number until the correct number is guessed.

do-while Loop Example: Menu-driven Program

#include <iostream>

using namespace std;

int main() {

    int choice;

    do {

        cout << “\nMenu:\n”;

        cout << “1. Print Hello\n”;

        cout << “2. Print World\n”;

        cout << “3. Exit\n”;

        cout << “Enter your choice: “;

        cin >> choice;

        switch (choice) {

            case 1:

                cout << “Hello”;

                break;

            case 2:

                cout << “World”;

                break;

            case 3:

                cout << “Exiting…”;

                break;

            default:

                cout << “Invalid choice. Please try again.”;

        }

    } while (choice != 3);

    return 0;

}

This do-while loop example implements a menu-driven program that executes at least once and repeats until the user decides to exit.