pseudocode
Algorithm: (tl;dr) A finite set of steps to solve a problem. Here are examples:
swap
guessNumber
guessNumberAlternate
paperRockScissors
doubleDiceRollGame
From the Digital Solutions syllabus:
Algorithmic design method
Pseudocode will be used as the formal method of representing algorithms in this syllabus. Pseudocode is a descriptive method used to represent an algorithm and is a mixture of everyday language and programming conventions.
Pseudocode implements the basic control structures of assignment, sequence, selection, condition, iteration and modularisation through the use of keywords associated with the constructs, and textual indentation. Used to show how a computing algorithm should and could work, it is often an intermediate step in programming between the planning stage and writing executable code. Pseudocode can also be useful for:
- demonstrating thinking that later can become comments in the final program
- describing how an algorithm should work
- explaining a computing process to less technical people
- generating code in collaboration with others.
Conventions for writing pseudocode
KEYWORDS are written in bold capitals and are often words taken directly from programming languages.
For example, IF, THEN and ELSE are all words that can be validly used in most languages. OUTPUT and COMPUTE are from the language COBOL and WRITE is from the language Pascal.
Keywords do not have to be valid programming language words as long as they clearly convey the intent of the line of pseudocode.
Statements that form part of a REPETITION LOOP are indented by the same amount to indicate that they form a logical grouping.
In a similar way, IF, THEN and ELSE statements are indented to clearly distinguish the alternative processing paths.
The end of REPETITION LOOPS and IF, THEN and ELSE statements are explicitly indicated by the use of ENDWHILE and ENDIF at the appropriate points. ATTENTION DIGITAL SOLUTIONS SYLLABUS WRITERS - THE USE OF THESE END KEYWORDS IS REDUNDANT, GIVEN THAT A BLOCK INDENT CONNOTATES IDENTICAL BEHAVIOUR!!!
Pseudocode should clearly indicate what is happening at each step, including formulas of calculations.
For example:
CALCULATE net
is not as clear as:
CALCULATE net = gross − tax.
Programmers prefer to use a more abbreviated version in which memory cells used to store the input are given program-like names.
For example:
INPUT num1
INPUT num2
is preferable to:
INPUT first number
INPUT second number
swap
INPUT height_A, height_B SET tallest = height_A, smallest = height_B IF height_B > height_A THEN SET temp = height_A SET height_A = height_B SET height_B = temp ENDIF
guessNumber
SET randomNumber = RANDOM_INT(1 to 10) FOR guessNumber = 1 to 3: INPUT userGuess IF userGuess > randomNumber THEN PRINT("lower") ELSE IF userGuess < randomNumber THEN PRINT("higher") ELSE PRINT(JOIN("winner on guess: "), guessNumber) ENDIF ENDIF ENDFOR
guessNumberAlternate
SET guessesRemaining = 3 SET unknownNumber = RANDOM_INT(1 to 10) WHILE guessesRemaining > 0 INPUT userGuess IF userGuess == unknownNumber: PRINT(JOIN("winner with guesses remaining: "), guessesRemaining) SET guessesRemaining = 0 ENDIF IF userGuess > randomNumber THEN PRINT("lower") ELSE PRINT("higher") ENDIF SET guessesRemaining = guessesRemaining - 1 ENDWHILE
paperRockScissors
SET userWinning = False, computerWinning = False SWITCH RANDOM_INT(1 to 3): CASE 1: computerPick = "Paper" CASE 2: computerPick = "Scissors" CASE 3: computerPick = "Rock" ENDSWITCH INPUT userPick SWITCH userPick: CASE "Paper": SWITCH computerPick: CASE "Scissors": SET computerWinning = True CASE "Rock": SET userWinning = True ENDSWITCH CASE "Rock": SWITCH computerPick: CASE "Paper": SET computerWinning = True CASE "Scissors": SET userWinning = True ENDSWITCH CASE "Scissors": SWITCH computerPick: CASE "Rock": SET computerWinning = True CASE "Paper": SET userWinning = True ENDSWITCH ENDSWITCH IF userWinning == True THEN PRINT("Winner") ELSE IF userWinning == True THEN PRINT("Loser") ELSE PRINT("Draw") ENDIF ENDIF
doubleDiceRollGame
FUNCTION rollTwoDice() SET dice1 = RANDOM_INT(1,6) SET dice2 = RANDOM_INT(1,6) IF dice1 > dice2 THEN SET jointNumber = JOIN(dice1, dice2) ELSE SET jointNumber = JOIN(dice2, dice1) END IF RETURN jointNumber END FUNCTION SET points = 10 WHILE points > 0: SET mynumber = CALL rollTwoDice() SET points = points - 1 INPUT challenge IF challenge == True THEN SET cpunumber = CALL rollTwoDice() IF mynumber > cpunumber THEN SET points = points + 5 ELSE IF cpunumber < mynumber THEN SET points = points - 5 END IF END IF END IF END WHILE