0% found this document useful (0 votes)
21 views17 pages

Lec 5 - For Loop With Exercises

The document provides an introduction to loops in C++, specifically focusing on for loops. It explains the structure, purpose, and syntax of for loops, along with examples demonstrating their use in printing numbers and calculating sums. Additionally, it highlights the potential for infinite loops if the loop condition is always true.

Uploaded by

shaikham2004
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)
21 views17 pages

Lec 5 - For Loop With Exercises

The document provides an introduction to loops in C++, specifically focusing on for loops. It explains the structure, purpose, and syntax of for loops, along with examples demonstrating their use in printing numbers and calculating sums. Additionally, it highlights the potential for infinite loops if the loop condition is always true.

Uploaded by

shaikham2004
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/ 17

For Loops

Introduction to Programming C++


Loops
• Loops are used to repeat a certain portion of the
code several times. The number of repetition
times is specified by the programmer.
• There are Three types of loops:

Introduction to Programming C++


1. While loop
2. For loop
3. Do While Loop

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)

Introduction to Programming C++


3. Have a condition for the counter (when should your
loop end)

4. Update the counter (increase it or decrease it)

• If the counter is not updated, that will cause an


infinite loop.
3
Operators used for comparison
in the condition

Introduction to Programming C++


4
For Loop
• A for loop is a type of control structure in programming that
allows you to repeat a block of code a specified number of
times.
• Useful for when we want to repeat an action a certain number
of times, for example, iterating over the elements in an array

Introduction to Programming C++


or processing a series of numbers.

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.

Introduction to Programming C++


• Purpose:
• To repeat a block of code multiple times
• The exact syntax and usage may vary from language to language,
but the purpose remains the same

• 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 }

Introduction to Programming C++


• initialization - initializes variables and is executed only once
• condition - If true, the loop continues to execute. If false, the loop
terminates.
• update - updates the value of initialized variables and again checks the condition

Example:
for (int num=0; num<2; num++)
{

BODY OF FOR LOOP 7

}
Flowchart

Introduction to Programming C++


Example 1

• Write a code that print the numbers from 0 to 20

Introduction to Programming C++


9
Prepare your laptops
Example 1
Output Screen
The numbers from 0 to 20 are:
0
1
#include <iostream> 2
using namespace std; 3
4
int main () 5
{ 6

Introduction to Programming C++


7
cout<<“The numbers from 0 to 20 are\n”; 8
for (int w=0; w<=20; w++) 9
10
{ 11
cout<<w<<endl; 12
13
} 14
return 0; 15
16
} 17
18
19 10
20
Example 2

• Write a code that print “Hello World ! “ five times

Introduction to Programming C++


11
Example 2
Output Screen

#include <iostream>
using namespace std;
int main()
{

Introduction to Programming C++


for (int i = 1; i <= 5; ++i) { Hello World!
Hello World!
cout << "Hello World! " << endl;
Hello World!
}
Hello World!
return 0; Hello World!
}

12
Example 3

• Write a code that print the sum of numbers between 1 and 5

Introduction to Programming C++


13
Example 3
Output Screen

#include<iostream>
using namespace std;
int main() {

Introduction to Programming C++


int n=5, sum=0 ;
for(int i =1;i<=n;i++)
The Sum = 15
sum=sum+i;
cout<<"The Sum = "<<sum;
return 0; }

14
Example 4

• Write a code that will ask the user to enter a number.


• The job of your code is to print the 5 numbers that will follow
the number that the user entered.

Introduction to Programming C++


• For example, if the user entered 4 the output should be :
5
6
7
8
9
15
Example 4
Output Screen
#include <iostream>
The user enters 5
using namespace std;
Enter a number
int main ()

Introduction to Programming C++


5
{ 6
int n; 7
cout<<"Enter a number\n"; 8
cin>> n; 9
10
for (int x=0; x<5; x++)
{
n=n+1;
cout<<n<<endl; 16
}
return 0;
}
C++ Infinite for loop
• If the condition in a for loop is always true, it runs forever
(until memory is full). For example,

// infinite for loop

Introduction to Programming C++


for(int i = 1; i > 0; i++) {
// block of code
}
In the above program, the condition is always true which will
then run the code for infinite times.

17

You might also like