The for loop in C++ is one of the most commonly used loops to repeat a block of code a specific number of times. It's ideal when you know in advance how many times you want to execute a block of code.
1️⃣ Syntax of for Loop:
for (initialization; condition; increment/decrement) {
// Code to be executed
}
- initialization: This step is executed only once, before the loop starts. It's typically used to set the loop control variable.
- condition: This expression is evaluated before each iteration. If the condition is
true, the loop continues; otherwise, it exits. - increment/decrement: This step is executed after each iteration. It's used to update the loop control variable.
2️⃣ Example: Basic for Loop
Here’s a simple example that prints numbers from 1 to 5 using a for loop:
#include <iostream>
using namespace std;
int main() {
// for loop to print numbers 1 to 5
for (int i = 1; i <= 5; i++) {
cout << i << " ";
}
return 0;
}
Explanation:
- Initialization:
int i = 1(start at 1) - Condition:
i <= 5(continue as long asiis less than or equal to 5) - Increment:
i++(increaseiby 1 after each iteration)
Output:
1 2 3 4 5
3️⃣ Example: for Loop with Decrement
You can also use the for loop to count downwards. Here's an example where we print numbers from 5 to 1:
#include <iostream>
using namespace std;
int main() {
// for loop to print numbers 5 to 1
for (int i = 5; i >= 1; i--) {
cout << i << " ";
}
return 0;
}
Explanation:
- Initialization:
int i = 5(start at 5) - Condition:
i >= 1(continue as long asiis greater than or equal to 1) - Decrement:
i--(decreaseiby 1 after each iteration)
Output:
5 4 3 2 1
4️⃣ Example: Nested for Loop
A nested for loop is a for loop inside another for loop. It's useful when you need to perform multiple iterations for each iteration of the outer loop. Here's an example that prints a multiplication table:
#include <iostream>
using namespace std;
int main() {
int rows = 5;
// Nested for loop for multiplication table
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows; j++) {
cout << i * j << "\t"; // Print the product
}
cout << endl; // Move to the next line after each row
}
return 0;
}
Explanation:
- The outer loop runs from
1to5(the number of rows). - The inner loop runs from
1to5for each iteration of the outer loop, printing the product ofiandj.
Output:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
5️⃣ Example: for Loop with Multiple Initialization or Increment
You can have multiple initialization and increment expressions in a for loop. Here's an example where we initialize multiple variables and update them in the loop:
#include <iostream>
using namespace std;
int main() {
// Multiple initialization and increment in for loop
for (int i = 1, j = 10; i <= 5; i++, j--) {
cout << "i: " << i << ", j: " << j << endl;
}
return 0;
}
Explanation:
- Initialization:
int i = 1, j = 10(initialize two variablesiandj) - Condition:
i <= 5(loop continues untilireaches 5) - Increment/Decrement:
i++andj--(increaseiand decreasejafter each iteration)
Output:
i: 1, j: 10
i: 2, j: 9
i: 3, j: 8
i: 4, j: 7
i: 5, j: 6
6️⃣ Example: for Loop with continue and break
continue: Skips the rest of the loop's body for the current iteration and moves to the next iteration.break: Exits the loop entirely, even if the condition hasn't been met.
Here’s an example using both continue and break:
#include <iostream>
using namespace std;
int main() {
// for loop with continue and break
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue; // Skip the iteration when i is 5
}
if (i == 8) {
break; // Exit the loop when i is 8
}
cout << i << " ";
}
return 0;
}
Explanation:
- The loop will skip the number 5 because of the
continuestatement. - The loop will terminate when
iis 8 due to thebreakstatement.
Output:
1 2 3 4 6 7
7️⃣ Key Points to Remember:
- Initialization: Typically done at the beginning of the
forloop. - Condition: Checked before each iteration.
- Increment/Decrement: Used to update the loop variable (e.g.,
i++ori--). - Nested Loops: Useful for multi-dimensional problems, like matrices or grids.
- Control Statements:
continueskips the current iteration, whilebreakexits the loop.
Would you like to explore any specific use cases for the for loop, like processing arrays or strings, or would you prefer to dive deeper into more advanced loops?
0 Comments