For these exercises, you will usually output
and answer,
but you may return
for some
method adder(FIRST, SECOND)
THEANSWER = FIRST + SECOND
output THEANSWER
end method
Symbol | Meaning |
---|---|
= |
exactly equals |
!= or <> |
does not equal |
+ |
addition or string concatenation |
- |
subtraction |
* |
multiplication |
/ |
division (float) |
div |
division (integer) |
mod |
remainder (modulo) |
> |
greater than |
>= |
greater than or equal |
< |
less than |
<= |
less than or equal |
Symbol |
---|
NOT |
AND |
OR |
then
is optional. All if
statements must be on a single line. You can also use end
instead of end if
if NUM < 42 then
// something
end if
if NUM < 42 then
// something
else
// something else
end if
if NUM < 42 then
// something
else if NUM > 55
// something else
else
// more code
end if
NUM = 0
loop while NUM < 10
output NUM
NUM = NUM + 2
end loop
// 0
// 2
// 4
// 6
// 8
loop NUM from 0 to 4
output (5 - NUM)
end loop
output "Blastoff!"
// 5
// 4
// 3
// 2
// 1
// Blastoff!
Note that these methods are not defined by the IB. On test, you will need to find an alternate pathway to these same results.
Method | Behavior |
STR.Length() |
returns the length of the string |
STR.SubStr(S,L) |
Returns the substring that starts at index S and goes for up to L characters. If the string ends, it will return to the end of the string. |
STR = "Hello"
STR.Length() //5
STR.SubStr(0,3) //"Hel"
STR.SubStr(2,2) //"ll"
STR.SubStr(4, 7) //"o"
Note that these methods are not defined by the IB. On test, you will need to find an alternate pathway to these same results.
Method | Behavior |
A = new Array(SIZE) |
Creates a new array with SIZE null values and saves it in variable A |
ARR.Length() |
returns the length of the array |
ARR.Slice(S,L) |
Returns the array that starts at index S and contains the next L elements. If the array ends, it will return to the end of the string. |
NUMS = [42, 13, 78]
output NUMS[0] // 42
output NUMS[2] // 78
NUMS[1] = 11 // NUMS is now [42, 11, 78]
NUMS.Length() // 3
NUMS.Slice(0,2) // [42, 11]
Looping through an array
loop I from 0 to NUMS.Length() - 1
//do stuff with NUMS[I]
end loop
A collection is a list of like objects. Assume we have one named NAMES with a list of strings. These methods ARE defined by the IB and can and should be used in tests!
Method | Behavior |
C = new Collection() |
Creates a new collection and saves it to variable C |
NAMES.hasNext() |
returns true there are elements we have not seen yet |
NAMES.getNext() |
Gets the next element in the collection (and moves forward in the line) |
NAMES.resetNext() |
Resests back to the beginning of the collection for another loop |
NAMES.addItem() |
adds an item to the END of the collection |
NAMES.isEmpty()
|
returns true if the collection does not contain any elements |
Looping through a collection
NAMES.resetNext()
loop while NAMES.hasNext()
ELEMENT = NAMES.getNext()
// do stuff with ELEMENT
end loop