This tutorial teaches constructs of Coral: An ultra-simple language for learning programming. A free web-based simulator executes Coral. See corallanguage.org for more.
This tutorial is not a full intro to programming. For that, see Fundamental Programming Concepts at zyBooks.com.
Outputting text — Hello World!
The following puts the quoted text (a string literal) to output:
Put "Hello world!" to output
Run it in the simulator: Press “Enter execution”, then “Run”. When done, “Exit execution”, change the text (like Hello to Hi), and run again.
Variables
A variable is a memory location that holds a value. Below, integer x
declares a variable named x, able to hold an integer value. Then x = 5
assigns x with the value 5.
integer x x = 5
Run it (Notice that x’s value changes to 5 in memory)
Each line is a statement. Only one statement per line is allowed. Each statement executes one at a time.
A variable’s name starts with a letter, then any letters, digits, or underscores. Valid: x, x2, numDogs, total_width. Invalid: 2x, num-cats, flag!
A variable may be declared to hold a floating-point value rather than an integer:
float userHeight userHeight = 5.7
Outputting variable values
An output statement can output a variable’s value. The last statement below puts the value of numPlayers (here 5) to output. Note: numPlayers is not in quotes.
integer numPlayers numPlayers = 5 Put "Number of players: " to output Put numPlayers to output
Above, all output will appear on one line. A “\n” in the string literal outputs a newline.
integer numPlayers numPlayers = 5 Put "Number of players:\n" to output Put numPlayers to output
“Put f to output with 2 decimal places”, for f = 1.035555, outputs 1.04.
Getting input
Below, the second statement gets the next value from input and assigns x with that value. (x must have been declared first.)
integer x x = Get next input
Assignment statements
An assignment statement assigns a variable with the value of an expression. Below, the third statement assigns x with 2 times x’s current value. If x was 4, the statement assigns x with 2 * 4, or 8.
integer x x = Get next input x = 2 * x Put x to output
An assignment statement’s right side can be an arithmetic expression involving +, -, *, /, and (). Ex: x = (3 * y) + (z / 2).
Example
The program below computes the total cost of a loan given the loan amount and interest.
float loanAmount float intRate float totalCost loanAmount = Get next input intRate = Get next input totalCost = loanAmount + (loanAmount * intRate) Put totalCost to output
Run it (feel free to change the input values)
Branches
An if-else construct implements branching. Below, if x < 0 is true, the first branch executes, outputting “Negative”. Else, the second branch executes, outputting “Non-negative”.
integer x x = Get next input if x < 0 Put "Negative" to output else Put "Non-negative" to output Put "\nDone" to output
While loops
A while loop construct executes its sub-statements while its condition is true. Below, while x (gotten from input) > 0, the loop outputs x’s square.
integer x integer xSquared x = Get next input while x > 0 xSquared = x * x Put xSquared to output Put "\n" to output x = Get next input
For loops
A for loop construct iterates a specified number of times. The below for loop iterates with i being 0, 1, 2, 3, then 4.
integer i for i = 0; i < 5; i = i + 1 Put i to output Put " " to output
Above, i = 0 executes only once, when the for loop first executes. Then condition i < 5 is checked, and if true the sub-statements execute. After, the update i = i + 1 executes, and i < 5 is checked again; the update and check repeat until the condition is false.
Comments
Comments help a human understand code. Each comment starts with //, and must be on its own line. A comment usually describes the code below the comment.
integer i // Count down from 10 to 0 for i = 10; i >= 0; i = i - 1 Put i to output // Add a space between numbers Put " " to output
Arrays
An array variable holds many values rather than just one value. The number of values (size) can be set in the declaration. The size can be read as myArray.size, useful in loops iterating through the array.
integer array(5) userNums integer i for i = 0; i < userNums.size; i = i + 1 userNums[i] = i * 2 for i = 0; i < userNums.size; i = i + 1 Put userNums[i] to output
Functions
A function is a statement group callable from code. A function definition may have parameter variables and a return variable. The functions returns the return variable’s last value.
If a user defines a function, the main code must then be in a main function (which is otherwise implicit).
Function FtInchToCm(float numFt, float numInch) returns float numCm numCm = ((numFt * 12) + numInch) * 2.54 Function Main() returns nothing float resultCm resultCm = FtInchToCm(5, 6) Put resultCm to output
A function without parameters has empty parentheses, like Main above. A function that doesn’t return a value uses “returns nothing”, like Main above.
A function call’s arguments can be expressions. Ex: z = FtInchToCm(x, y + 1)
.
A function call may appear in an expression. Ex: z = 1.0 + FtInchToCm(5, 6)
.
A Put statement’s item may be an expression, so a call may appear there, as below.
Function FtInchToCm(float numFt, float numInch) returns float numCm numCm = ((numFt * 12) + numInch) * 2.54 Function Main() returns nothing Put FtInchToCm(5, 6) to output
Built-in functions
Several built-in math functions exist, illustrated below.
float x float y x = Get next input y = Get next input Put SquareRoot(x) to output Put "\n" to output Put RaiseToPower(x, y) to output Put "\n" to output Put AbsoluteValue(x) to output
A built-in function generates random numbers. Each call specifies a range and returns a new random number within that range.
Put RandomNumber(0, 5) to output Put "\n" to output Put RandomNumber(0, 5) to output Put "\n" to output Put RandomNumber(0, 5) to output
To enable reproducible programs, a programmer can set a seed, as in SeedRandomNumbers(7), prior to any calls to RandomNumber(). Then, for any run of the program, the sequence of “random” numbers is always the same for that seed. Ex: If the above program began with SeedRandomNumbers(0), the program will always output 4, 0, 1. If no seed is provided, Coral by default uses the current time as the seed, yielding the expected “randomness” for each run of the program.