0% found this document useful (0 votes)
4 views

intro+for+loop

The document explains the concept of for loops in programming, highlighting their utility for executing statements repeatedly. It describes how for loops iterate over sequences, using examples with strings and the range function. Additionally, it provides coding examples demonstrating the output of various for loop implementations.

Uploaded by

panwar14
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

intro+for+loop

The document explains the concept of for loops in programming, highlighting their utility for executing statements repeatedly. It describes how for loops iterate over sequences, using examples with strings and the range function. Additionally, it provides coding examples demonstrating the output of various for loop implementations.

Uploaded by

panwar14
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Introduction: For Loop

• Loops are useful for repeatedly executable statements


• While loop works on conditions whereas while loop don’t

How for loop works


Msg = ‘Hello’

Variable string

Msg Array
H e l l o

So the string is arranged in the form of an array.

Suppose if we write For x in msg:


Print(x)
It will print like this
H
e
l
l
o
first it prints H
X
H

Then again it repeats using for and prints the next letter
X

H e

 ✓
x
H e

 
l


It goes on repeating till the end of an array
This loop is also called as foreach loop in languages like java
and c++
Msg=’hello’
For x in msg:#for loop runs for each statement in the sequence
(print x)
Flow chart(for loop)

True
For I in Body of for
range(0,10)

false

so if the value condition is true so loop of the body is


executed continuously and then at one point it stops.
Basically for loop depends upon the elements in a sequence.
Elements has finished means false and elements has not
finished means true.
What is Range?
• Range is a function
• Suppose range(10)
• 0,1,2,3,4,5,6,7,8,9 it will stop at just one number before
the last number.
• If you give range(5,10) 5,6,7,8,9

• Start End
• Can also give step range(1,10,2) 1,3,5,7,9

• Start End Step


• Range(10,0,-1) 10,9,8,7,6,5,4,3,2,1

• Start End Step


• Range(-10,-1,2) –10,-8,-6,-5,-4

• Start End Step
• this is how range function works
Coding
Input:
msg='Hello'
for x in msg:
print(x)
Output:
H
e
l
l
o
it’s printing as H e l l o one by one . so, for loop executes
for each element inside given statement.
Usually this types of things which are having elements called
as sequence.
Input:
for i in range(10): #it will take in from 0 to 10
print(i)
Output:
0
1
2
3
4
5
6
7
8
9
Input:
for i in range(5,10): #it will print 5 to 10
print(i)
Output:
5
6
7
8
9
Input:
for i in range(0,10,2): #it will print on every
two steps from 0 to10
print(i)
Output:
0
2
4
6
8

Process finished with exit code 0


Input:
for i in range(10,0,-1): #it prints the numbers in
reverse order
print(i)
Output:
10
9
8
7
6
5
4
3
2
1
How i is working ?

For i in range(0,10):
Print(i)

0 1 2

It works upto 10.


For each I in this range(0,10)
Print(i) will be executed
Each i is 0,1,2---upto 9

You might also like