Search Header Logo
[IGCS] Counting Loop

[IGCS] Counting Loop

Assessment

Presentation

Computers

9th Grade

Easy

Created by

Andy tsui

Used 2+ times

FREE Resource

9 Slides • 11 Questions

1

[IGCS] Counting Loop

Slide image

2

There are mainly two types of loops

  • Conditional loops - Number of time is indefinite

  • Counting loops - Loop with a definite number of times

3

Open Ended

What are the two types of condition loops?

4

Counting

counter <- 0

WHILE counter < 10 DO

⠀# do something....

⠀counter <- counter + 1

ENDWHILE

5

For loop

# The previous slide's counting algorithm can be rewrite using for loop:


FOR counter <- 0 to 9 DO

⠀# Do something

NEXT counter

6

Multiple Choice

If we write:

FOR counter <- 0 to 5 DO

PRINT counter

NEXT counter

What will be the last number to print?

1

0

2

4

3

5

4

6

7

FOR [count_var] <- [start] to [stop] DO

  • count_var the variable that used as counter, usually we use counter or i, j, k to name counter

  • start: the value that the count_var is initialized with. In other word, count_var = start when the loop is first executed

  • stop: the loop ends when counter reaches stop value.

  • i.e. count_var = stop when the last time the loop run

8

Python for loop is a bit different

# In Python

for counter in range(0, 10):

⠀print(counter)


# implies that the counter is counting from 0 to 9 (not including 10)

# so the above code will print from 0 - 9 only

9

Open Ended

Describe what is the following program produce:

FOR i <- 10 to 1 DO

⠀PRINT i

NEXT i

10

For the previous program, it will be:

for i in range(10, 0, -1):

⠀print(i)


# when coding in Python

11

Slide image

12

Multiple Choice

What is the range of the value (inclusive) if we write:

range(5)

1

1..5

2

0..5

3

0..4

4

1..4

13

Multiple Choice

What is the range of the value (inclusive) if we write:

range(0, 5)

1

1..5

2

0..5

3

0..4

4

1..4

14

Multiple Choice

What will be the sequence of number generated if we write:

range(1, 5, 1)

1

1 2 3 4 5

2

0 1 2 3 4 5

3

0 1 2 3

4

1 2 3 4

15

Multiple Choice

What will be the sequence of number generated if we write:

range(0, 5, 2)

1

0 1 2 3 4

2

0 1 2 3 4 5

3

0 2 4

4

0 2 4 6

16

Multiple Choice

What will be the sequence of number generated if we write:

range(3, 0, -1)

1

0 1 2 3

2

0 1 2

3

3 2 1 0

4

3 2 1

17

Open Ended

Write the range() function to generate 0-5 (inclusive)

18

Open Ended

Write the range() function to generate even numbers from 0-10 (inclusive)

19

Multiple Select

** Which of the following program(s) print first 5 odd numbers?

1

for i in range(10):

print(i*2 + 1)

2

for i in range(1, 9, 2):

print(i)

3

for i in range(1, 11, 2):

print (i)

4

for i in range(1, 10, 2):

print(i)

5

for i in range(0,5,2):

print(i*2 + 1)

20

Summary

  • Use For-loop when the number of times is definite

  • The counter range can be customized by specifying: start, end and interval

[IGCS] Counting Loop

Slide image

Show answer

Auto Play

Slide 1 / 20

SLIDE