Command
Every program is just a sequence of commands given to a computer.
A command is a specific instruction or directive that can be carried out by a computer. It is the fundamental syntactic unit of a program. No matter how complex an algorithm may seem, it can always be broken down into individual commands.
There are two categories of commands: simple and compound. A simple command is complete by itself and cannot contain other commands. A compound command may contain "nested" commands, which themselves can be simple or compound.
Assignment
Assignment is a simple command that sets or re-sets the value stored at a memory location given by a variable name.
x ← 2
You may also see assignment denoted as :=, which isn't as widely-used, or as =, which, within a mathematical context, can be confused with equality.
Call
Calling a predefined method is as simple as writing out the method's name and supplying parameters. Since this is significantly easier than writing methods on your own, before calling a method, make sure that you are allowed to call it.
y ← gcd(8, 12)
Return
return is a simple command that forces execution to leave the current method and pass back a return value to the code that called the method, then resume from there.
return x
Conditional
Conditionals are a family of compound commands that handle decisions by diverting code. Different actions are performed based on whether a condition is true or false.
An if command executes the commands indented within it if its condition is true.
if x < 7
return x
An if-else command executes the commands indented in its if branch if its condition is true, otherwise, it executes the commands indented in its else branch.
if x < 7
x ← x + 1
return x
else
return y
You might be familiar with else if branches too, which are the same as nesting an if branch inside of an else branch.
Loop
Loops are a family of compound commands that serve to repeatedly execute code.
A for loop executes the commands indented within it a fixed number of times. This is done by initializing an index with a start value and incrementing it by \(1\) with each iteration. During the last iteration, the index will have its end value. The following for loop will have exactly \(5\) iterations, the number of integers from \(1\) to \(5\), inclusive.
sum ← 0
for i = 1 to 5
sum ← sum + i
return sum
This algorithm calculates the sum of the first \(5\) positive integers. In summation notation, this is equivalent to \(\sum\limits_{i=1}^{5}i.\)
A for each loop executes the commands indented within it a fixed number of times by traversing through each item in a collection. This type of loop is easier to read.
S ← { 1, 2, 3, 4, 5 }
sum ← 0
for each element s in S
sum ← sum + s
return sum
A while loop continually executes the commands indented within it until its guard condition becomes false. This can lead to an infinite loop if the guard condition never changes from true to false. The following loop stops when i = 6, which is when the guard condition i < 6 becomes false.
i ← 1
sum ← 0
while i < 6
sum ← sum + i
i ← i + 1
return sum