PriyoScriptPriyoScript
Basics

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

ConstructSyntaxUse case
forprakritiCount (init; condition; update) { ... }Counted iteration
for-eachprakritiCount (item priyoInside arr) { ... }Array traversal
whileprakritiAsLongAs (condition) { ... }Condition-driven repetition
switchprakritiChoose (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
3

Example 2: For-each over array

monalisa {
  priyoKeep names = ["mona", "piue"]
  prakritiCount (name priyoInside names) {
    priyoTell(name)
  }
}

Output:

mona
piue

6. 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
2

Example 2: While with break condition

monalisa {
  priyoKeep i = 1
  prakritiAsLongAs (priyoTrue) {
    priyoTell(i)
    prakritiIf (i == 3) {
      prakritiStop
    }
    i = i + 1
  }
}

Output:

1
2
3

7. 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 access

Example 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
General

Practice Questions

Concept Check

  1. What is the practical difference between prakritiCount and prakritiAsLongAs loops? 2. When is prakritiCount better than indexed looping? 3. Why is prakritiChoose often clearer than many chained prakritiIf checks for fixed values?

Last updated on

On this page