HiveBrain v1.2.0
Get Started
← Back to all entries
gotchaMinor

Difference between := and ← in pseudocode

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
anddifferencebetweenpseudocode

Problem

This is a snippet from some pseudocode for a sorting algorithm. In it, the symbol ← is used to denote assignment, for example for the variable done. However, in the while loop the statement done:= false is written. I would assume it is also an assignment statement but I suspect it means somethings else, or perhaps extra, since if not, the ← would have simple be used again.

Algorithm MyAlgorithm(A, n) 
    Input: Array of integer containing n elements 
    Output: Possibly modified Array A

done ← true 
j ← 0
while j ≤ n - 2 do
     if A[j] > A[j + 1] then
        swap(A[j], A[j + 1])
        done:= false
     j ← j + 1

Solution

The use of := in this context appears to be a slip-up from whomever it is who wrote that pseudocode. The initial impression given is that they have different meanings (I thought at first that := was perhaps "redefine", and that was "declare and define"). However, with the use of j ← j + 1 on the line following it, it seems clear that the := was a mistake.

In general, := and = are the most common assignment operators, seemingly in both pseudocode and real code. You may be interested in looking at notations of the assignment operator in various programming languages, which include :=, =, <-, , and many others.

Context

StackExchange Computer Science Q#8995, answer score: 8

Revisions (0)

No revisions yet.