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

For Loop C++ with various simple examples

  • 1.
  • 2.
    Loops § We alreadyknow 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 loopsare 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 § Writea program that prints numbers from 1 to n. n is an input. Imran Zualkernan 5
  • 6.
    Imran Zualkernan 6 #include <iostream> usingnamespace std; int main() { int n; cin >> n; for(int i=1; i<=n; i=i+1){ cout << i; } return 0; }
  • 7.
    Problem § Write aprogram 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 aprogram that prints the following pattern given an n. Imran Zualkernan 9
  • 10.
    #include <iostream> using namespacestd; 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 aprogram to produce the following output based on an input n. Imran Zualkernan 11
  • 12.
    Imran Zualkernan 12 Every timethrough 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 namespacestd; 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 aprogram 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 Printn 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 krows 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 namespacestd; 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 aprogram to produce all the numbers between a and b that are divisible by c. Imran Zualkernan 18
  • 19.
    Imran Zualkernan 19 #include <iostream> usingnamespace 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 loopsare 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