Skip to main content

Flow Control

A new hatchling to DucklingScript is the flow control. Our ducks generally define this as commands you can use that help control how your code runs such as conditionals and loops.

Conditional Statements

These let you tell our ducks to run or continue running segments of your code only if they meet certain conditions. Most commonly you can ask our ducks to check for this using the IF, ELIF, AND ELSE commands.

Continuing the above, our brainy birds wanted to note that ELIF can only be used after IF and ELSE can only be used after either IF or ELIF as well as that any IF or ELIF(s) that are true will result in subsequent ELIF and ELSE commands within the same check being ignored.

DucklingScript

VAR eggs 5

REM if eggs is equal to 5
IF eggs == 5
REM this code is ran
STRINGLN All the eggs are here!

REM if eggs is not equal to 5
ELSE
STRINGLN Some eggs are missing!

Compiled

STRINGLN All the eggs are here!

For Loops

Much like our duck's predecessor Rubber Ducky 1.0, DucklingScript also allows you to REPEAT code blocks multiple times along with recognizing FOR loops that perform the same action.

info

Our white-coated web feet have again asked us to place a bit of info here to let you know that DucklingScript does not support checking if you have compiled code BEFORE the REPEAT command while using the original Rubber Ducky 1.0 syntax, which means our ducks won't notice any syntax issues caused by this.

warning

Keep in mind our ducks can only handle looping so many times; if you run it for too long they will get dizzy and eventually error. It is possible to circumvent this using nested loops, but using this method may have unintended consequences and is not recommended.

DucklingScript/Rubber Ducky 1.0

STRINGLN Hello Pond!
REPEAT 4

End Result

STRINGLN Hello Pond!
STRINGLN Hello Pond!
STRINGLN Hello Pond!
STRINGLN Hello Pond!

While this is a nice feature to have, in the past it only worked on one line of code. With our new quacky compilation, DucklingScript lets you use multiple lines using some fancy code block indentation!

DucklingScript

REPEAT 3
STRING This duck quacks...
STRING and this one waddles!

Compiled

STRING This duck quacks...
STRING and this one waddles!
STRING This duck quacks...
STRING and this one waddles!
STRING This duck quacks...
STRING and this one waddles!

Along with this our feathered fowl also can count the amount of iterations, that is repeats, that have occurred in a variable! However, our downy doctors would like you to keep in mind that the first iteration is considered to be iteration 0. You can accomplish this effect with the <variable_name>, <iteration_count> syntax as shown:

DucklingScript

REPEAT i,3
$STRING "The ducks have counted "+i+" iterations!"

Compiled

STRING The ducks have counted 0 iterations!
STRING The ducks have counted 1 iterations!
STRING The ducks have counted 2 iterations!

While Loops

Unlike the prior discussed loops, a while loop will continue to iterate while its conditional returns TRUE.

warning

Like other loops, iterating too many times will make the ducks dizzy and cause an error. So try to make sure your while loops eventually resolve.

DucklingScript

VAR eggs 0
WHILE eggs!=5
$STRING "There are " + eggs + " eggs."
VAR eggs eggs+1

Compiled

STRING There are 0 eggs.
STRING There are 1 eggs.
STRING There are 2 eggs.
STRING There are 3 eggs.
STRING There are 4 eggs.


Like the previous loop, our ducks are also able to count the number of iterations in a while loop and store it to a variable using this syntax:

DucklingScript

VAR duck_name "Dav"
WHILE count,duck_name!="Daveee"
VAR duck_name duck_name+"e"
$STRING duck_name + " has spun " + count + " times!"
REM Don't worry, Dave likes spinning.

Compiled

STRING Dave has spun 0 times!
STRING Davee has spun 1 times!
STRING Daveee has spun 2 times!

Loop Controls

BREAKLOOP

To help keep our ducks from getting too dizzy, DucklingScript has added the BREAKLOOP/BREAK_LOOP command which lets you break out of a loop, this can be helpful for situations where you may want to stop looping early.

DucklingScript

REPEAT i,10
$STRING "Dave spun " + i + "time(s)."
IF i==2
BREAKLOOP

Compiled

STRING Dave spun 0 time(s).
STRING Dave spun 1 time(s).
STRING Dave spun 2 time(s).

As you can see here, our duck Dave only spun 3 times instead of the given 10 because we BREAKLOOP when i is 2.

CONTINUELOOP

Along with this, we've also taught our ducks the CONTINUELOOP/CONTINUE_LOOP/CONTINUE command, allowing them to continue, that is, skip, through a loop like so:

DucklingScript

REPEAT i,5
IF i==3
CONTINUELOOP
$STRING "Suzzie likes the number " + i + "!"

Compiled

STRING Suzzie likes the number 0!
STRING Suzzie likes the number 1!
STRING Suzzie likes the number 2!
STRING Suzzie likes the number 4!

Looking here, you can see, that we didn't say our duck Suzzie liked the number 3 because we CONTINUE the loop at that number, ignoring the code after it.