SlideShare a Scribd company logo
C++ For Loops
CMP120
Imran Zualkernan
1
Loops
§ We already know how to do if-then-else
§ We already know how to do while loops.
§ We already know how to do nested while loops.
§ Now we will learn how to do for loops.
Imran Zualkernan
2
ForLoops
§ For loops are a short cut for doing while loops so the
components are all the same.
§ Only the syntax of for loops is different.
Imran Zualkernan
3
Imran Zualkernan
4
int j = 1;
while(j < 10){
cout << j;
j=j+1;
}
for(int j=1; j<10; j=j+1){
cout << j;
}
1. Initialize 2. Condition
3. Increment
4.Work
For loops are another way or writing
a while loop.
Simple Example
§ Write a program that prints numbers from 1 to n. n
is an input.
Imran Zualkernan
5
Imran Zualkernan
6
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for(int i=1; i<=n; i=i+1){
cout << i;
}
return 0;
}
Problem
§ Write a program that takes n as input and prints the
following type of ladder. (n=5)
Imran Zualkernan
7
int main()
{
int n;
cin >> n;
for(int j=1; j<=n; j=j+1){
for(int i=1; i<=j; i=i+1){
cout << i;
}
cout << endl;
}
return 0;
}
Imran Zualkernan
8
j=1
j=2
j=3
j=4
j=5
Problem
§ Write a program that prints the following pattern given
an n.
Imran Zualkernan
9
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for(int j=1; j<=n; j=j+1){
for(int i=1; i<=j; i=i+1){
cout << i;
}
for(int i=1; i<=j; i=i+1){
cout << "*";
}
cout << endl;
}
return 0;
}
Imran Zualkernan
10
Prints i’s
Prints *’s
j=1
j=2
j=3
j=4
j=5
Problem
§ Write a program to produce the following output
based on an input n.
Imran Zualkernan
11
Imran Zualkernan
12
Every time through the loop.
1. Print forward from 1 to j
2. Print backward from j to 1
j=1
j=2
j=3
j=4
j=5
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for(int j=1; j<=n; j=j+1){
for(int i=1; i<=j; i=i+1){
cout << i;
}
for(int i=j; i>=1; i=i-1){
cout << i;
}
cout << endl;
}
return 0;
}
Imran Zualkernan
13
Printing forward from 1 to j
Printing backwards from j to 1
Prints forward
Prints backwards
Problem
§ Write a program to produce the following pattern
given a particular value of n.
Imran Zualkernan
14
Imran Zualkernan
15
k=1
k=2
k=3
k=4
k=5
J=1
j=1
j=2
j=3
j=4
j=5
i=1 i=3
i=2
Print n rows
For each row k print j sets of stars
For each set of stars j print from 1 to j
Imran Zualkernan
16
Print k rows
For each row print k sets of stars
For each set of stars print from 1 to j
for (k = 1 to n)
for (j = 1 to k)
for (i = 1 to j)
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for(int k =1; k<=n; k++){
for(int j=1; j<=k; j=j+1){
for(int i=1; i<=j; i=i+1){
cout << "*";
}
cout << " ";
}
cout << endl;
}
return 0;
}
Imran Zualkernan
17
Print n rows
Print j sets of
stars
Print j stars
k=1
k=2
k=3
k=4
k=5
J=1
j=1
j=2
j=3
j=4
j=5
i=1 i=3
i=2
Problem
§ Write a program to produce all the numbers between
a and b that are divisible by c.
Imran Zualkernan
18
Imran Zualkernan
19
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b >> c;
for(int i = a; i<=b; i=i+1){
if(i%c==0) cout << i << " is divisible by " << c << endl;
}
return 0;
}
Summary
§ For loops are just another way of writing while loops.
§ Nested for loops are used with indexs within indexes
§ Multiple for loops can be nested at any level.
§ Each for loop with all it’s four components can be
considered a block of computation.
Imran Zualkernan
20

More Related Content

