Comments with // (7c)

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Comments with // (7c)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line is a correct pseudocode comment?
A // Add the marks together
B Add the marks together //
2. What does a comment do when the algorithm is run?
A Nothing - it only explains the code to the reader
B It changes the value of a variable
3. Where can a comment go?
A On its own line above the code it explains
B Only at the very end of the program
4. Which line has a short comment at the end of a line correctly?
A Total ← Total + Mark Add mark to total //
B Total ← Total + Mark // Add mark to total
5. A comment needs 3 lines of explanation. How should it be written?
A Put // on the first line only
B Start every line with //

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1// Add the mark to the total AThe explanation has no // at the start, so it is not marked as a comment and looks like part of the code. It should be: Count ← Count + 1 // Add one to the count
2Total ← Total + Mark // running total B// Check if the student passed IF Mark >= 50 THEN
3A comment that needs several lines must start each line with // CThe comment on the second line is fine, but the last line ends with an empty //, which explains nothing. Also the comment lines should describe each statement. Remove the empty // or write a useful comment such as // Show the total
4A comment starts with // and explains the code without being part of the algorithm DA short comment at the end of a line, explaining that statement without changing it
5Count ← Count + 1 Add one to the count E// Calculate the average // of the three test marks
6// Read the mark // and add it to Total Input Mark Total ← Total + Mark OUTPUT Total // FA comment on its own line above the code, explaining what the next statement does

Definitions

//

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
__ Ask the user for their age INPUT Age
2.
Total ← 0 __ Start the total at zero
3.
// Add the three marks together __ then divide by three to find the mean Average ← Total / 3
4.
__ Repeat for each of the five students FOR Count ← 1 TO 5 INPUT Mark NEXT Count
5.
OUTPUT Total __ Show the final total

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A // Double the price
B DECLARE Total : REAL
C Total ← Price * 2 # doubled
D OUTPUT Total
Question 2
A INPUT Age
B / Check for an adult
C IF Age >= 18 THEN
D OUTPUT "Adult"
E ENDIF
Question 3
A DECLARE Total : INTEGER
B Total ← Mark1 + Mark2 + Mark3
C // Divide total by 3 to find
D the average mark
E Average ← Total / 3
Question 4
A Total ← 0
B FOR Count ← 1 TO 5
C // INPUT Num // read it
D Total ← Total + Num
E NEXT Count
Question 5
A /* Area of a rectangle */
B INPUT Length
C INPUT Width
D OUTPUT Length * Width

Answer Key

Comments with // (7c)
Section A: Quick Questions
Q1
A — // Add the marks together
Q2
A — Nothing - it only explains the code to the reader
Q3
A — On its own line above the code it explains
Q4
B — Total ← Total + Mark // Add mark to total
Q5
B — Start every line with //
Section B: Match the Pairs
Q1
// Add the mark to the total → A comment on its own line above the code, explaining what the next statement does
Q2
Total ← Total + Mark // running total → A short comment at the end of a line, explaining that statement without changing it
Q3
A comment that needs several lines must start each line with // → // Calculate the average // of the three test marks
Q4
A comment starts with // and explains the code without being part of the algorithm → // Check if the student passed IF Mark >= 50 THEN
Q5
Count ← Count + 1 Add one to the count → The explanation has no // at the start, so it is not marked as a comment and looks like part of the code. It should be: Count ← Count + 1 // Add one to the count
Q6
// Read the mark // and add it to Total Input Mark Total ← Total + Mark OUTPUT Total // → The comment on the second line is fine, but the last line ends with an empty //, which explains nothing. Also the comment lines should describe each statement. Remove the empty // or write a useful comment such as // Show the total
Section C: Definitions
Q1
// — Starts a comment that runs to the end of the line. It explains the code to the reader but is not part of the algorithm, so it is ignored when run.
Section D: Fill in the Blanks
Q1
// Ask the user for their age INPUT Age
Q2
Total ← 0 // Start the total at zero
Q3
// Add the three marks together // then divide by three to find the mean Average ← Total / 3
Q4
// Repeat for each of the five students FOR Count ← 1 TO 5 INPUT Mark NEXT Count
Q5
OUTPUT Total // Show the final total
Section E: Spot the Error
Q1
C — Pseudocode comments start with //, not #. The line should be Total ← Price * 2 // doubled.
Q2
B — A comment needs two slashes. A single / is not a comment. The line should be // Check for an adult.
Q3
D — A comment that runs over several lines needs // at the start of every line. The second line is missing its //, so it should be // the average mark.
Q4
C — The // at the start of the line turns the whole statement into a comment, so the number is never input. The line should be INPUT Num // read it.
Q5
A — /* ... */ is not the pseudocode comment style. A comment starts with //, so the line should be // Area of a rectangle.