In this tutorial, we'll explore how to use control flow in C++. Control flow refers to the order in which the program's code executes. It's one of the most essential concepts in programming for creating dynamic and interactive programs.
By the end of this tutorial, you will understand and be able to use different control flow mechanisms in C++, including conditional statements (like if
, else
, switch
), and loops (like for
, while
, do-while
).
Prerequisites: You should have a basic understanding of C++ programming, including how to write, compile, and run a simple C++ program.
Conditional statements allow us to execute different parts of code based on certain conditions.
If Statement: The if
statement is used to execute a block of code if a specified condition is true.
if (condition) {
// code to be executed if condition is true
}
If-Else Statement: The if-else
statement is used to execute one block of code if the condition is true, and another block of code if the condition is false.
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
Switch Statement: The switch
statement is used to execute one of many blocks of code, based on the value of a variable.
switch(expression) {
case constant1:
// code to be executed if expression is constant1
break;
case constant2:
// code to be executed if expression is constant2
break;
// you can have any number of case statements
default:
// code to be executed if expression doesn't match any constants
}
Loops allow us to repeatedly execute a block of code as long as a specified condition is true.
For Loop: The for
loop is used when you know beforehand how many times you want to execute a block of code.
for (initialization; condition; increment) {
// code to be executed
}
While Loop: The while
loop is used when you want to execute a block of code as long as a condition is true.
while (condition) {
// code to be executed
}
Do-While Loop: The do-while
loop is similar to the while
loop but tests the condition at the end of the loop. This ensures that the loop will be executed at least once.
do {
// code to be executed
} while (condition);
#include<iostream>
using namespace std;
int main() {
int num = 10;
if (num > 5) {
cout << "Number is greater than 5";
} else {
cout << "Number is not greater than 5";
}
return 0;
}
If num
is greater than 5, this program will print "Number is greater than 5". Otherwise, it will print "Number is not greater than 5".
#include<iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
cout << i << "\n";
}
return 0;
}
This program will print the numbers from 1 to 5, each on a new line.
In this tutorial, we've learned about control flow in C++, including conditional statements (if, else, switch) and loops (for, while, do-while). These control flow statements allow us to create more dynamic and interactive programs.
Next, you can explore more complex control flow concepts like nested loops and conditions, and how to use break
and continue
statements to further control your program's flow.
while
loop.if-else
statement.for
loop.Remember to test your code and understand how it works. If you're stuck, ask for help or look up solutions online.
Happy Coding!