Repetitive statements, or loops, are constructs that allow you to execute a block of code multiple times. They are essential for performing repetitive tasks efficiently. In C++, there are several types of loops: for, while, and do-while. Each type serves specific use cases and has its own syntax.
for LoopThe for loop is used when the number of iterations is known beforehand. It consists of three main components: initialization, condition, and increment/decrement.
Syntax:
for (initialization; condition; increment) {
// Code to execute on each iteration
}
Example:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
cout << "Iteration " << i << endl;
}
return 0;
}
Flow:
int i = 1).i <= 5).i++).while LoopThe while loop is used when the number of iterations is not known in advance and continues until a specified condition becomes false.
Syntax:
while (condition) {
// Code to execute as long as condition is true
}
Example:
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << "Iteration " << i << endl;
i++;
}
return 0;
}
Flow:
i <= 5).i++).do-while LoopThe do-while loop is similar to the while loop but guarantees that the loop body is executed at least once, as the condition is checked after the loop body.
Syntax:
do {
// Code to execute
} while (condition);
Example:
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
cout << "Iteration " << i << endl;
i++;
} while (i <= 5);
return 0;
}
Flow:
i <= 5).Loops can be nested, meaning you can place one loop inside another. This is useful for working with multi-dimensional data structures.
Example:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
cout << "i: " << i << ", j: " << j << endl;
}
}
return 0;
}
Flow:
i from 1 to 3.i, the inner loop runs for j from 1 to 2.Control statements like break and continue can alter the flow of loops:
break: Exits the loop immediately.continue: Skips the rest of the current iteration and proceeds to the next iteration.Example of break:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Exit the loop when i is 3
}
cout << "Iteration " << i << endl;
}
return 0;
}
Example of continue:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip the iteration when i is 3
}
cout << "Iteration " << i << endl;
}
return 0;
}
Repetitive statements (loops) allow for efficient execution of code blocks multiple times, which is crucial for tasks requiring iteration. Understanding the for, while, and do-while loops, along with control statements like break and continue, enables you to create flexible and powerful programs that can handle repetitive tasks effectively. By mastering these constructs, you can control the execution flow of your programs, making them more dynamic and responsive to user inputs and conditions.
Open this section to load past papers