Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 38

Programming

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.

• There are three type of loops in C++ programming:


1. for loop
2. while loop
3. do...while loop

2
C++ for Loop Syntax
• Syntax of for loop is:

for(initializationStatement; testExpression; updateStatement)


{ No semicolon after last statement
// codes
}
• Example
for( int counter = 1; counter <= 10; counter++ )
{
cout << counter << endl;
}

• 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

for( int counter = 1; counter <= 10; counter++ )


{
cout << counter << endl; Body of
} for loop

• The initialization statement is executed only once at the beginning.


• Then, the test expression is evaluated.
• If the test expression is false, for loop is terminated. But if the test
expression is true, codes inside body of for loop is executed and
update expression is updated.
• Again, the test expression is evaluated and this process repeats
until the test expression is false.
4
Example 1: C++ for Loop

Let’s suppose user enter


an integer 5

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

factorial=1 i=2 n=5

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

factorial=2 i=3 n=5

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

factorial=6 i=4 n=5

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

factorial=24 i=5 n=5

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

factorial=24 i=6 n=5

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;

cout << "Enter a positive integer: ";


cin >> n;

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


cout << n << " * " << i << " = " << n * i << endl;
}

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

• The while loop evaluates the test expression.


• If the test expression is true, codes inside the body of while loop is
evaluated.
• Then, the test expression is evaluated again. This process goes on until
the test expression is false.
• When the test expression is false, while loop is terminated.

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;

while( i <= 10){


cout << n << " * " << i << " = " << n * i << endl;
i++;
}

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;

cout << "Enter a positive integer: ";


cin >> n;

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

You might also like