0% found this document useful (0 votes)
16 views8 pages

C++ For Loop Examples and Syntax

Uploaded by

priya.by
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

C++ For Loop Examples and Syntax

Uploaded by

priya.by
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

🔹 What is a for Loop?

A for loop in programming is used when we know how many times we want to repeat a block of
code.
It is often called a counter-controlled loop.

🔹 Syntax in C++:
for(initialization; condition; update)

// body of loop

Explanation:

1. Initialization – Sets the starting value of the loop counter. (Runs only once at the start)

2. Condition – The loop runs as long as this is true.

3. Update – Changes the counter variable (usually increment or decrement).

4. Body – Code inside { } that executes repeatedly.

🔹 Flow of Execution:
1. Initialization happens once.

2. Check condition:

o If true → run loop body → then do update.

o If false → stop the loop.

🔹 Example 1: Print numbers from 1 to 5


#include <iostream>

using namespace std;

int main() {

for(int i = 1; i <= 5; i++) {

cout << i << " ";

return 0;
}

👉 Output:
12345

🔹 Example 2: Print even numbers from 2 to 10


#include <iostream>

using namespace std;

int main() {

for(int i = 2; i <= 10; i += 2) {

cout << i << " ";

return 0;

👉 Output:
2 4 6 8 10

🔹 Example 3: Reverse counting


#include <iostream>

using namespace std;

int main() {

for(int i = 10; i >= 1; i--) {

cout << i << " ";

return 0;

👉 Output:
10 9 8 7 6 5 4 3 2 1

🔹 Nested for Loop Example (Multiplication Table)


#include <iostream>

using namespace std;

int main() {

for(int i = 1; i <= 5; i++) {

for(int j = 1; j <= 5; j++) {

cout << i * j << "\t";

cout << endl;

return 0;

👉 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

🔹 Practice Questions for You:


1. Write a program to print all odd numbers between 1 and 20.

2. Write a program to print the factorial of a number using for loop.

3. Write a program to calculate the sum of first 10 natural numbers.

4. Write a program to print a pyramid pattern like this:

**

***

****

*****

Perfect 👍 You want 10 simple for loop programs in C++ with outputs.
Here’s a neat collection for your students:

🔹 1. Print numbers from 1 to 10


#include <iostream>

using namespace std;

int main() {

for(int i = 1; i <= 10; i++) {

cout << i << " ";

return 0;

Output:

1 2 3 4 5 6 7 8 9 10

🔹 2. Print even numbers from 1 to 20


#include <iostream>

using namespace std;

int main() {

for(int i = 2; i <= 20; i += 2) {

cout << i << " ";

return 0;

Output:

2 4 6 8 10 12 14 16 18 20

🔹 3. Print odd numbers from 1 to 20


#include <iostream>

using namespace std;

int main() {

for(int i = 1; i <= 20; i += 2) {


cout << i << " ";

return 0;

Output:

1 3 5 7 9 11 13 15 17 19

🔹 4. Print reverse numbers from 10 to 1


#include <iostream>

using namespace std;

int main() {

for(int i = 10; i >= 1; i--) {

cout << i << " ";

return 0;

Output:

10 9 8 7 6 5 4 3 2 1

🔹 5. Print table of 5
#include <iostream>

using namespace std;

int main() {

for(int i = 1; i <= 10; i++) {

cout << "5 x " << i << " = " << 5*i << endl;

return 0;

Output:

5x1=5

5 x 2 = 10
...

5 x 10 = 50

🔹 6. Sum of first 10 natural numbers


#include <iostream>

using namespace std;

int main() {

int sum = 0;

for(int i = 1; i <= 10; i++) {

sum += i;

cout << "Sum = " << sum;

return 0;

Output:

Sum = 55

🔹 7. Factorial of a number (say 5)


#include <iostream>

using namespace std;

int main() {

int fact = 1;

for(int i = 1; i <= 5; i++) {

fact *= i;

cout << "Factorial = " << fact;

return 0;

Output:

Factorial = 120
🔹 8. Print squares of numbers from 1 to 10
#include <iostream>

using namespace std;

int main() {

for(int i = 1; i <= 10; i++) {

cout << i << "^2 = " << i*i << endl;

return 0;

Output:

1^2 = 1

2^2 = 4

3^2 = 9

...

10^2 = 100

🔹 9. Print characters A to Z
#include <iostream>

using namespace std;

int main() {

for(char c = 'A'; c <= 'Z'; c++) {

cout << c << " ";

return 0;

Output:

A B C D E ... Z

🔹 10. Print simple star pattern


#include <iostream>

using namespace std;


int main() {

for(int i = 1; i <= 5; i++) {

for(int j = 1; j <= i; j++) {

cout << "*";

cout << endl;

return 0;

Output:

**

***

****

*****

You might also like