Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Iterations in Programming

The document explains iteration statements in programming, focusing on loops that repeat sections of code. It distinguishes between counter-controlled loops (like FOR loops) and condition-controlled loops (like WHILE and REPEAT...UNTIL loops), providing pseudocode examples for each. Additionally, it includes exercises for calculating totals, averages, and counting even or odd numbers using these loop structures.

Uploaded by

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

Iterations in Programming

The document explains iteration statements in programming, focusing on loops that repeat sections of code. It distinguishes between counter-controlled loops (like FOR loops) and condition-controlled loops (like WHILE and REPEAT...UNTIL loops), providing pseudocode examples for each. Additionally, it includes exercises for calculating totals, averages, and counting even or odd numbers using these loop structures.

Uploaded by

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

Iterations in Programming

Chapter: 7

MAA - Aitchison College - Lahore 1

Iteration statements in programming


Iteration means Loops – sections of a program that repeat over and over again. ▪ Counter –
controlled loops – when you know exactly how many loops you need in the program.

▪ Condition – controlled loop – when you do not know how many times you need to repeat the loop. -
need to set a logical test
– the result of the test tells the computer whether to repeat the loop.
Note:
Every loop must have an exit condition. The exit condition is a logical test that tells the computer when to
stop repeating the loop.

MAA - Aitchison College - Lahore 2

Counter - Controlled Loop : FOR loop ▪ value.


A variable is set up, with a start value and an stop
iteration that is performed a set (fixed) number of times.
▪ This variable is automatically increased each time the loop is performed until the stop value is reached and
the iteration finishes.

Syntax of a pseudocode counter-controlled loop (FOR loop) :

FOR Variable ← Start Value TO Stop Value


FOR s
instructions
FOR Count 🡨 1
instructions
OUTPUT
NEXT Variable
NEXT Count

MAA - Aitchison College

Write a pseudocode to input a value by a user and multiply and print the value with the
number of iterations using counter - controlled loop.
DECLARE Value : INTEGER # Declaration of Variable named Value OUTPUT “ Enter a value ” # Prompt

INPUT Value # take input from user and store in variable Value FOR counter ← 1 TO 4 # this loop will

iterate five times, OUTPUT “Value is ” , Value * counter # multiply a value entered by user with # of

iteration NEXT counter # next iteration starts

OUTPUT “The loop has stopped.” # prints the output statement after loop ends. MAA - Aitchison College - Lahore

Exercise: Write a pseudocode to input 10 numbers and find the total and average using
counter controlled loop.
DECLARE Number: INTEGER
Total 🡨 0 // initialize the variable Total

FOR counter ← 0 TO 10
Number 🡨 INPUT “ Enter a positive number "
Total 🡨 Total + Number

NEXT counter

Average 🡨 Total / 10

OUTPUT “Total value is ” , Total , “Average value is ” , Average

MAA - Aitchison College - Lahore 5


Q: Write the pseudocode that ask a number from a Number 🡨 INPUT “ Enter a positive number "
user and print the 12 multiples of that number
using counter – controlled loop. FOR counter ← 1 TO 12
Times 🡨 Number * counter
OUTPUT Number, “*”, counter , “=”, Times
DECLARE Number : INTEGER
NEXT counter Even 🡨 Even + 1
Q: Write the pseudocode using counter – ELSE
controlled loop that input 10 numbers and prints
Odd 🡨 Odd + 1
how many of these numbers were even or odd.
ENDIF
DECLARE Number : INTEGER NEXT counter
Even, Odd 🡨 0 OUTPUT “Total even numbers:”, Even , “Total odd
FOR counter ← 0 TO 10 numbers:”, Odd
Number 🡨 INPUT “ Enter a positive number "
IF Number % 2 = 0 THEN
MAA - Aitchison College - Lahore 6

TOTALLING
Adding a new value into previous value
Total 🡨 Total + NewValue
Example:-
Input 10 numbers from user and output total and average of 10 numbers.
DECLARE Num, Count, Total: INTEGER
Total 🡨 0
FOR Count 🡨 1 to 3
Num 🡨 INPUT “Enter a number”
Total 🡨 Total + Num
NEXT Count
Average 🡨 Total/ 10
OUTPUT “Total of entered 10 numbers is”, Total
OUTPUT “Average of entered 10 numbers is”, Average
MAA - Aitchison College - Lahore 7

COUNTING
Adding 1 in previous value
Count 🡨 Count + 1
Example:-
Input 10 numbers from user and output how many numbers were greater then
50 DECLARE Num, Count, GreaterCount : INTEGER
GreaterCount 🡨 0
FOR Count 🡨 1 to 3
INPUT “Enter a number”, Num
IF Num > 50 THEN
GreaterCount🡨 GreaterCount +1
END IF
NEXT Count
OUTPUT “Numbers greater then 50 are” , MAA GreaterCount - Aitchison College - Lahore 8

Condition – controlled loops in programming


Pre-condition loops – may have Language
no iterations.

WHILE <Logical test> DO Pseudocode

<statements>
ENDWHILE
while <Logical test>: Python uses a colon to show the start of
the while loop and indentation to show
<statements>
which statements are in the while loop.

Post-condition loops – always has at Language


least one iteration.

REPEAT Pseudocode

<statements>
UNTIL <condition>
There is no REPEAT … UNTIL Loop statements in Python
MAA - Aitchison College - Lahore

9
Example (Pre-condition loop) Language

Number 🡨 1 Pseudocode

WHILE Number < 11 DO


OUTPUT Number
Number 🡨 Number + 1
ENDWHILE

Number = 1 Python uses a colon to show the start of


the while loop and indentation to show
while Number < 11 :
which statements are in the while loop.
print ( Number )
Number = Number + 1

MAA - Aitchison College - Lahore 10


Example ( Post condition loop) Language

Number 🡨 1 Pseudocode

REPEAT
OUTPUT Number
Number 🡨 Number + 1
UNTIL Number > 10

There is no REPEAT … UNTIL Loop statements in Python

MAA - Aitchison College - Lahore 11

You might also like