Control Flow
If Statement
Basic conditional:
gpc
if (get_val(BUTTON_10) > 50) {
set_val(BUTTON_10, 100);
}With else:
gpc
if (get_val(BUTTON_9)) {
set_val(BUTTON_15, 100);
} else {
set_val(BUTTON_15, 0);
}Else-if chain:
gpc
int value = get_val(BUTTON_10);
if (value > 80) {
// Heavy press
} else if (value > 40) {
// Medium press
} else if (value > 0) {
// Light press
} else {
// Not pressed
}While Loop
Repeats while condition is true:
gpc
int i = 0;
while (i < 10) {
set_val(i, 0);
i++;
}Warning: Avoid infinite loops in main block - use combos with wait() for timing.
Do-While Loop
A do-while loop runs its body once before checking the condition:
gpc
int attempts = 0;
do {
attempts++;
} while (attempts < 3);Keep loops short; wait() belongs in a combo, not a loop in main.
For Loop
Standard C-style for loop:
gpc
for (int i = 0; i < 10; i++) {
set_val(i, 0);
}Iterating arrays:
gpc
int values[5];
for (int i = 0; i < 5; i++) {
values[i] = i * 10;
}Counting down:
gpc
for (int i = 9; i >= 0; i--) {
set_val(i, 100 - (i * 10));
}Switch Statement
Multi-way branching:
gpc
int mode = 2;
switch (mode) {
case 0:
// Mode 0 logic
break;
case 1:
// Mode 1 logic
break;
case 2:
// Mode 2 logic
break;
default:
// Default logic
break;
}Important: Use break to prevent fall-through.
Break Statement
Exits the current loop or switch:
gpc
for (int i = 0; i < 100; i++) {
if (get_val(i) > 50) {
break; // Exit loop early
}
}Continue Statement
Skips to next loop iteration:
gpc
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue; // Skip i=5
}
set_val(i, 100);
}Return Statement
Exits a function with optional value:
gpc
function int calculate_value(int base, int multiplier) {
if (multiplier == 0) {
return 0; // Early exit
}
return base * multiplier;
}Practical Examples
State Machine
gpc
int state = 0;
main {
switch (state) {
case 0: // Idle
if (event_active(BUTTON_15)) {
state = 1;
}
break;
case 1: // Active
combo_run(MyCombo);
if (event_active(BUTTON_14)) {
combo_stop(MyCombo);
state = 0;
}
break;
}
}
combo MyCombo {
set_val(BUTTON_15, 100);
wait(250);
set_val(BUTTON_15, 0);
wait(100);
}Button Combination Detection
gpc
main {
// Detect BUTTON_11 + BUTTON_12 held together
if (get_val(BUTTON_11) && get_val(BUTTON_12)) {
// Both pressed
}
// Detect either button
if (get_val(BUTTON_15) || get_val(BUTTON_14)) {
// At least one pressed
}
}Timed Hold Detection
gpc
main {
// Check if button held for 500ms
if (check_active(BUTTON_1, 500)) {
// Long press detected
}
}