Professional Documents
Culture Documents
Programming Fundamentals C++
Programming Fundamentals C++
Fundamentals
C++
Loops in c++
Programming Fundamentals
Zainab Ishfaq
1
Loops in c++
• Loops are used in programming to repeat a specific
block until some end condition is met.
2
C++ for Loop Syntax
• Syntax of for loop is:
• Output:
Prints integers from one to ten
3
Flow chart of FOR loop in c++ and how does it work?
Update statement
Initialization Test counter=counter+1;
Example : statement expression
n=5
5
Example 1: C++ for Loop
i=1 n=5
1 is less then or
equal 5
Condition is TRUE!
Factorail=factorial × i
factorial=1×1
factorial=1
Increment i
i=i+1
i=1+1
i=2
6
Example 1: C++ for Loop
2 is less then or
equal 5
Condition is TRUE!
factorail=factorial × i
factorial=1×2
factorial=2
Increment i
i=i+1
i=2+1
i=3
7
Example 1: C++ for Loop
3 is less then or
equal 5
Condition is TRUE!
Factorail=factorial × i
factorial=2×3
factorial=6
Increment i
i=i+1
i=3+1
i=4
8
Example 1: C++ for Loop
4 is less then or
equal 5
Condition is TRUE!
Factorail=factorial × i
factorial=6×4
factorial=24
Increment i
i=i+1
i=4+1
i=5
9
Example 1: C++ for Loop
5 is less then or
equal 5
Condition is TRUE!
Factorail=factorial × i
factorial=24×5
factorial=120
Increment i
i=i+1
i=5+1
i=6
10
Example 1: C++ for Loop
6 is less then or
equal 5
Condition is FALSE!
TERMINATE!
11
Example 1: C++ for Loop
12
Class Activity
• Generate the multiplication table of a number (entered by the user)
using for loop.
13
Solution:
#include <iostream> Output
using namespace std;
int main()
{
int n;
return 0;
}
14
Home Work
Positive integers 1, 2, 3, 4... are known as natural numbers.
This program takes a positive integer from user( suppose user
entered n ) then, this program displays the value of 1+2+3+....+n.
• Output:
15
C++ while Loop where, testExpression is checked on each entry of the while
loop.
• The syntax of a while loop is:
while (testExpression)
{
// codes
}
• Example
int counter = 1;
while(counter <= 5)
{
cout << counter << endl;
counter++;
}
• Output:
Prints integers from one to five 16
Flow chart of WHILE loop in c++ and how does it work?
Example: Test
expression
int counter = 1;
while(counter <= 5)
{ Body of
cout << counter << endl; while
counter++; loop
17
Example 2: C++ while Loop
Let’s suppose user enter
an integer 5
number=5
18
Example 2: C++ while Loop
number= factorial=
i=1
5 1
1 is less then or
equal 5
Condition is TRUE!
Factorail=factorial × i
factorial=1×1
factorial=1
Increment i
i=i+1
i=1+1
i=2
19
Example 2: C++ while Loop
number= factorial=
i=2
5 1
2 is less then or
equal 5
Condition is TRUE!
Factorail=factorial × i
factorial=1×2
factorial=2
Increment i
i=i+1
i=2+1
i=3
20
Example 2: C++ while Loop
number= factorial=
i=3
5 2
3 is less then or
equal 5
Condition is TRUE!
Factorail=factorial × i
factorial=2×3
factorial=6
Increment i
i=i+1
i=3+1
i=4
21
Example 2: C++ while Loop
number= factorial=
i=4
5 6
4 is less then or
equal 5
Condition is TRUE!
Factorail=factorial × i
factorial=6×4
factorial=24
Increment i
i=i+1
i=4+1
i=5
22
Example 2: C++ while Loop
number= factorial=
i=5
5 24
5 is less then or
equal 5
Condition is TRUE!
Factorail=factorial × i
factorial=24×5
factorial=120
Increment i
i=i+1
i=5+1
i=6
23
Example 2: C++ while Loop
number= factorial=
i=6
5 120
6 is less then or
equal 5
Condition is FALSE!
Terminate!
24
Class Activity
• Generate the multiplication table of a number (entered by the user)
using while loop.
25
Solution:
#include <iostream>
using namespace std;
Output
int main()
{
int n,i=1;
cout << "Enter a positive integer: ";
cin >> n;
return 0;
}
26
Home Work
Positive integers 1, 2, 3, 4... are known as natural numbers.
This program takes a positive integer from user( suppose user
entered n ) then, this program displays the value of 1+2+3+....+n.
Implement while loop.
• Output:
27
C++ do..while Loop Syntax
• Syntax of do..while loop is:
do {
// codes
}
while (testExpression);
•Example
int i=1;
do {
cout << i << endl;
i++;
}
while(i<=5);
• Output:
28
Prints integers from one to five.
Flow chart of DO..WHILE loop in c++ and how does it work?
Example :
int counter=1;
Body of
do{
do.while
cout << counter << endl;
loop
counter++
}while(counter<=5); Test
expression
• The codes inside the body of loop is executed at least once. Then,
only the test expression is checked.
• If the test expression is true, the body of loop is executed. This
process continues until the test expression becomes false.
• When the test expression is false, do...while loop is terminated.
29
Example 3: C++ do..while Loop
Let’s suppose user enter
an integer 5
number=5
30
Example 3: C++ do..while Loop
number= factorial=
i=1
5 1
Factorail=factorial × i
factorial=1×1
factorial=1
Increment i
i=i+1
i=1+1
i=2
2 is less then or
equal 5
Condition is TRUE!
31
Example 3: C++ do..while Loop
number= factorial=
i=2
5 1
Factorail=factorial × i
factorial=1×2
factorial=2
Increment i
i=i+1
i=2+1
i=3
3 is less then or
equal 5
Condition is TRUE!
32
Example 3: C++ do..while Loop
number= factorial=
i=3
5 2
Factorail=factorial × i
factorial=2×3
factorial=6
Increment i
i=i+1
i=3+1
i=4
4 is less then or
equal 5
Condition is TRUE!
33
Example 3: C++ do..while Loop
number= factorial=
i=4
5 6
Factorail=factorial × i
factorial=6×4
factorial=24
Increment i
i=i+1
i=4+1
i=5
5 is less then or
equal 5
Condition is TRUE!
34
Example 3: C++ do..while Loop
number= factorial=
i=5
5 24
Factorail=factorial × i
factorial=24×5
factorial=120
Increment i
i=i+1
i=5+1
i=6
6 is less then or
equal 5
Condition is FALSE!
TERMINATE! 35
Class Activity
• Generate the multiplication table of a number (entered by the user)
using for loop.
36
Solution:
#include <iostream>
using namespace std;
Output
int main()
{
int n,i=1;
do{
cout << n << " * " << i << " = " << n * i << endl;
i++;
}
while(i <= 10);
return 0;
} 37
Home Work
Positive integers 1, 2, 3, 4... are known as natural numbers.
This program takes a positive integer from user( suppose user
entered n ) then, this program displays the value of 1+2+3+....+n.
Implement do..while loop
• Output:
38