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

Document 4

The document introduces loops in programming, detailing three main types: FOR, WHILE, and REPEAT...UNTIL loops. It provides pseudocode and Python examples for each type, illustrating their use cases and syntax. The summary emphasizes the appropriate scenarios for each loop type, such as using FOR loops for known iterations and WHILE loops for conditions that need to be checked before execution.

Uploaded by

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

Document 4

The document introduces loops in programming, detailing three main types: FOR, WHILE, and REPEAT...UNTIL loops. It provides pseudocode and Python examples for each type, illustrating their use cases and syntax. The summary emphasizes the appropriate scenarios for each loop type, such as using FOR loops for known iterations and WHILE loops for conditions that need to be checked before execution.

Uploaded by

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

1.

Introduction to Loops
Loops allow us to repeat a block of code multiple times without rewriting it.
There are three main types of loops:

 FOR loop (Used when the number of iterations is known)


 WHILE loop (Used when the condition must be checked before
execution)
 REPEAT...UNTIL loop (Used when the condition is checked after
execution)

2. FOR Loop
The FOR loop is used when we know exactly how many times we need to
execute a set of instructions.

Pseudocode Syntax:

FOR counter FROM start TO end DO


// Statements to execute
ENDFOR

Example 1: Print numbers from 1 to 5

Pseudocode:

FOR i FROM 1 TO 5 DO
OUTPUT i
ENDFOR

Python Code:

for i in range(1, 6):


print(i)

Example 2: Calculate sum of first 10 numbers

Pseudocode:
sum <- 0
FOR i FROM 1 TO 10 DO
sum <- sum + i
ENDFOR
OUTPUT sum

Python Code:

sum = 0
for i in range(1, 11):
sum += i
print(sum)

Example 3: Print even numbers from 2 to 10

Pseudocode:

FOR i FROM 2 TO 10 STEP 2 DO


OUTPUT i
ENDFOR

Python Code:

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


print(i)

Example 4: Print squares of numbers from 1 to 5

Pseudocode:

FOR i FROM 1 TO 5 DO
OUTPUT i * i
ENDFOR

Python Code:

for i in range(1, 6):


print(i * i)

Example 5: Print "Hello" 3 times

Pseudocode:
FOR i FROM 1 TO 3 DO
OUTPUT "Hello"
ENDFOR

Python Code:

for i in range(3):
print("Hello")

3. WHILE Loop
The WHILE loop runs as long as a condition is TRUE. The condition is checked
before executing the loop body.

Pseudocode Syntax:

WHILE condition DO
// Statements to execute
ENDWHILE

Example 1: Print numbers from 1 to 5

Pseudocode:

num <- 1
WHILE num <= 5 DO
OUTPUT num
num <- num + 1
ENDWHILE

Python Code:

num = 1
while num <= 5:
print(num)
num += 1
Example 2: Print even numbers up to 10

Pseudocode:

num <- 2
WHILE num <= 10 DO
OUTPUT num
num <- num + 2
ENDWHILE

Python Code:

num = 2
while num <= 10:
print(num)
num += 2

Example 3: Take input until user enters 0

Pseudocode:

num <- USERINPUT


WHILE num <> 0 DO
num <- USERINPUT
ENDWHILE

Python Code:

num = int(input("Enter a number: "))


while num != 0:
num = int(input("Enter a number: "))

Example 4: Print first 5 multiples of 3

Pseudocode:

num <- 3
count <- 1
WHILE count <= 5 DO
OUTPUT num
num <- num + 3
count <- count + 1
ENDWHILE

Python Code:

num = 3
count = 1
while count <= 5:
print(num)
num += 3
count += 1

Example 5: Countdown from 10 to 1

Pseudocode:

num <- 10
WHILE num >= 1 DO
OUTPUT num
num <- num - 1
ENDWHILE

Python Code:

num = 10
while num >= 1:
print(num)
num -= 1

4. REPEAT…UNTIL Loop
This loop ensures that the block of code executes at least once, and then
repeats until the condition is TRUE.

The REPEAT UNTIL loop ensures that the code inside it runs at least once,
regardless of the condition. After the first execution, the loop will continue to
repeat until the specified condition becomes TRUE. This makes it different
from the WHILE loop, which checks the condition before executing the loop
body.
Pseudocode Syntax:

REPEAT
// Statements to execute
UNTIL condition

Example 1: Take input until user enters 0

Pseudocode:

REPEAT
num <- USERINPUT
UNTIL num = 0

Python Code (using a while loop since Python has no REPEAT


UNTIL):

while True:
num = int(input("Enter a number: "))
if num == 0:
break

Example 2: Print numbers from 1 to 5

Pseudocode:

num <- 1
REPEAT
OUTPUT num
num <- num + 1
UNTIL num > 5

Python Code:

num = 1
while True:
print(num)
num += 1
if num > 5:
break
5. Summary
 FOR loops are best for a known number of iterations.
 WHILE loops are best when we don’t know how many times we need to
run the loop beforehand.
 REPEAT UNTIL loops guarantee at least one execution and stop when
the condition becomes true.

Practice Questions:

1. Write a REPEAT UNTIL loop to take input from the user until they enter
a valid password (e.g., "pass123").
2. Write a REPEAT UNTIL loop to keep asking for a positive number until
the user enters one.
3. Write a WHILE loop to keep taking user input until they enter a
negative number.
4. Write a WHILE loop to calculate the sum of digits of a given number.

5. Write a FOR loop to print the first 10 multiples of 5.

You might also like