Control Flow
Looping and selection with for, while, and switch in PriyoScript.
Theory
Loop and switch constructs control repeated execution and multi-branch value selection. They solve iterative tasks (counting, traversing arrays, accumulation) and value-based dispatch logic (menu choice, status mapping, routing labels). In PriyoScript, prakritiCount and prakritiAsLongAs repeat blocks under explicit conditions, and prakritiChoose selects a branch by matching a value against cases.
Control-flow strategy
Use the simplest construct that matches your intent: for for counted loops, while for
open-ended loops, switch for fixed value choices.
Control Flow Syntax Table
| Construct | Syntax | Use case |
|---|---|---|
for | prakritiCount (init; condition; update) { ... } | Counted iteration |
for-each | prakritiCount (item priyoInside arr) { ... } | Array traversal |
while | prakritiAsLongAs (condition) { ... } | Condition-driven repetition |
switch | prakritiChoose (value) { prakritiCase (...) { ... } ... } | Value-based branching |
5. prakritiCount loop
prakritiCount loop runs a block with explicit start, condition, and update.
Example 1: Counted loop
monalisa {
prakritiCount (priyoChange i = 1; i <= 3; i = i + 1) {
priyoTell(i)
}
}Output:
1
2
3Example 2: For-each over array
monalisa {
priyoKeep names = ["mona", "piue"]
prakritiCount (name priyoInside names) {
priyoTell(name)
}
}Output:
mona
piue6. prakritiAsLongAs loop
prakritiAsLongAs loop executes as long as condition remains true.
Example 1: Basic while
monalisa {
priyoKeep x = 0
prakritiAsLongAs (x < 3) {
priyoTell(x)
x = x + 1
}
}Output:
0
1
2Example 2: While with break condition
monalisa {
priyoKeep i = 1
prakritiAsLongAs (priyoTrue) {
priyoTell(i)
prakritiIf (i == 3) {
prakritiStop
}
i = i + 1
}
}Output:
1
2
37. prakritiChoose case
prakritiChoose compares one value against multiple candidate cases.
Example 1: Basic switch/default
monalisa {
priyoKeep role = "admin"
prakritiChoose (role) {
prakritiCase ("admin") {
priyoTell("Full access")
}
prakritiCase ("user") {
priyoTell("Limited access")
}
prakritiOtherwise {
priyoTell("Guest access")
}
}
}Output:
Full accessExample 2: Switch in loop
monalisa {
priyoKeep tags = ["news", "music", "other"]
prakritiCount (tag priyoInside tags) {
prakritiChoose (tag) {
prakritiCase ("music") { priyoTell("Audio") }
prakritiCase ("news") { priyoTell("Text") }
prakritiOtherwise { priyoTell("General") }
}
}
}Output:
Text
Audio
GeneralPractice Questions
Concept Check
- What is the practical difference between
prakritiCountandprakritiAsLongAsloops? 2. When isprakritiCountbetter than indexed looping? 3. Why isprakritiChooseoften clearer than many chainedprakritiIfchecks for fixed values?
Related Docs
Last updated on