IF, WHILE AND FOR

A program that only runs from top to bottom can only ever do one thing. The statements let it choose and repeat: satellite.statement.if chooses between paths, satellite.statement.while repeats while something is true, and satellite.statement.for counts its way through a loop. satellite.statement.else goes with an if. Those four are the whole family today.

Each one takes a condition in brackets, something that is true or false, and a body between { and }. A comparison such as n > 0 or name == "Ada" is the usual condition. See OPERATORS: LOGIC AND BITS for all of them.

if, else, and else if

satellite.statement.if runs its body only when its condition is true. A satellite.statement.else straight after it runs when the condition was false. Put another if straight after an else to test one more condition, as many times as you need.

weather.satlSATELLITE 004 Download
Choosing between three paths
satellite.include(satellite)

satellite.capsule satellite.main()
{
    satellite.variable.number temperature = 18
    satellite.statement.if (temperature > 25)
    {
        satellite.console.display("hot")
    }
    satellite.statement.else satellite.statement.if (temperature > 15)
    {
        satellite.console.display("mild")
    }
    satellite.statement.else
    {
        satellite.console.display("cold")
    }

    satellite.variable.bool raining = satellite.bool.false
    satellite.statement.if (!raining)
    {
        satellite.console.display("no umbrella today")
    }
    satellite.return(satellite)
}
OUTPUT
mild
no umbrella today

The first test that is true wins, and the rest are skipped. raining is a satellite.variable.bool, which holds satellite.bool.true or satellite.bool.false, and ! turns true into false and false into true.

A condition has to be true or false. satellite.statement.if (lives), where lives is a number, is refused when it runs: “satellite.statement.if was given a number and needs a true or false”. Write what you mean instead, such as lives > 0.

while

satellite.statement.while runs its body again and again, for as long as its condition is true. The condition is checked before every turn, so if it is false at the start the body never runs at all. Something in the body has to change, or the loop never ends.

doubling.satlSATELLITE 004 Download
Repeat while a condition holds
satellite.include(satellite)

satellite.capsule satellite.main()
{
    satellite.variable.number grains = 1
    satellite.variable.number square = 1
    satellite.statement.while (grains < 1000)
    {
        satellite.console.display("square " + square.string + ": " + grains.string)
        grains = grains * 2
        square = square + 1
    }
    satellite.return(satellite)
}
OUTPUT
square 1: 1
square 2: 2
square 3: 4
square 4: 8
square 5: 16
square 6: 32
square 7: 64
square 8: 128
square 9: 256
square 10: 512

for

satellite.statement.for puts the three parts of a counting loop in one place, divided by semicolons:

  1. a declaration, which makes the counter: satellite.variable.number i = 0
  2. a condition, checked before every turn: i < 3
  3. a step, which moves the counter after every turn

The step is written without an =, because the counter’s own name is already in front of it: i + 1 means “i becomes i + 1”. Any of + - * / % ^ works, and i++ and i-- are short for adding and taking away one. The step may also be left empty, when the body moves the counter itself.

counting.satlSATELLITE 004 Download
Three ways to step a for loop
satellite.include(satellite)

satellite.capsule satellite.main()
{
    satellite.statement.for (satellite.variable.number i = 0; i < 3; i++)
    {
        satellite.console.display(i)
    }
    satellite.statement.for (satellite.variable.number i = 10; i > 0; i - 4)
    {
        satellite.console.display(i)
    }
    satellite.statement.for (satellite.variable.number i = 1; i <= 1000; i * 10)
    {
        satellite.console.display(i)
    }
    satellite.return(satellite)
}
OUTPUT
0
1
2
10
6
2
1
10
100
1000

The counter lives only as long as its loop. After the loop it is gone, which is why the same name i can be declared again by the next loop. A body shares every other variable with the lines around it, so a loop can add to a total declared before it.

If you are used to other languages you may write the step as i = i + 1. satl catches that before the program runs, and says how the step is written:

old_habit.satlSATELLITE 004 Download
A for's step has no = in it
satellite.include(satellite)

satellite.capsule satellite.main()
{
    satellite.statement.for (satellite.variable.number i = 0; i < 3; i = i + 1)
    {
        satellite.console.display(i)
    }
    satellite.return(satellite)
}
OUTPUT
--------------------------------------------------------------------------------
SATELLITE CRITICAL ERROR REPORT
--------------------------------------------------------------------------------
S110: LINE_NOT_UNDERSTOOD
satl(check): in satellite.main, satellite.statement.for's step does not move i
-- a for's step is i and one of + - * / % ^ ** with a space on both sides, or
i++ or i--

directory: old_habit.satl:5
syntax: satellite.statement.for (satellite.variable.number i = 0; i < 3; i = i + 1)
        /\

satellite has no scenario for this line -- it is spelled in a shape the language
does not have a meaning for.
machine code 13 satl_line_not_understood -- satl exits with this.

--------------------------------------------------------------------------------

Leaving a loop early

There is no break or continue in satellite today, and satl refuses both. To leave a loop early, put the loop in a capsule and use satellite.return, which ends the capsule from wherever it stands, even from deep inside a loop. See CAPSULES: SATELLITE FUNCTIONS.

Conditions cannot yet be joined with “and” or “or” (&& and || are refused today). For “and”, put one if inside another. For “or”, test each condition in turn with else if.

Where next

The FULL REFERENCE has every statement’s rules. Comparisons and ! are in OPERATORS: LOGIC AND BITS, and loops over the items of a list are in LISTS.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *