C++ ProgrammingProgramming

C++ Control Structures | Conditional & Loops

Introduction

In C++, control structures are building blocks that control the flow of execution of a program. They allow programmers to make decisions and repeat certain parts of code based on conditions.

Broadly, control structures are divided into two categories:

  1. Conditional / Decision-making statements β†’ Execute code based on conditions.
  2. Looping statements β†’ Repeat a block of code multiple times.

1. Conditional Statements

These structures help the program take decisions.

(a) if statement

Executes a block of code only if the condition is true.

#include <iostream>
using namespace std;

int main() {
    int age = 20;
    if(age >= 18) {
        cout << "You are eligible to vote.";
    }
    return 0;
}

(b) if-else statement

Provides two alternative paths.

int marks = 45;
if(marks >= 40) {
    cout << "Pass";
} else {
    cout << "Fail";
}

(c) if-else-if ladder

Checks multiple conditions one after another.

int marks = 85;
if(marks >= 90) {
    cout << "Grade A";
} else if(marks >= 75) {
    cout << "Grade B";
} else if(marks >= 40) {
    cout << "Grade C";
} else {
    cout << "Fail";
}

(d) switch statement

Used when multiple choices depend on the value of a single variable.

int day = 3;
switch(day) {
    case 1: cout << "Monday"; break;
    case 2: cout << "Tuesday"; break;
    case 3: cout << "Wednesday"; break;
    default: cout << "Invalid day";
}

2. Looping Statements

These help execute a block of code repeatedly.

(a) for loop

Used when the number of iterations is known.

for(int i = 1; i <= 5; i++) {
    cout << "Hello World " << i << endl;
}

(b) while loop

Executes as long as the condition is true.

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

(c) do-while loop

Executes at least once, even if the condition is false.

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

3. Jump Statements in C

Jump statements are used to change the flow of control unconditionally in a program. They allow you to break out of loops, skip iterations, return from functions, or jump to specific points in the code.

3.1 break

The break statement is used to terminate a loop or a switch statement immediately.
Once break is executed, control jumps to the first statement after the loop or switch.

Example:

#include <stdio.h><br>int main() {<br>    for (int i = 1; i <= 5; i++) {<br>        if (i == 3) {<br>            break;  // exits the loop when i == 3<br>        }<br>        printf("%d ", i);<br>    }<br>    return 0;<br>}<br>

Output:

1 2

3.2 continue

The continue statement skips the current iteration of the loop and jumps to the next iteration.

Example:

#include <stdio.h>
int main() {
    for (int i = 1; i <= 5; i++) {
        if (i == 3) {
            continue;  // skips printing 3
        }
        printf("%d ", i);
    }
    return 0;
}

Output:

1 2 4 5

3.3 goto

The goto statement allows unconditional jump to a labeled statement in the same function.
πŸ‘‰ It’s generally discouraged because it can make code harder to read and maintain.

Example:

#include <iostream.h>
int main() {
    int i = 1;
start:
    if (i <= 3) {
        printf("%d ", i);
        i++;
        goto start;  // jumps back to 'start' label
    }
    return 0;
}

Output:

1 2 3

3.4 return

The return statement is used to exit from a function. It can optionally return a value to the calling function.

Example:

#include <stdio.h>
int add(int a, int b) {
    return a + b;  // exits function and sends value back
}

int main() {
    int sum = add(5, 3);
    printf("Sum = %d", sum);
    return 0;  // exits main function
}

Output:

Sum = 8

Summary

  • Conditional statements: if, if-else, if-else-if, switch.
  • Loops: for, while, do-while.
  • Jump Statements: break, continue, go-to, return.
  • These structures make programs dynamic, efficient, and logical.

Leave a Reply

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