Skip to content

FlowControl

LabRicecat edited this page Nov 25, 2022 · 1 revision

There are some commands in MeowScript that make it possible execute code on some conditions or in a specific way.

if/else

if

if executes code if a given expression equals to 1.

if(something == something) {
	do something
}

The curly braces and the expression has t be on the same line!

else

Checks if the last if in the current scope failed (aka did not execute) and executes something if the check succeeds.

if(1 == 2) {
	print "I think the universe is wrong..."
}
else {
	print "nevermind..." # gets executed!
}

elif

A combination of else and if. Executes something if the last if fails and a given expression equals to 1. This can be chained as well.

if(1 + 1 == 3) { # fails
	print "math is wrong"
}
elif(1 * 1 == 1 + 1) { # fails
	print "math is still wrong"
}
elif(11 - 1 == 10) { # executes!
	print "yaay math is not wrong!"
}
else { # gets ignored
	print "huh??"
}

Loops

while

Execute it's body while the given expression equals to 1.

new counter 5
while(counter != 0) {
	print counter
	counter = counter - 1
}

This will output:

5
4
3
2
1

for

Iterates through a given value and sets a variable based on the iteration each time the loop runs.

for <var> in <value> {

}

for on strings

for i in "Hello" {
	print i
}

This loops over each character in the given string. Output:

H
e
l
l
o

for on numbers

for i in 4 {

}

This will run the body setting i starting with 0 and ending with 3. Output:

0
1
2
3

for on lists

for i in [1,"2",[3,4]] {
	print i
}

Loops over every element of the list beginning at the front.

1
2
[3,4]

continue

The command continue will end the current run of the loop and start a new one right away. Example:

for i in 10 {
	if(i % 2 == 0) {
		continue
	}
	print i
}

This will output all even numbers from 0 to 9:

0
2
4
6
8

break

Quits a loop, not allowing any further iterations or executions of the loop in general.

new inp ""
while(true) {
	inp = {input("> ")}
	if(inp == "q") {
		print "exit!"
		break
	}
}

Possible output:

> hi
> huh
> q
exit!

>>> Next Article

Clone this wiki locally