Learn
Lesson 2

Variables

Name values with variables, give them types, change them, and keep the ones that shouldn't change fixed.

A variable is a name for a value. In OSL you write the type first, then the name, then the value:

main.osl
string name = "Ada"
int age = 36
number height = 1.65
boolean curious = true

log name age height curious
Click the code to edit it

Those four are the types you'll use most:

TypeHoldsExamples
stringtext"Ada", 'hi', ""
intwhole numbers42, -7, 1_000_000
numbernumbers with a fractional part1.65, 0.5, 3.0
boolean (or bool)true or falsetrue

Let the compiler work out the type#

When the value makes the type obvious, write auto and the compiler fills it in:

main.osl
auto city = "Lisbon"      // a string
auto population = 545000  // an int
log city ++ " has " ++ population ++ " people"
Click the code to edit it

auto still gives the variable one fixed type. It just saves you writing it. OSL code tends to spell types out where they help the reader, such as at the top of a file and on functions, and use auto for short-lived values in the middle.

++ joins values into text, turning numbers into strings on the way. You'll see it everywhere.

Changing a variable#

Assign a new value with =. There are shortcuts for the common changes:

main.osl
int score = 10
score = 15
score += 5     // score = score + 5
score -= 2
score *= 2
score++        // add one
log score

string greeting = "Hello"
greeting ++= ", world"   // greeting = greeting ++ ", world"
log greeting
Click the code to edit it

A variable keeps the type it was declared with. Putting a value of another type in it is an error the compiler catches before the program runs:

main.osl
int score = 10
score = "ten"
Click the code to edit it

Declare before you use#

Every variable is declared once, with its type or auto. Assigning to a name that hasn't been declared is an error, so a typo can't quietly create a new variable:

main.osl
int count = 1
cuont = count + 1
Click the code to edit it

The message names the mistake and shows the declaration you probably meant. If a value is computed but not needed, assign it to _ to throw it away on purpose.

Quick check

Which of these lines doesn't compile?

Values that never change#

Use const<T> for a value that must stay as it is. The compiler rejects any later assignment:

main.osl
const<int> maxPlayers = 4
const<string> title = "Space Race"
log title ++ " for up to " ++ maxPlayers
Click the code to edit it
main.osl
const<int> maxPlayers = 4
maxPlayers = 5
Click the code to edit it

const also protects arrays and objects from being changed through that name, which you'll meet in the lessons on arrays and types.

Names#

Names start with a letter or an underscore and continue with letters, digits and underscores. OSL code uses camelCase for variables and functions and PascalCase for types. Letters from any language are fine: string año = "2026" works.

A few words belong to the language and can't be names, such as if, for, return, class and import. The compiler tells you when you've picked one, and suggests another name.

Your turn

A shopping total

Three apples cost 40 each and a loaf costs 150, so the total should be 270. This program prints 193. Fix the line that works out total.

main.osl
Ctrl Enter runs
Expected output
270
0 of 24 lessons done