Looping in C++: For, While, and Do-While Loops
Looping in C++
For Loop
- The for loop is an entry-controlled loop, meaning the condition is checked before the loop body is executed.
- It's preferred when the number of iterations is known beforehand.
- Syntax:
cpp
for (initialization; test condition; update) {
// body of for loop
}
- Initialization: Initializes the loop variable to some initial value.
- Test Condition: If the condition is true, the loop body executes; if false, the loop terminates.
- Update Expression: Increments/decrements the loop variable after each iteration.
- Example:
cpp
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i < 5; i++){
cout << i << endl;
}
}
- Example printing even numbers between 2 and 20:
While Loop
- The while loop is also an entry-controlled loop.
- It repeatedly executes a block of code as long as the given condition remains true.
- Used when the exact number of iterations is unknown beforehand.
- Loop execution terminates based on the test condition.
- Syntax:
cpp
while (condition) {
// Body of the loop
update expression;
}
- The loop variable needs to be declared beforehand and updated in the body of the loop.
- Condition: The loop continues as long as this condition is true.
- Update Expression: Updates the loop variable to approach the termination condition.
- Body: The statements executed as long as the condition is true.
- Example:
cpp
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i < 5) {
cout << i << endl;
i++;
}
}
- Example prompting the user to enter numbers until -1 is entered.
Do-While Loop
- The do-while loop is an exit-controlled loop, meaning the loop body executes at least once.
- It continues executing as long as a given condition remains true.
- Guarantees that the loop body will execute at least once, regardless of the condition.
- Syntax:
cpp
do {
// Body of the loop
// Update expression
} while (condition);
- Condition: Checked after the loop body executes. If true, the loop continues; if false, the loop exits.
- Update Expression: Updates the loop variable to approach the termination condition.
- Body: Statements executed at least once and then as long as the condition remains true.
- Example:
cpp
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
cout << i << endl;
i++;
} while(i < 5);
}
- Example displaying a countdown from 10 to 1.
Code Examples
- Example with a
for loop:
cpp
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i >= 5; i++) {
cout << i << " ";
}
}
- Example with a
for loop and variable x:
cpp
#include <iostream>
using namespace std;
int main() {
int x = 0;
for (int i = 5; i >= 1; i--) {
x += i;
cout << x;
}
}
- Example to print even numbers using a
for loop:
cpp
#include <iostream>
using namespace std;
int main() {
for (int i = 2; i <= 10; i += 2) {
cout << i << " ";
}
}
- Example with a
while loop:
cpp
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 3) {
cout << "i";
i++;
}
}
- Example with a
do-while loop:
cpp
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
cout << (13) << " ";
i++;
} while (i <= 5);
}
- Example with a
do-while loop and variable y:
cpp
#include <iostream>
using namespace std;
int main() {
int y = 0, i = 1;
do {
y += i;
i++;
} while (i <= 3);
cout << y;
}
Nested Loops
- A nested loop is a loop statement inside another loop statement.
- Also referred to as a “loop inside a loop.”
- Syntax for Nested
for loop:
cpp
for (initialization; condition; increment) {
for (initialization; condition; increment) {
// statement of inside loop
}
// statement of outer loop
}
- Example:
cpp
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cout << "*";
}
cout << endl;
}
}
- Syntax for Nested
while loop:
cpp
while(condition) {
while(condition) {
// statement of inside loop
}
// statement of outer loop
}
*Example:
cpp
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 3) {
int j = 1;
while (j <= 3) {
cout << j;
j++;
}
cout << endl;
i++;
}
}
- Syntax for Nested
do-while loop:
cpp
do {
do {
// statement of inside loop
} while(condition);
// statement of outer loop
} while(condition);
*Example
cpp
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
int j = 1;
do {
cout << j << " ";
j++;
} while (j <= 3);
cout << endl;
i++;
} while (i <= 3);
}
- Any type of loop can be nested inside any other type to any level.
- Example:
cpp
do {
while(condition) {
for (initialization; condition; increment) {
// statement of inside for loop
}
// statement of inside while loop
}
// statement of outer do-while loop
} while(condition);
- Example:
cpp
#include <iostream>
using namespace std;
int main() {
int outer = 1;
do {
int middle = 1;
while (middle <= 2) {
for (int inner = 1; inner <= 3; inner++) {
cout << inner << " ";
}
cout << endl;
middle++;
}
outer++;
} while (outer <= 1);
}
Nested Loop Examples
- Example generating a multiplication table:
cpp
#include <iostream>
using namespace std;
int main() {
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 3; j++) {
cout << i * j << "\t";
}
cout << endl;
}
}
- Example printing a right-angled triangle of asterisks:
cpp
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 3; i++) {
for(int j = 1; j <= i; j++) {
cout << "*";
}
cout << endl;
}
}
- Example printing an inverted right-angled triangle of asterisks:
cpp
#include <iostream>
using namespace std;
int main() {
for(int i = 3; i >= 1; i--) {
for(int j = 1; j <= i; j++) {
cout << "*";
}
cout << endl;
}
}
- Example printing a pattern of 'X's and 'O's based on whether (i∗j) is even:
cpp
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i < 3; i++) {
for(int j = 1; j <= 3; j++) {
if((i * j) % 2 == 0) {
cout << "X";
} else {
cout << "O";
}
}
cout << endl;
}
}
- Example printing a square pattern with asterisks on the border:
cpp
#include <iostream>
using namespace std;
int main() {
int size = 3;
for(int i = 1; i <= size; i++) {
for (int j = 1; j <= size; j++) {
if(i == 1 || i == size || j == 1 || j == size) {
cout << "*";
} else {
cout << " ";
}
}
cout << endl;
}
}
- Example printing an inverted right-angled triangle using a while loop:
cpp
#include <iostream>
using namespace std;
int main() {
int i = 3;
while (i >= 1) {
int j = 1;
while (j <= i) {
cout << "*";
j++;
}
cout << endl;
i--;
}
}