Lec 5 - For Loop With Exercises
Lec 5 - For Loop With Exercises
2
Loops
• Four essential parameter in each loop:
1. Define a counter (who is your counter)
2. Initialize the counter (what is the first value your counter will
have)
5
For Loop
• History:
• Introduced in ALGOL 60 programming language in the late 1950s
• Incorporated into many other programming languages including
C, Java, Python, etc.
• Example:
• Repeat a block of code 5 times 6
• Repeat a block of code until a particular condition is met.
For Loop
The syntax of for-loop is:
for (initialization; condition; update)
{ // body of-loop }
Example:
for (int num=0; num<2; num++)
{
}
Flowchart
#include <iostream>
using namespace std;
int main()
{
12
Example 3
#include<iostream>
using namespace std;
int main() {
14
Example 4
17