Instagram
youtube
Facebook
Twitter

Control Structure

Control Structures:

C++ provides control structures to control the flow of execution in a program. Common control structures include tentative statements (if, else, if, else).

  • if-else statements: In C++, if -else statements are used for conditional branching, allowing you to execute different blocks of code based on a specified condition. Then there is the basic syntax of an if-else statement in C++:

The ‘if’ keyword is followed by a set of parentheses ‘()’ containing the condition you want to check.

Still, the code inside the corresponding block following the ‘if’ statement is executed, if the condition inside the parentheses is ‘true’.

Still, the code inside the block following the ‘else’ keyword is executed (if an ‘else’ block is provided), if the condition evaluates to ‘false’. 

#include <iostream>

using namespace std;

int main() {

    int num;

    cout << "Enter a number: ";

    cin >> num;

    if (num > 0) {

        cout << "The number is positive." << endl;

    } else if (num < 0) {

        cout << "The number is negative." << endl;

    } else {

        cout << "The number is zero." << endl;

    }

    return 0;

}

 

  • for Loop:The for loop is a control structure that allows you to repeatedly execute a block of code a specified number of times. 
#include <iostream>

using namespace std;

int main() {

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

        cout << "Iteration " << i << endl;

    }

    return 0;

}
  • while loop: The while loop is another control structure that allows you to constantly execute a block of code as long as a specified condition is true.
#include <iostream>

using namespace std;

int main() {

    int count = 0;

    while (count < 5) {

        cout << "Count: " << count << endl;

        count++;

    }

    return 0;

}
  • do-while Loop: The ‘do-while’ loop in C++ is similar to the ‘while’ loop, but with one key difference: the code inside the loop’s body is executed at least once before the loop condition is checked. This ensures that the loop body is executed at least once, regardless of whether the loop condition was originally true or false.
#include <iostream>

using namespace std;

int main() {

    int num;

    do {

        cout << "Enter a positive number: ";

        cin >> num;

    } while (num <= 0);

    cout << "You entered a positive number: " << num << endl;

    return 0;

}
  • Switch Statement: The ‘switch’ statement is used to make a decision based on the value of an expression. It provides an indispensable way to handle multiple possible cases in a more concise manner than a series of nested ‘if-else’ statements. The ‘switch’ statement compares the value of an expression against various constant values (case labels) and executes the code associated with the matching case label.
#include <iostream>

using namespace std;

int main() {

    char grade;

    cout << "Enter your grade: ";

    cin >> grade;

    switch (grade) {

        case 'A':

            cout << "Excellent!" << endl;

            break;

        case 'B':

            cout << "Good job!" << endl;

            break;

        case 'C':

            cout << "Passing grade." << endl;

            break;

        default:

            cout << "You need to improve." << endl;

    }

    return 0;

}