0% found this document useful (0 votes)
76 views4 pages

CMSC 11 Handout 3

This document discusses iteration in Python using while and for loops. It provides examples of using while loops, which repeatedly execute a block of code as long as a condition is true, and for loops, which iterate over items in a sequence. Key aspects covered include the basic structure of while and for loops, nested loops, infinite loops, and how break and continue can alter loop behavior.

Uploaded by

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

CMSC 11 Handout 3

This document discusses iteration in Python using while and for loops. It provides examples of using while loops, which repeatedly execute a block of code as long as a condition is true, and for loops, which iterate over items in a sequence. Key aspects covered include the basic structure of while and for loops, nested loops, infinite loops, and how break and continue can alter loop behavior.

Uploaded by

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

CMSC 11: Introduction to Computer Science

Handout No. 3

Iteration with while and for statements

Iterative Statements
- Statements that causes the repeated execution of a sequence of statements
given that the statement is true. It is often called a looping statement.
There are two types of loops in Python (unlike in other programming languages).
1. While loop

A loop that repeatedly executes a statement as long as the statement is true.


A while loop must have an initialization, condition, and increment/decrement or
update statement.
Syntax:
while boolean_expression:
block_of_code
Example 1:
count = 0
while count < 10:
print(Hello.)
count = count + 1
Parts of the basic form of the loop
count = 0 #initialization
while count < 10: #condition/bool_expr
print(Hello.)
count = count + 1 #update

Example 2:
N = int(input(How many numbers will you enter?:))
i = 0
while i < N:
num = int(input(Enter a number: ))
print(Youve entered, num)
i = i + 1

1st Sem 2016-2017

RBAguila

CMSC 11: Introduction to Computer Science

Handout No. 3

2. For loop

A loop that iterates over items in a sequence whether a list or a string. It also must
have an initialization, condition, and increment.
Syntax:
for variable in sequence:
block_of_code

A sequence can be defined by range, strings, or lists.


for count in range(0,5):
print(Hello.)
range(start,stop) produces a sequence starting from start and ends with
stop-1. e.g. range(0,5) will create the sequence 0, 1, 2, 3, 4
Example 3:
N = int(input(How many numbers will you enter?:))
total = 0
for i in range(0,N):
num = int(input(Enter a number: ))
print(Youve entered, num)
total = total + num
print(The sum is, total)
`
Example 4:
#prints the numbers 0 to 4
i = 0
while i < 5:
print(i)
i = i +1
#for loop version
for i in range(0,5)
print(i)
While loop follows the basic form of the loop any change to the variable within the
loop body affects the semantics
For loop always reassigns its variable at the start of the loop
1st Sem 2016-2017

RBAguila

CMSC 11: Introduction to Computer Science

Handout No. 3

Example 5:
#this loop only stops when youve entered five zeroes
num_of_zeroes = 0
loop_num = 1
while num_of_zeroes < 5:
print(This is loop number:, loop_num)
num = int(input(Enter an integer: ))
if num == 0:
num_of_zeroes = num_of_zeroes + 1
loop_num = loop_num + 1
Nested Loops

Loops within a loop (and so on)


Example 6:
for i in range(0,3)
for j in range(0,4)
print(*, end=)
print()
# The end= parameter suppresses new lines. print() is the

same as \n.

Infinite Loops

Happen when the condition never becomes false.


Try to avoid this(unless you know what you are doing)
To terminate your program, just press CTRL+C
while true:
print(hindi na ako aasa.)

Behavior changing keywords


break exits out of the smallest enclosing loop. If a break is encountered, it terminates
the loop and transfers execution to the statement after the loop.
Example 7:
i = 0
while i < 10:
print(i)
break
i = i + 1
1st Sem 2016-2017

RBAguila

CMSC 11: Introduction to Computer Science

Handout No. 3

Example 8:
while True:
num = int(input(Enter a positive number:))
if num <= 0:
print(Youve entered a non-positive number.)
break
continue forces a loop again, skipping the rest of the body. Once a continue is
encountered, we stop the current iteration and move to the next one.

Example 9:
for i in range(0,10)
if i % 2 == 0
print(We found an even number.)
continue
print(i)
References:
1. Laboratory Guide (JMBawagan)
2. CMSC 11 Lab Session 2 Handout (GBCEmalada)

1st Sem 2016-2017

RBAguila

You might also like