Loops are repetition statements, more formally explained as a series of instructions that is repeated.
There are three types of loops that are used in programming:
- For Loop - repeating a series of instructions for a specified number of times
- While Loop - repeating a series of instructions while the condition is true
- Repeat Until Loop - repeating a series of instructions until the condition is true
For Loop (Also known as the 'do-loop') is considered a counter controlled loop.
Syntax for the For Loop
For <variable> = <begin> to <end> step <increment> do
<steps to be repeated>
end for
When applying the syntax practically, the command 'do' is not used, and the command 'end for' is changed to 'next'
Terminology
Snippet - a small section of a code
Loop variable - the variable used within the loop statement
Loop Block - the repetition statements starting with the command For and ending with the command Next
Increment - representing the value by which the loop variable increases
Iteration - repetition
Example of a Counter Control Algorithm
Write an algorithm that calculates and outputs the average of 15 numbers. If the average exceeds 20, calculate and output the square of the average; otherwise, output the words "Low average".
SOLUTION:
- total = 0
- for numbers = 1 to 15 do
- print "enter a number"
- input num
- let total = total + num
- next
- average = total / 15
- print "the average of fifteen numbers is", average
- if average > 20 then
- square = average * average
- print "the square of the average is", square
- else
- print "low average"
- end if
- input key
Line is is identified as - initializing a variable
Lines 2 to 6 - the loop block
Lines 9 to 14 - the conditional block
Lines 3 to 5 - the steps to be repeated
Line 6 - terminating the loop block
Logic: Repeat asking the user to enter a number for fifteen times. Within the loop, keep a tally of all the numbers being entered. After the loop, calculate and output the average. After the calculation of the average, determine if the average exceeded 20. If it did, calculate and output the square of the average. (The square of any number is the number multiplied by itself) If it did not exceed 20, then the value if false is to output a statement saying low average.
PRACTICE ALGORITHMS
1. Calculate and output the total cost of 5 items. If each item entered costs more than $20,00 output a statement saying that the item qualifies for a discount. Count and output the number of items that cost more than $20.00
2. Tickets to a movie costs as follows: Adults $15.00 Minors $10.00
Calculate and output the total cost for a group of ten people that has both adults and minors. (Solve this problem as a for loop with conditional statements AND NOT as a simple algorithm)
3. You IT Teacher wants to know how many students in your class feels that they will earn an "A" on this upcoming test. Write an algorithm that solves and displays how many students from your class says yes.
TABLES
In computer programming, tables are the basic concept of columns and rows with column headings. Using the For Loop, we can create conversion tables. To do so, we follow these easy steps:
- Display column headings
- Write the for statement indicating the range
- Perform the calculation(s)
- Display the results of the calculations by outputting the variable names.
Step 1 - Display column headings Print "Pounds","Kilograms"
Step 2 -Write the for statement For lb = 1 to 15
Step 3 - Perform calculation kg = lb * 0.45
Step 4 - Display variables print lb,kg
Step 5 - Terminate loop Next
Step 6 - Terminate algorithm Input key
Now you try one:
Print a conversion table that shows 3% and 5% of numbers ranging from 100 to 500 in steps of 10.
No comments:
Post a Comment