Every meaningful program eventually needs to make decisions or repeat certain actions based on changing data. A program that simply executes one statement after another, from top to bottom, without ever branching or repeating, would be extremely limited in what it could actually accomplish.
Control statements are the tools that give a C++ program this flexibility. They allow the flow of execution to change based on conditions, letting a program choose between different paths, skip certain steps, or repeat a block of code multiple times until a specific requirement is met.
In this tutorial, you will learn how decision-making statements like if, if-else, nested if, and switch work in C++, how loops allow repeated execution, and how break and continue give you finer control over how those loops behave.
Control statements are instructions in a program that determine the order in which other statements are executed. Instead of always running in a strict sequence, a program can use control statements to branch into different paths or repeat certain blocks of code based on a condition.
#include <iostream>
using namespace std;
int main() {
string channel = "CS Engineering Gyan";
int subscribers = 105000;
if (subscribers >= 100000) {
cout << channel << " has crossed one lakh subscribers!" << endl;
}
return 0;
}
CS Engineering Gyan has crossed one lakh subscribers!
In this example, the message is only printed because the condition inside the if statement evaluates to true. Without this control statement, the program would have no way to decide whether or not to display that particular line.
The if statement is the most basic decision-making tool in C++. It evaluates a condition, and if that condition is true, the block of code inside it executes. If the condition is false, the block is simply skipped over.
if (condition) {
// code to execute if condition is true
}
#include <iostream>
using namespace std;
int main() {
string channel = "CS Engineering Gyan";
int videoLength = 18;
if (videoLength > 10) {
cout << channel << " uploaded a long-form video." << endl;
}
return 0;
}
CS Engineering Gyan uploaded a long-form video.
The if statement is useful when a specific action should only happen under one particular condition, without needing any alternate action to occur if that condition turns out to be false.
Often, a program needs to handle two possible outcomes: one action when a condition is true, and a different action when it is false. The if-else statement makes this possible by combining both paths into a single, clean structure.
if (condition) {
// executes when condition is true
} else {
// executes when condition is false
}
#include <iostream>
using namespace std;
int main() {
string channel = "CS Engineering Gyan";
int subscribers = 87000;
if (subscribers >= 100000) {
cout << channel << " has reached the milestone." << endl;
} else {
cout << channel << " is still growing towards the milestone." << endl;
}
return 0;
}
CS Engineering Gyan is still growing towards the milestone.
Since exactly one of the two blocks will always run, if-else is a reliable structure whenever a decision naturally has just two possible outcomes.
Some situations require checking more than two possibilities. C++ handles this using an else-if ladder, where multiple conditions are evaluated one after another until one of them turns out to be true.
if (condition1) {
// executes if condition1 is true
} else if (condition2) {
// executes if condition2 is true
} else {
// executes if none of the above are true
}
#include <iostream>
using namespace std;
int main() {
string channel = "CS Engineering Gyan";
int subscribers = 52000;
if (subscribers >= 100000) {
cout << channel << " has reached one lakh subscribers." << endl;
} else if (subscribers >= 50000) {
cout << channel << " has crossed fifty thousand subscribers." << endl;
} else {
cout << channel << " is still building its audience." << endl;
}
return 0;
}
CS Engineering Gyan has crossed fifty thousand subscribers.
C++ evaluates each condition from top to bottom and stops as soon as it finds one that is true, which means the order in which conditions are written can significantly affect the final result, especially when value ranges overlap.
A nested if statement is simply an if statement placed inside another if or else block. This structure is useful when a decision genuinely depends on more than one related condition, checked one after another.
#include <iostream>
using namespace std;
int main() {
string channel = "CS Engineering Gyan";
int subscribers = 105000;
bool monetizationEnabled = true;
if (subscribers >= 100000) {
if (monetizationEnabled) {
cout << channel << " is eligible for premium partnerships." << endl;
} else {
cout << channel << " has crossed subscribers but monetization is disabled." << endl;
}
} else {
cout << channel << " has not yet reached the required subscriber count." << endl;
}
return 0;
}
CS Engineering Gyan is eligible for premium partnerships.
While nested if statements are useful, relying on them too heavily can make code harder to follow. In many cases, combining conditions using logical operators can achieve the same result with a simpler structure.
The switch statement offers an alternative way to handle multiple possible values of a single variable. Instead of writing a long chain of else-if conditions, each possible value is organized into its own separate case.
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
default:
// code block if no case matches
}
#include <iostream>
using namespace std;
int main() {
string channel = "CS Engineering Gyan";
int dayNumber = 3;
string uploadTopic;
switch (dayNumber) {
case 1:
uploadTopic = "C++ Basics";
break;
case 2:
uploadTopic = "Pointers and Memory";
break;
case 3:
uploadTopic = "Object-Oriented Programming";
break;
default:
uploadTopic = "General Programming Tips";
}
cout << channel << " upload topic today: " << uploadTopic << endl;
return 0;
}
CS Engineering Gyan upload topic today: Object-Oriented Programming
The break statement plays an important role here, since it prevents execution from falling through into the next case once a match has already been found.
Forgetting to include a break statement is one of the most common mistakes made while working with switch statements, since C++ will continue executing every case below a matching one unless explicitly told to stop.
#include <iostream>
using namespace std;
int main() {
int rating = 2;
switch (rating) {
case 1:
cout << "Needs Improvement" << endl;
case 2:
cout << "Average Content" << endl;
case 3:
cout << "Great Content" << endl;
break;
default:
cout << "Invalid Rating" << endl;
}
return 0;
}
Average Content Great Content
Since there is no break statement after case 2, execution continues into case 3 as well, printing both messages rather than stopping after the first match.
Loops allow a block of code to repeat multiple times, either for a fixed number of repetitions or until a certain condition becomes false. C++ provides three main types of loops, each suited to slightly different situations.
for (initialization; condition; update) {
// code to repeat
}
#include <iostream>
using namespace std;
int main() {
string channel = "CS Engineering Gyan";
for (int week = 1; week <= 3; week++) {
cout << channel << " weekly upload " << week << " completed." << endl;
}
return 0;
}
CS Engineering Gyan weekly upload 1 completed. CS Engineering Gyan weekly upload 2 completed. CS Engineering Gyan weekly upload 3 completed.
while (condition) {
// code to repeat
}
#include <iostream>
using namespace std;
int main() {
string channel = "CS Engineering Gyan";
int subscribers = 97000;
while (subscribers < 100000) {
subscribers += 1000;
cout << channel << " subscribers now: " << subscribers << endl;
}
return 0;
}
CS Engineering Gyan subscribers now: 98000 CS Engineering Gyan subscribers now: 99000 CS Engineering Gyan subscribers now: 100000
do {
// code to repeat
} while (condition);
#include <iostream>
using namespace std;
int main() {
string channel = "CS Engineering Gyan";
int videoCount = 0;
do {
videoCount++;
cout << channel << " published video " << videoCount << endl;
} while (videoCount < 3);
return 0;
}
CS Engineering Gyan published video 1 CS Engineering Gyan published video 2 CS Engineering Gyan published video 3
Unlike the while loop, the do-while loop always executes its body at least once, since its condition is checked only after the first repetition has already run.
The break statement immediately ends a loop, even if its condition would otherwise still be true. It is commonly used when a specific situation is reached and there is no need to continue checking any further repetitions.
#include <iostream>
using namespace std;
int main() {
string channel = "CS Engineering Gyan";
for (int video = 1; video <= 10; video++) {
if (video == 5) {
cout << channel << " stopped uploads at video " << video << endl;
break;
}
cout << channel << " uploaded video " << video << endl;
}
return 0;
}
CS Engineering Gyan uploaded video 1 CS Engineering Gyan uploaded video 2 CS Engineering Gyan uploaded video 3 CS Engineering Gyan uploaded video 4 CS Engineering Gyan stopped uploads at video 5
The continue statement works differently from break. Rather than ending the loop entirely, it skips the remaining code for the current repetition and moves directly to the next one.
#include <iostream>
using namespace std;
int main() {
string channel = "CS Engineering Gyan";
for (int video = 1; video <= 5; video++) {
if (video == 3) {
continue;
}
cout << channel << " processed video " << video << endl;
}
return 0;
}
CS Engineering Gyan processed video 1 CS Engineering Gyan processed video 2 CS Engineering Gyan processed video 4 CS Engineering Gyan processed video 5
Notice that video 3 is skipped entirely, but the loop still continues running normally for the remaining values, unlike break, which would have stopped the loop completely at that point.
| if-else Statement | switch Statement |
|---|---|
| Works well with complex conditions and ranges of values. | Works best when comparing a single variable against fixed values. |
| Can evaluate multiple different variables within one structure. | Typically evaluates only one variable or expression. |
| May become harder to read with many conditions. | Often more organized and readable for many discrete cases. |
| Situation | Recommended Loop |
|---|---|
| Number of repetitions is known in advance. | for loop |
| Repetition depends on a condition that may change over time. | while loop |
| Code must run at least once regardless of the condition. | do-while loop |
| Mistake | Correct Practice |
|---|---|
| Using the assignment operator (=) instead of the equality operator (==) in a condition. | Always use == when comparing values inside an if statement. |
| Forgetting the break statement in a switch case. | Add a break after each case unless fall-through behavior is intentional. |
| Forgetting to update the loop variable, causing an infinite loop. | Always ensure the update statement changes the condition over time. |
| Confusing when to use break versus continue. | Use break to exit the loop completely, and continue to skip only the current repetition. |
Control statements give C++ programs the ability to make decisions and repeat actions, transforming a simple sequence of instructions into a flexible, responsive application. Starting with the if statement and moving through if-else structures, else-if ladders, and switch statements, C++ offers multiple tools for handling almost any decision-making scenario.
Loops such as for, while, and do-while allow tasks to repeat efficiently, while break and continue provide additional control over exactly how those repetitions behave. Together, these tools form the foundation for building programs that can adapt intelligently based on the data they process.
With a solid understanding of control statements, you are now ready to explore functions in C++, which allow you to organize code into reusable, well-structured blocks.