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

Chap 9-Part 3 1

The program prompts the user to enter a number, reads the input, and checks if it is between 10 and 20 or greater than 100 using logical operators connected with brackets. It repeats this process until a valid number is entered.

Uploaded by

Muaz Malik
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)
9 views

Chap 9-Part 3 1

The program prompts the user to enter a number, reads the input, and checks if it is between 10 and 20 or greater than 100 using logical operators connected with brackets. It repeats this process until a valid number is entered.

Uploaded by

Muaz Malik
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/ 14

Iteration (the repetition of a process)

 When some actions performed as part of an algorithm need


repeating, this is called an Iteration.
 Loop structures are used to perform the iteration
To perform iteration using FOR-TO-NEXT Loop

➢ A FOR loop has a fixed number of repeats.


➢ Loop counter is automatically ,managed
➢ The STEP increment is an optional expression that must be an integer
➢ An increment can be specified as follows:
FOR <identifier> ← <value1> TO <value2> STEP <increment>
<statement(s)>
NEXT <identifier>
➢ In this case the identifier will be assigned the values from value1 in successive
increments of increment until it reaches value2. If it goes past value2, the loop
terminates. The increment can be negative.
Iteration using REPEAT-UNTIL Loop
➢ Statements in a REPEAT loop are always executed at least
once (because the condition for exiting the loop is checked
at the end of the loop)
➢ This loop works on condition being FALSE
➢ Actions are repeated until a given condition becomes true,
meaning the loop terminates when the condition becomes
true
DIY
Write a pseudocode using REPEAT-UNTIL to take
marks as input and add them, stop taking input
when -1 is entered
Iteration using WHILE- DO-ENDWHILE
loop
Practice of all loops - DIY
Logic Operators
 WHILE and REPEAT loops and IF statements make use of comparisons to decide
whether statements within a loop are repeated or a statement or group of
statements are executed.
 Why not For loop?
 The comparisons make use of relational operators (discussed previously) and the
logic operators:
 AND
 OR
 NOT
 The outcome of these comparisons is always either True or False
 A simple algorithm using the logic operators:
Nested IF

 Algorithm that checks if a percentage mark is valid and whether it is a pass or a fail
 This makes use of 2 IF statements, the second IF statement is part of the first ELSE path.
This is called Nested IF
Pseudocode:
OUTPUT “Please enter a mark”
INPUT PercentageMark
IF (PercentageMark < 0) OR (PercenrageMark >100)
THEN
OUTPUT “Invalid Mark”
ELSE
IF PercentageMark > 49
THEN
OUTPUT “Pass”
ELSE
OUTPUT “Fail”
ENDIF
ENDIF
A Sample Pseudocode: to find the
average of a number of integers input
 A more realistic algorithm would include checks that all the values input are
whole numbers and that the number input to determine how many integers are
input is also positive.
 We’ll make use of the function INT(x) that returns the integer part of x:
INT(x : REAL) RETURNS INTEGER
returns the integer part of x
Example: INT(27.5415) returns 27
➢ The identifier table for this algorithm is:
Solution using Nested Loops
Activity 9E
In pseudocode, write statements to check that a number input is between 10 and 20 or
over 100. Make use of brackets to ensure that the order of the comparisons is clear.

Number <- 0
REPEAT
PRINT "Enter a number, your number must be between
10 and 20 or over 100"
INPUT Number
UNTIL (Number > 10 AND Number < 20) OR (Number > 100)
Activity 9F
In pseudocode, write an algorithm to set a password for a user when they have to input the same word
twice. Then allow the user three attempts to enter the correct password. Complete an identifier table
for your algorithm.

Finally, check your pseudocode algorithm works by writing a short program from your
pseudocode statements using the same names for your identifiers.
DECLARE Password1, Password2 : STRING
DECLARE Counter : INTEGER
Counter <- 0
REPEAT
OUTPUT "Enter your password "
INPUT Password1
OUTPUT "Enter your password again "
INPUT Password1
Counter <- Counter + 1
UNTIL (Counter = 3) OR (Password1 = Password2)
IF Password1 = Password2
THEN
OUTPUT "Password correct"
ELSE
OUTPUT "Password incorrect"
ENDIF

Identifier Name Description


Counter To count the number of attempts
Password1 To store the first password attempt
Password2 To store the second password attempt

You might also like