PDF
Programming Fundamentals presentation slide
PPTX
12-Lec - Repetition For Loop.pptx
PPTX
C++ Programming Club-Lecture 3
PPT
C++ control loops
PPT
PPT
Counting and looping
PPTX
Introduction to Loops using C++. Exploring While&Do-While Loop
PDF
COneShotPart3.pdf1111111111111111111111111
Programming Fundamentals presentation slide
12-Lec - Repetition For Loop.pptx
C++ Programming Club-Lecture 3
C++ control loops
Counting and looping
Introduction to Loops using C++. Exploring While&Do-While Loop
COneShotPart3.pdf1111111111111111111111111

Similar to For Loop C++ with various simple examples (20)

PPT
FP 201 - Unit 3 Part 2
PPT
Iteration
PPTX
Overview of c++ language
PPTX
Lec7 - Loops updated.pptx
DOCX
CST2403 NOTES
PPTX
Iterative control structures, looping, types of loops, loop working
DOCX
C++ Loops General Discussion of Loops A loop is a.docx
PDF
computerprogrammingggggggggggggggggg.pdf
PPTX
lab-8 (1).pptx
PDF
4th_Ed_Ch03.pdf
PPT
PPTX
Iterations FOR LOOP AND WHILE LOOP .pptx
PPTX
operating system introduction to programming
PPTX
Cs1123 6 loops
PDF
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
PDF
Stanford splash spring 2016 basic programming
PDF
C++ control structure
PPTX
C_BASICS FOR C PROGRAMMER WITH SRIVATHS P
FP 201 - Unit 3 Part 2
Iteration
Overview of c++ language
Lec7 - Loops updated.pptx
CST2403 NOTES
Iterative control structures, looping, types of loops, loop working
C++ Loops General Discussion of Loops A loop is a.docx
computerprogrammingggggggggggggggggg.pdf
lab-8 (1).pptx
4th_Ed_Ch03.pdf
Iterations FOR LOOP AND WHILE LOOP .pptx
operating system introduction to programming
Cs1123 6 loops
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Stanford splash spring 2016 basic programming
C++ control structure
C_BASICS FOR C PROGRAMMER WITH SRIVATHS P
Ad

More from FazalHameed14 (13)

PPT
Instruction types arithmatics computer architecture
PPT
Performance Terminology and models computer architecture
PPT
High level language and Assembly language
PPT
Design of the memory hierarchy computer archiecture
PPT
Data path of Computer Architecture ALU and other components
PPT
Assembly language programming implemenation
PPTX
Synchronous counters digital logic design
PPTX
Digital Logic Design Synchronous logic circuits
PPT
Synchronous decade counters in digital logic design
PPT
Sequential circuits digital logic sesign
PPTX
Programming segment 8086 Memory model 8086
PPTX
Assembly programming 8085/8086 microprocessors
PPTX
Introduction to Computer System. 8085/8086 architecture
Instruction types arithmatics computer architecture
Performance Terminology and models computer architecture
High level language and Assembly language
Design of the memory hierarchy computer archiecture
Data path of Computer Architecture ALU and other components
Assembly language programming implemenation
Synchronous counters digital logic design
Digital Logic Design Synchronous logic circuits
Synchronous decade counters in digital logic design
Sequential circuits digital logic sesign
Programming segment 8086 Memory model 8086
Assembly programming 8085/8086 microprocessors
Introduction to Computer System. 8085/8086 architecture
Ad

Recently uploaded (20)

PPTX
Geodesy 1.pptx...............................................
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
web development for engineering and engineering
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Construction Project Organization Group 2.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
PPT on Performance Review to get promotions
PPTX
Sustainable Sites - Green Building Construction
DOCX
573137875-Attendance-Management-System-original
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPT
Mechanical Engineering MATERIALS Selection
Geodesy 1.pptx...............................................
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
web development for engineering and engineering
Embodied AI: Ushering in the Next Era of Intelligent Systems
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Model Code of Practice - Construction Work - 21102022 .pdf
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Construction Project Organization Group 2.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Internet of Things (IOT) - A guide to understanding
PPT on Performance Review to get promotions
Sustainable Sites - Green Building Construction
573137875-Attendance-Management-System-original
Operating System & Kernel Study Guide-1 - converted.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Mechanical Engineering MATERIALS Selection

