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

Programming Fundamentals: C++ For Loop

The document discusses for loops in C++. It explains that for loops repeat a block of code until a test expression is false. The syntax of a for loop includes an initialization statement, test expression, and update statement. It provides an example of a for loop that calculates the factorial of a user-entered number, walking through how the loop variables and calculation are updated on each iteration until the test expression becomes false.

Uploaded by

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

Programming Fundamentals: C++ For Loop

The document discusses for loops in C++. It explains that for loops repeat a block of code until a test expression is false. The syntax of a for loop includes an initialization statement, test expression, and update statement. It provides an example of a for loop that calculates the factorial of a user-entered number, walking through how the loop variables and calculation are updated on each iteration until the test expression becomes false.

Uploaded by

Hash zee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Programming Fundamentals

C++ for Loop

11/10/2019
by

Programming Fundamentals by
SHEHZAD LATIF (03134797617)
SHEHZAD LATIF
Assistant Professor,
Hajvery University – Lahore
Email: [email protected]

1
C++ for Loop
Loops are used in programming to repeat a specific block of code.

11/10/2019
Loops are used in programming to repeat a specific block until some end

condition is met.

Programming Fundamentals by
SHEHZAD LATIF (03134797617)
There are three type of loops in C++ programming:

 for loop

 while loop

 do...while loop

2
C++ for Loop Syntax

for(initializationStatement; testExpression; updateStatement)

11/10/2019
{

// codes

Programming Fundamentals by
SHEHZAD LATIF (03134797617)
}

where, only testExpression is mandatory.

3
How for loop works?

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

11/10/2019
• Then, the test expression is evaluated.

• If the test expression is false, for loop is terminated. But if the test

Programming Fundamentals by
SHEHZAD LATIF (03134797617)
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
Flowchart of for Loop in C++

Programming Fundamentals by
11/10/2019
5

SHEHZAD LATIF (03134797617)


Example 1: C++ for Loop
// C++ Program to find factorial of a number
// Factorial on n = 1*2*3*...*n

11/10/2019
#include <iostream>
using namespace std;
int main()

Programming Fundamentals by
SHEHZAD LATIF (03134797617)
{
int i, n, factorial = 1;
cout << "Enter a positive integer: ";
cin >> n;
for (i = 1; i <= n; ++i) {
factorial *= i; // factorial = factorial * i;
}
cout<< "Factorial of "<<n<<" = "<<factorial;
return 0;
}
6
Output

Factorial of 5 = 120
Enter a positive integer: 5

Programming Fundamentals by
11/10/2019
7

SHEHZAD LATIF (03134797617)


In the program, user is asked to enter a positive integer which is stored in

variable n(suppose user entered 5). Here is the working of for loop:

11/10/2019
1. Initially, i is equal to 1, test expression is true, factorial becomes 1.

Programming Fundamentals by
SHEHZAD LATIF (03134797617)
2. i is updated to 2, test expression is true, factorial becomes 2.

3. i is updated to 3, test expression is true, factorial becomes 6.

4. i is updated to 4, test expression is true, factorial becomes 24.

5. i is updated to 5, test expression is true, factorial becomes 120.

6. i is updated to 6, test expression is false, for loop is terminated.

You might also like