Example code have problem

Should I run code line by line?
Problems arise when I run multiple lines of the example.

2.4 Conditionals and randomness

// @@random is a class data member that is always available   
// to generate random values.

// Randomly print "heads" or "tails"
if @@random.coin_toss ["Heads!"] else ["Tails!"] 

// when is another way of expressing a conditional    
println("Tails!") when @@random.coin_toss 

// unless = "when not"
println("Heads!") unless @@random.coin_toss

This also generates a warning at compile time.

WARNING: The expression has no side effects so it does not seem useful as a statement on its own [not used as an argument or result (last expression) of a code block].
if @@random.coin_toss ["Heads!"] else ["Tails!"]
>>>>>>>>>>>>>>>>>>>>>> ^ <<<< Line: 1, column: 24

println("Tails!") when @@random.coin_toss  

Script code index: [23]

It works when I change it like this.

if @@random.coin_toss [print("Heads!")] else [print("Tails!")]

println("Tails!") when @@random.coin_toss 

println("Heads!") unless @@random.coin_toss

["Heads!"] to [print("Heads!")]
["Tails!")] to [print("Tails!")]

:sk: Tutorial Workbench code is same.

Many of the examples in the workbench tutorial - including these - are intended to be evaluated a line at a time.

If you changed the if example to use print() as you did, that allows them to be run with several additional lines.

Examples that include indented lines should generally be grouped together. Many other examples say something such as:

// Select and run the next four lines

Apologies if it isn’t always obvious what should be a single line and what should be evaluated as several lines.