Loops
Overview
Loops are flow control statements that can be used to repeat instructions until a specific condition is reached.
WHILE
A block of code can be executed repeatedly a specified number of times (called iterations) using a WHILE
statement. The code within the WHILE
statement will continue to execute for as long as the condition of the WHILE
statement is TRUE
.
A WHILE
statement is similar to an IF
statement, however it behaves differently when at the statements end. Whereas at the end of an IF
statement the payload will continue, when the end of a WHILE
statement is reached the payload execution will jump back to the beginning of the WHILE statement and reevaluate the condition. One way to interpret a WHILE
statement is to read it as "IF this condition is true, THEN do that until it isn't true anymore" — hence it being called a while loop.
Syntax
The WHILE
statement consists of four parts
The
WHILE
keyword.The condition, or expression that evaluates to
TRUE
orFALSE
.One or more newlines containing the block of code to execute.
The
END_WHILE
keyword.
Example
Result
The variable
$FOO
is set to 42.The
WHILE
loop begins, evaluating the condition "is $FOO greater than 0".Every time the condition is
TRUE
, the block of code betweenWHILE
andEND_WHILE
will run.The LED will blink green: half a second on, half a second off.
The variable
$FOO
will decrement by one.
Once
$FOO
reaches zero, theWHILE
condition will no longer evaluate toTRUE
. The payload will continue execution after theEND_WHILE
statement, where the LED will light red.If the button is pressed at any time during the payload execution, the
WHILE
loop will end and the USB Rubber Ducky will enterATTACKMODE STORAGE
since that is the default behavior when noBUTTON_DEF
has been initiated.
Example
Result
The variable
$FOO
is set to 5.The code block within the
WHILE
loop will be repeated until the expression evaluates toFALSE
.For each run of the code block, the message "
Press the button...
" is typed. The payload then waits until it detects the button is pressed, at which point the variable$FOO
is decremented.
Infinite Loop
The syntax of WHILE
states that in nearly all cases the expression should be surrounded by parenthesis ( )
. The exception is when initiating an infinite loop. The condition of the expression TRUE
will always evaluate to TRUE
. While it is not necessary to omit the parenthesis, it is technically more efficient. This is because it directly references TRUE
, reducing the number of instructions and removing the step of first reducing the order of precedence.
This is loop that will execute endlessly, until intervention occurs. This may either come in the form of a button press, or simply by unplugging the USB Rubber Ducky.
Example
Result
Because a button definition has been initiated with
BUTTON_DEF
, the default behavior will no longer apply when the button is pressed.The LED will blink green: half a second on, half a second off.
Pressing the button will stop the currently infinite loop of blinking the LED green and execute the button definition, thus blinking the LED red.
Last updated