


Khusboo Tayal
Control flow refers to the order in which individual statements, instructions, or function calls are executed in a program.
Conditional statements allow your program to make decisions based on conditions.
The if statement executes a block of code only if a specified condition is true.
if (condition) {
// Code to execute if the condition is true
}#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
if (age >= 18) {
cout << "You are eligible to vote." << endl;
}
return 0;
}Enter your age: 20 You are eligible to vote.
The else statement provides an alternative block of code if the condition in the if statement is false.
if (condition) {
// Code if condition is true
} else {
// Code if condition is false
}#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
if (age >= 18) {
cout << "You are eligible to vote." << endl;
} else {
cout << "You are not eligible to vote." << endl;
}
return 0;
}
Enter your age: 16 You are not eligible to vote.
The else if statement is used for multiple conditions.
if (condition1) {
// Code for condition1
} else if (condition2) {
// Code for condition2
} else {
// Code if none of the conditions are true
}#include <iostream>
using namespace std;
int main() {
int marks;
cout << "Enter your marks: ";
cin >> marks;
if (marks >= 90) {
cout << "Grade: A+" << endl;
} else if (marks >= 80) {
cout << "Grade: A" << endl;
} else if (marks >= 70) {
cout << "Grade: B" << endl;
} else if (marks >= 60) {
cout << "Grade: C" << endl;
} else {
cout << "Grade: F" << endl;
}
return 0;
}
Enter your marks: 85 Grade: A
The switch statement is a multi-way branch that allows you to choose from multiple options.
switch (expression) {
case value1:
// Code for case value1
break;
case value2:
// Code for case value2
break;
default:
// Code if none of the cases match
}
#include <iostream>
using namespace std;
int main() {
int choice;
cout << "Enter a number (1-3): ";
cin >> choice;
switch (choice) {
case 1:
cout << "You selected Option 1." << endl;
break;
case 2:
cout << "You selected Option 2." << endl;
break;
case 3:
cout << "You selected Option 3." << endl;
break;
default:
cout << "Invalid option." << endl;
}
return 0;
}
Enter a number (1-3): 2 You selected Option 2.
Looping statements allow you to execute a block of code multiple times.
Executes a block of code for a specified number of times.
for (initialization; condition; increment/decrement) {
// Code to be repeated
}#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
cout << "Count: " << i << endl;
}
return 0;
}Count: 1 Count: 2 Count: 3 Count: 4 Count: 5
Repeats a block of code as long as a specified condition is true.
while (condition) {
// Code to be repeated
}
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << "Count: " << i << endl;
i++;
}
return 0;
}Count: 1 Count: 2 Count: 3 Count: 4 Count: 5
Executes a block of code at least once, then checks the condition.
do {
// Code to be repeated
} while (condition);#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
cout << "Count: " << i << endl;
i++;
} while (i <= 5);
return 0;
}Count: 1 Count: 2 Count: 3 Count: 4 Count: 5
These statements alter the flow of control within loops.
for (int i = 1; i <= 5; i++) {
if (i == 3) break;
cout << i << " ";
}Output:
1 2
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
cout << i << " ";
}Output:
1 2 4 5
#include <iostream>
using namespace std;
int main() {
int i = 1;
label:
if (i <= 5) {
cout << i << " ";
i++;
goto label;
}
return 0;
}
1 2 3 4 5
In this tutorial, you learned: