Skip to content

Choices and Repeating

How a program decides what to do (if), how it checks things (comparisons, and, or, not), and how it does something again and again (repeat, with a number or until a check is true).


if

size = 50
if size is more than 40 {
  draw circle at 150, 100 having radius size, fill "gold"
}
otherwise {
  draw square at 100, 50 having size size, fill "skyblue"
}

if check { } otherwise { }

  • The lines in the first { } happen only when the check is true. Otherwise, the lines in otherwise { } happen instead.
  • otherwise { } can be left out. Then nothing happens when the check is false.
  • The check must be true or false. if size { } is a mistake when size is a number.
  • A true/false variable is a check on its own: if lit { }, if not lit { }.
  • To check something more after otherwise, put a new if inside its { }. (otherwise if on one line isn't part of VeeLang.)
  • In the picture, if is decided as the code is read, and only the lines it picks draw. In the animation (in a click, a forever, or after the first line that takes time), it's decided when that line runs.
  • if can't be written inside a shape's { } body or a recipe yet.
  • if checks something now. when waits for something to happen: when ball is clicked { }.

Learn it step by step: Remembering Things

Comparisons

clicks = 3
if clicks is at least 3 {
  draw text at 40, 80 having content "Well done!", size 40, color "green"
}
if clicks != 0 {
  draw circle at 200, 160 having radius 30, fill "gold"
}

Each comparison can be written in words or with symbols. Both mean the same.

Words Symbols True when (with clicks being a number)
clicks is 3 clicks == 3 clicks is exactly 3
clicks is not 3 clicks != 3 clicks is anything but 3
clicks is more than 3 clicks > 3 clicks is bigger than 3
clicks is less than 3 clicks < 3 clicks is smaller than 3
clicks is at least 3 clicks >= 3 clicks is 3 or bigger
clicks is at most 3 clicks <= 3 clicks is 3 or smaller
  • is, is not, == and != work for numbers, text and true/false: if mood is "happy", if lit is false.
  • more than, less than, at least and at most (and >, <, >=, <=) work only for numbers.
  • Two shapes can be compared with is and is not too. It asks whether they are the same shape, not whether they look alike: if row[i] is clicked_one, if ball is not other. See Bunches.
  • Both sides must be the same kind. mood is 3 is a mistake when mood holds text.
  • Maths happens before the comparison: x + 1 > 5 adds first.
  • A single = gives a value, it doesn't check. if speed = 3 is a mistake; write if speed is 3 or if speed == 3.
  • A comparison is true or false, so it can be kept in a variable: fast = speed > 3.

Learn it step by step: Remembering Things

and, or, not

repeat 7 times {
  if count > 2 and count < 6 {
    draw circle at 50 * count, 100 having radius 15, fill "gold"
  }
  if count is 1 or count is 7 {
    draw circle at 50 * count, 200 having radius 15, fill "tomato"
  }
}
Word True when
a and b both are true
a or b at least one is true
not a a is false
  • not goes first, then and, then or. So a or b and c means a or (b and c).
  • Brackets change the order: (a or b) and c, not (a and b).
  • not also goes after maths and comparisons: not x > 3 means not (x > 3).
  • Both sides of and and or must be true or false. speed > 1 and 5 is a mistake.
  • These are words only, written and or And (not && or AND).

Learn it step by step: Remembering Things

if ... is playing

draw square fan at 150, 100 having size 60, fill "skyblue"
animation spin {
  forever {
    turn fan by 90 in 1 second
  }
}
when fan is clicked {
  if spin is playing {
    stop spin
  }
  otherwise {
    play spin
  }
}

if name is playing { } otherwise { }

  • True while the animation called name is running.
  • The animation must be made with animation name { } somewhere in the program.
  • It's a check of its own, written exactly this way. It can't be joined with and or or, or turned around with not or is not playing. Use otherwise { } for "not playing".
  • It's always decided when the line runs, so it's part of the animation.

Learn it step by step: Remembering Things


repeat

repeat 5 times {
  draw circle at 60 * count, 100 having radius 25, fill "red"
}

repeat N times { }

  • Everything inside the { } happens N times. This is called a loop.
  • N can be any whole number from 0 to 1000, written as a number, a variable or maths: repeat n + 1 times. repeat 0 times does nothing.
  • N can't be below zero or have a dot: repeat 2.5 times is a mistake.
  • Loops can go inside loops, and inside a shape's { } body. Shapes a loop makes in a body are parts of that shape.

Learn it step by step: Repeating Things

The counter

repeat 4 times with row {
  repeat 6 times with col {
    draw circle at 55 * col, 55 * row having radius 3 + 3 * row, fill "teal"
  }
}

repeat N times with name { }

  • Inside the loop, the counter says which time round it is: 1 the first time, then 2, and so on up to N.
  • The counter is called count unless you give it a name with with.
  • The counter can't be changed: count = 3 is a mistake.
  • The counter can't have a name that's already used, by a variable, a shape or an outer loop's counter. So a loop inside a loop needs with and a name of its own.
  • The counter is gone after the loop.
repeat 4 times {
  repeat 6 times {
    draw circle at 55 * count, 55 * count
  }
}

Learn it step by step: Repeating Things

Names made in a loop

repeat 4 times with n {
  draw circle balloon at 80 * n, 100 having radius 30, fill "pink"
  draw line from balloon.center.x, 130 to balloon.center.x, 220 having color "gray"
}
  • A shape or variable made inside a loop lasts one time round. Each time round, balloon is the balloon from that time.
  • After the loop, the name is gone. Using it there is a mistake.
  • A variable made before the loop is the same one every time round, so a loop can add up: after total = 0 and repeat 5 times { total = total + count }, total is 15.

Learn it step by step: Repeating Things

repeat until and repeat while

A loop that goes on until a check is true, instead of a number of times.

number score = 0
repeat until score > 4 {
  draw circle at 20 + score * 30, 100 having radius 12, fill "gold"
  score = score + 1
}

repeat while is the same loop said the other way round: it goes on while the check is true. repeat while score < 5 and repeat until score > 4 are the same loop.

number size = 10
repeat while size < 60 {
  draw circle at 100, 100 having radius size, stroke "teal", fill "none"
  size = size + 10
}
  • The check is made before each time round, so a loop whose check already says stop doesn't run at all.
  • The check can be anything if takes: is, is not, >, <, and, or, not, touching, spin is playing.
  • Something in the loop has to change what the check looks at, or a line that takes time (wait, in 1 second) has to let it change, or the loop never ends. A loop that takes no time and never ends stops with a message after 10000 times round.
  • A loop with a line that takes time in it is part of the animation, like a counted loop. This one moves the ball until it is past the middle:
draw circle ball at 20, 100 having radius 15, fill "tomato"
repeat until ball.x > 100 {
  move ball by 30, 0 in 1 second
}
  • To wait for one of a bunch of shapes, the check can be any or every: repeat until any c in coins is touching ball { } goes on until some coin touches the ball, and then c is that coin on the lines after the loop. See any and every.
  • A loop that ends on a check has no counter. Use repeat 5 times when you want one.
  • until and while are words of the loop only straight after repeat. Everywhere else they can still be names.

How many times

Limit
One loop up to 1000 times
All loops in one program together up to 10000 times

Every time round of every loop counts towards the 10000, inner and outer loops alike. So repeat 100 times with repeat 100 times inside it is too many: 100 + 100 × 100 is 10100. The limits stop a typo like repeat 50000 times from freezing your computer.

Learn it step by step: Repeating Things

A loop that takes time

draw circle ball at 50, 100 having radius 20, fill "tomato"
repeat 3 times {
  move ball by 80, 0 in 1 second
  wait 0.5 seconds
}
  • A loop with a line that takes time inside it (in 1 second, wait, forever) is part of the animation. Each time round runs after the one before has finished.
  • A loop after the first line that takes time is part of the animation too, even if nothing in it takes time.
  • Variables changed in such a loop are read when each line runs. See Variables in the animation.

Learn it step by step: Making Things Move