Skip to content
Rajesh Gautam edited this page Jun 12, 2026 · 1 revision

Loops

While Loop

Repeats while a condition is true:

var{x, 0}\
while{var{x} < 5}-
    pcType{var{x}}\
    var{x, var{x} + 1}\
--\

Always update the condition variable inside the loop, or it will run forever.

For Loop

Iterates a variable from start up to (not including) end:

for{i, 0, 5}-
    pcType{var{i}}\
--\

This prints 0 1 2 3 4.

Access the loop variable with var{i} inside the body.

Examples

Count to 10:

for{i, 1, 11}-
    pcType{var{i}}\
--\

Sum numbers 1–5:

var{total, 0}\
for{i, 1, 6}-
    var{total, var{total} + var{i}}\
--\
pcType{var{total}}\

Repeat until input:

var{done, 0}\
while{var{done} == 0}-
    pcAsk{"Type 1 to stop: "}\
    var{done, input{ans}}\
--\

Difference Between while and for

while for
Use when You don't know how many iterations You know start and end
Condition Any boolean expression Number range
Risk Can loop forever Always terminates

Clone this wiki locally