A pencil-and-paper workbook: think it through first, learn the Delphi, then prove you can do it.
A computer is brilliant at repeating a task without getting bored. First you will see why that is useful, then you will meet the FOR loop.
A pie costs R12. Complete the running total after each pie is bought.
| Pies bought | Running total (R) |
|---|---|
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 |
Look at your table. What is done again and again? What changes each time? What stays the same?
var
iCount, iTotal : Integer;
begin
iTotal := 0;
for iCount := 1 to 5 do
begin
iTotal := iTotal + iCount;
end;
ShowMessage(IntToStr(iTotal)); // shows 15
end;Complete the table for the loop in Learn It (the FOR loop that adds 1 to 5). Write the value of iTotal after each pass.
iTotal := 0; for iCount := 1 to 5 do iTotal := iTotal + iCount;
| iCount | iTotal after this pass |
|---|---|
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 |
Trace this loop. Start with iResult = 100.
iResult := 100; for i := 4 downto 1 do iResult := iResult - i * 5;
| i | iResult after this pass |
|---|---|
| 4 | |
| 3 | |
| 2 | |
| 1 |
Write exactly what appears in memOut when this code runs.
for i := 1 to 4 do memOut.Lines.Add(IntToStr(i) + ' x 3 = ' + IntToStr(i * 3));
Write a FOR loop that displays the 7 times table from 1 to 10 in memTable (for example: 1 x 7 = 7).
Write the code to add up all the whole numbers from 1 to 100 and display the answer in lblTotal. Include your variable declarations. (The answer should be 5050.)
Display the numbers 10 down to 1 in memOut, and then the words Lift off! once the loop has finished.
Sometimes nobody knows in advance how many times a task must be repeated. You keep going until something happens. This is where WHILE and REPEAT come in.
// WHILE: test first. Sentinel = 0
iNum := StrToInt(InputBox('Input', 'Number (0 to stop):', ''));
while iNum <> 0 do
begin
iTotal := iTotal + iNum;
iNum := StrToInt(InputBox('Input', 'Number (0 to stop):', ''));
end;
// REPEAT: test last. Keeps asking until the age is valid
repeat
iAge := StrToInt(InputBox('Age', 'Enter your age (14 to 19):', ''));
until (iAge >= 14) and (iAge <= 19);Complete the table.
| WHILE ... DO | REPEAT ... UNTIL | |
|---|---|---|
| When is the condition tested? | ||
| Fewest times the body can run | ||
| The loop continues while the condition is… |
Circle the loop that suits each problem best.
| 1 | Display the numbers 1 to 50. | FOR WHILE REPEAT |
| 2 | Keep asking for a password until it is correct (it must be asked at least once). | FOR WHILE REPEAT |
| 3 | Add up numbers until the user types -1. The user might type -1 straight away. | FOR WHILE REPEAT |
| 4 | Print the 12 times table. | FOR WHILE REPEAT |
| 5 | Roll a die until a 6 comes up. | FOR WHILE REPEAT |
| 6 | Read marks until the user types 0, where 0 may be the very first entry. | FOR WHILE REPEAT |
Trace this code. For each pass, write whether the condition is True or False before the body runs, then the values after the body. The last row is the check that stops the loop.
iNum := 100; iCount := 0; while iNum > 10 do begin iNum := iNum div 2; Inc(iCount); end;
| Check | iNum > 10 ? | iNum | iCount |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 |
The user types 12, then 7, then 20, then 0.
iTotal := 0;
repeat
iNum := StrToInt(InputBox('Input', 'Number (0 to stop):', ''));
iTotal := iTotal + iNum;
until iNum = 0;| Typed | iNum | iTotal | iNum = 0 ? |
|---|---|---|---|
| 12 | |||
| 7 | |||
| 20 | |||
| 0 |
What is the final value of iTotal? Now imagine the sentinel had been -1 instead of 0 and the same code was used. What would go wrong, and how could you fix it?
This code should add 1 + 2 + 3 + 4 + 5, but the program freezes and then fails to display a result. Find three errors and write the corrections.
var
iNum, iTotal : Integer;
begin
iNum := 1;
while iNum <= 5 do
begin
iTotal := iTotal + iNum;
end;
lblTotal.Caption := iTotal;
end;This code should keep rolling a die until a 6 is rolled and then count the rolls. Find two errors.
repeat iPick := Random(6) + 1; Inc(iRolls); until iPick <> 6;
Write code that uses a loop to read marks with an InputBox until the user types -1. Then display the average in lblAvg. The -1 must not be counted. Only calculate the average if at least one mark was entered.
A string is just a row of characters, each with a position number. If you can walk along that row with a loop, you can count, search, reverse and change text without any built-in shortcuts.
Put your pencil on box 1. Move along one box at a time and count the letter E, exactly as a computer would. How many E's are there? Then write the steps you followed in order.
Start at box 5 and read backwards to box 1, writing each letter as you go. What word do you get?
sWord := 'LOGIC'; for i := 1 to Length(sWord) do memOut.Lines.Add(IntToStr(i) + ': ' + sWord[i]); // 1: L 2: O 3: G 4: I 5: C
iCode := Ord('A'); // 65
cNext := Chr(iCode + 1); // 'B'
cLower := Chr(Ord('A') + 32); // 'a'Use Ord('A') = 65 and Ord('a') = 97 to complete the table. Count along the alphabet.
| Character | Ord() value |
|---|---|
| A | |
| C | |
| F | |
| Z | |
| d | |
| 0 | |
| 5 |
Trace this code. Write Yes or No to show whether the letter is an E, and write the value of iCount after each pass.
sWord := 'REMEMBER';
iCount := 0;
for i := 1 to Length(sWord) do
if sWord[i] = 'E' then
Inc(iCount);| i | sWord[i] | Is it 'E'? | iCount |
|---|---|---|---|
| 1 | R | ||
| 2 | E | ||
| 3 | M | ||
| 4 | E | ||
| 5 | M | ||
| 6 | B | ||
| 7 | E | ||
| 8 | R |
What is displayed in lblOut? How many times does the loop body run?
sWord := 'ROBOT'; sRev := ''; for i := Length(sWord) downto 1 do sRev := sRev + sWord[i]; lblOut.Caption := sRev;
A sentence is stored in sSentence. Write the loop that counts how many spaces it contains and stores the answer in iSpaces.
Write code that builds a new string sResult containing the same characters as sSentence but without any spaces.
Each letter of the uppercase word in sWord is replaced by the next letter in the alphabet (A becomes B, B becomes C, and so on). Write code that puts the coded word in sCoded. You may assume the word has no letter Z.
What goes wrong in the secret code if the word contains a Z? Rewrite the line inside your loop so that Z becomes A.
Factors, primes and common divisors are old ideas from maths. Here you will first investigate them with pencil and paper, then teach the computer to do the same.
A prime number has exactly two factors: 1 and itself. Use this method (invented by Eratosthenes over 2000 years ago):
iCount := 0;
for iDivisor := 1 to iNumber do
if iNumber mod iDivisor = 0 then
Inc(iCount);
bPrime := (iNumber > 1) and (iCount = 2);iA := 48; iB := 18; while iB <> 0 do begin iRemainder := iA mod iB; iA := iB; iB := iRemainder; end; // iA now holds the GCD
iNumber = 12. Complete the table for every value of iDivisor.
iCount := 0;
for iDivisor := 1 to 12 do
if 12 mod iDivisor = 0 then
Inc(iCount);| iDivisor | 12 MOD iDivisor | Factor? | iCount |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 | |||
| 9 | |||
| 10 | |||
| 11 | |||
| 12 |
Is 12 a prime number? Use your table to explain.
iA starts at 48 and iB starts at 18. Complete one row for each pass of the loop.
while iB <> 0 do begin iRemainder := iA mod iB; iA := iB; iB := iRemainder; end;
| Pass | iRemainder | iA after | iB after |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 |
State the GCD of 48 and 18 from your table. Then calculate the LCM using (48 DIV GCD) × 18.
Write code that displays all the factors of iNumber in lblOut, separated by spaces (for 12 it would show 1 2 3 4 6 12).
Use nested loops to display every prime number from 2 to 30 in memOut. Think: the inner loop is the prime test you already wrote. What must be reset for every new number?
No notes, no computer. Show all your working. Write Delphi code neatly and use correct indentation.
Trace this code. Show the value of iTotal after each pass.
iTotal := 0;
for i := 1 to 6 do
if i mod 2 = 0 then
iTotal := iTotal + i;| i | i MOD 2 = 0 ? | iTotal |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| 6 |
What is the final value of iNum? How many times did the loop body run?
iNum := 3; while iNum < 40 do iNum := iNum * 2;
What does sOut contain at the end? Describe in one sentence what the code does.
sIn := 'GRADE'; sOut := ''; for i := 1 to Length(sIn) do sOut := sIn[i] + sOut;
Circle the best loop for each problem.
| 1 | Display every number from 1 to 20. | FOR WHILE REPEAT |
| 2 | Ask for a password until it is correct (at least one attempt). | FOR WHILE REPEAT |
| 3 | Read numbers until -1 is typed, which might be typed first. | FOR WHILE REPEAT |
| 4 | Display the numbers 10 down to 1. | FOR WHILE REPEAT |
Explain, in your own words, one difference between a WHILE loop and a REPEAT loop.
This code should add the numbers 1 to 10. Find and correct three errors.
var
i, iSum : Integer;
begin
for i = 1 to 10 do
begin
iSum := iSum + i;
end;
lblSum.Caption := iSum;
end;This code should count the vowels in sWord but fails. Find the error and write the corrected line.
iCount := 0;
for i := 0 to Length(sWord) do
if sWord[i] in ['A','E','I','O','U'] then
iCount := iCount + 1;Write code that displays the sum of all the odd numbers from 1 to 49 in lblOut.
A learner's name is typed in edtName. Write code that counts how many times the letter A (uppercase or lowercase) appears in the name and displays the answer in lblCount.
Use a REPEAT loop to keep asking for a mark with an InputBox until the mark is from 0 to 100 inclusive. Then display Mark accepted using ShowMessage.