For Loop C++ with various simple examples

  • 2. Loops § We already know how to do if-then-else § We already know how to do while loops. § We already know how to do nested while loops. § Now we will learn how to do for loops. Imran Zualkernan 2
  • 3. ForLoops § For loops are a short cut for doing while loops so the components are all the same. § Only the syntax of for loops is different. Imran Zualkernan 3
  • 4. Imran Zualkernan 4 int j = 1; while(j < 10){ cout << j; j=j+1; } for(int j=1; j<10; j=j+1){ cout << j; } 1. Initialize 2. Condition 3. Increment 4.Work For loops are another way or writing a while loop.
  • 5. Simple Example § Write a program that prints numbers from 1 to n. n is an input. Imran Zualkernan 5
  • 6. Imran Zualkernan 6 #include <iostream> using namespace std; int main() { int n; cin >> n; for(int i=1; i<=n; i=i+1){ cout << i; } return 0; }
  • 7. Problem § Write a program that takes n as input and prints the following type of ladder. (n=5) Imran Zualkernan 7
  • 8. int main() { int n; cin >> n; for(int j=1; j<=n; j=j+1){ for(int i=1; i<=j; i=i+1){ cout << i; } cout << endl; } return 0; } Imran Zualkernan 8 j=1 j=2 j=3 j=4 j=5
  • 9. Problem § Write a program that prints the following pattern given an n. Imran Zualkernan 9
  • 10. #include <iostream> using namespace std; int main() { int n; cin >> n; for(int j=1; j<=n; j=j+1){ for(int i=1; i<=j; i=i+1){ cout << i; } for(int i=1; i<=j; i=i+1){ cout << "*"; } cout << endl; } return 0; } Imran Zualkernan 10 Prints i’s Prints *’s j=1 j=2 j=3 j=4 j=5
  • 11. Problem § Write a program to produce the following output based on an input n. Imran Zualkernan 11
  • 12. Imran Zualkernan 12 Every time through the loop. 1. Print forward from 1 to j 2. Print backward from j to 1 j=1 j=2 j=3 j=4 j=5
  • 13. #include <iostream> using namespace std; int main() { int n; cin >> n; for(int j=1; j<=n; j=j+1){ for(int i=1; i<=j; i=i+1){ cout << i; } for(int i=j; i>=1; i=i-1){ cout << i; } cout << endl; } return 0; } Imran Zualkernan 13 Printing forward from 1 to j Printing backwards from j to 1 Prints forward Prints backwards
  • 14. Problem § Write a program to produce the following pattern given a particular value of n. Imran Zualkernan 14
  • 15. Imran Zualkernan 15 k=1 k=2 k=3 k=4 k=5 J=1 j=1 j=2 j=3 j=4 j=5 i=1 i=3 i=2 Print n rows For each row k print j sets of stars For each set of stars j print from 1 to j
  • 16. Imran Zualkernan 16 Print k rows For each row print k sets of stars For each set of stars print from 1 to j for (k = 1 to n) for (j = 1 to k) for (i = 1 to j)
  • 17. #include <iostream> using namespace std; int main() { int n; cin >> n; for(int k =1; k<=n; k++){ for(int j=1; j<=k; j=j+1){ for(int i=1; i<=j; i=i+1){ cout << "*"; } cout << " "; } cout << endl; } return 0; } Imran Zualkernan 17 Print n rows Print j sets of stars Print j stars k=1 k=2 k=3 k=4 k=5 J=1 j=1 j=2 j=3 j=4 j=5 i=1 i=3 i=2
  • 18. Problem § Write a program to produce all the numbers between a and b that are divisible by c. Imran Zualkernan 18
  • 19. Imran Zualkernan 19 #include <iostream> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; for(int i = a; i<=b; i=i+1){ if(i%c==0) cout << i << " is divisible by " << c << endl; } return 0; }
  • 20. Summary § For loops are just another way of writing while loops. § Nested for loops are used with indexs within indexes § Multiple for loops can be nested at any level. § Each for loop with all it’s four components can be considered a block of computation. Imran Zualkernan 20