Programming Constructs Flashcards

All 10 cards in this deck

Name the three basic programming constructs.

Sequence, selection (branching) and iteration.

Which construct do the keywords if, elseif and switch belong to?

Selection.

Which construct do the keywords for, while and do...until belong to?

Iteration.

What is the difference between count-controlled and condition-controlled iteration?

Count-controlled repeats a set number of times; condition-controlled repeats until/while a condition is met.

What is the difference between a while loop and a do...until loop?

A while loop checks the condition before the loop, so may run zero times; do...until checks after, so always runs at least once.

What are the steps to write a selection statement with more than two outcomes in OCR Exam Reference Language?

e.g. grade A if score is 80+, B if 60+, otherwise C

  1. Test the first condition: if score >= 80 then print("A")
  2. Add further tests: elseif score >= 60 then print("B")
  3. Catch the rest and close: else print("C") then endif

What are the steps to write a count-controlled loop in OCR Exam Reference Language?

e.g. print the numbers 1 to 5

  1. Set counter, start and end: for i = 1 to 5
  2. Put the repeated code inside: print(i)
  3. Close the loop: next i

In OCR Exam Reference Language, how do you write a switch/case statement?

e.g. act on the value of choice being 1, 2 or anything else

switch choice: then case 1: … case 2: … default: … then endswitch

True or false? A for loop can only count up in steps of 1.

False. A step can be given, e.g. for i = 0 to 10 step 2.

What are the steps to write a while loop in OCR Exam Reference Language?
e.g. keep doubling total, which starts at 1, while it is under 50

  1. Set the variable used in the condition before the loop: total = 1
  2. Write while and the condition: while total < 50
  3. Write the code to repeat, then close with endwhile: total = total * 2 then endwhile