C++ Control Statements | if else, switch case, loops, break and continue


This complete tutorial on C++ Control Statements explains how to control the flow of execution in a C++ program. It covers conditional statements (if, else if, else), switch statements, looping constructs (for, while, do-while), and jump statements such as break, continue, and goto (concept only). The tutorial follows best coding practices and helps beginners write clear, logical, and efficient C++ programs.

Control Statements – Complete Tutorial

1. Control Statements in C++

Control statements determine the flow of execution in a C++ program. They allow programs to make decisions, repeat tasks, and jump to different parts of code based on conditions.

Types:

  1. Selection statements
  2. Iteration statements
  3. Jump statements

2. if, else if, else Statement

The if statement is used to execute code based on a condition.

Syntax:


if (condition) {
// code
} else if (condition) {
// code
} else {
// code
}

Example:


int marks = 75;

if (marks >= 90) {
cout << "Grade A";
} else if (marks >= 60) {
cout << "Grade B";
} else {
cout << "Grade C";
}

Best Practices:

  1. Use meaningful conditions
  2. Avoid deeply nested if statements
  3. Use braces {} for clarity

3. switch Statement

The switch statement is used when multiple conditions depend on a single variable.

Syntax:


switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}

Example:


int day = 3;

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

Key Points:

  1. break prevents fall-through
  2. default executes when no case matches
  3. Works with integers and characters

4. Loops in C++

Loops are used to execute a block of code repeatedly.

4.1 for Loop

Used when the number of iterations is known.


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

4.2 while Loop

Used when the condition is checked before execution.


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

4.3 do-while Loop

Executes at least once, condition checked after execution.


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

Best Practices:

  1. Avoid infinite loops
  2. Update loop variables correctly
  3. Choose loop type based on use case

5. break and continue

break

Terminates the loop or switch statement.


for (int i = 1; i <= 5; i++) {
if (i == 3)
break;
cout << i;
}

continue

Skips the current iteration and moves to the next one.


for (int i = 1; i <= 5; i++) {
if (i == 3)
continue;
cout << i;
}

6. goto Statement (Concept Only)

The goto statement transfers control to a labeled statement.

Example:


goto label;
label:
cout << "Hello";

Why Avoid goto?

  1. Makes code hard to read
  2. Difficult to debug
  3. Breaks structured programming

Best Practice:

  1. Avoid goto
  2. Use loops or functions instead

Common Mistakes to Avoid

  1. Missing break in switch
  2. Infinite loops
  3. Complex nested conditions
  4. Overusing goto

Summary

In this chapter, you learned how C++ control statements manage program flow using conditions, loops, and jump statements. These constructs are essential for building logical, efficient, and maintainable C++ programs.