Control Flow
Add intelligence to your application with conditions, loops, and error handling.
1. Conditional (If/Else)
Execute commands only if a specific condition is met.
Expressions
Support for ==, !=, >, <, AND, OR.
App.xml
<If Condition="{age} >= 18 AND {hasId} == 'true'">
<Alert Message="Access Granted!" Icon="Success"/>
<Else>
<Alert Message="Access Denied" Icon="Error"/>
</Else>
</If>
Operators: Use
AND / && for multiple conditions. Use OR / || for alternatives.
2. Switch Case
A cleaner way to handle multiple conditions based on a single value.
<Switch Expression="{role}">
<Case Value="admin">
<Set Target="lblRole" Property="Text" Value="Administrator"/>
<Show Control="adminPanel"/>
</Case>
<Case Value="user">
<Set Target="lblRole" Property="Text" Value="Standard User"/>
<Hide Control="adminPanel"/>
</Case>
<Default>
<Set Target="lblRole" Property="Text" Value="Guest"/>
</Default>
</Switch>
3. Loops (ForEach)
Iterate through a collection of items or a fixed number of times. Perfect for populating lists.
Collection Loop
Loop through a comma-separated string or list variable.
<!-- List of items -->
<Var Name="fruits" Value="Apple,Banana,Orange" Type="list"/>
<ForEach Collection="{fruits}" Var="fruit">
<AddItem Container="fruitList" Text="{fruit}"/>
</ForEach>
Count Loop
Repeat actions a specific number of times.
<ForEach Count="5" IndexVar="i">
<AddItem Container="starRating" Text="⭐ Star {i}"/>
</ForEach>
4. While Loop
Repeat actions as long as a condition is true. Be careful to avoid infinite loops!
<Var Name="power" Value="1" Type="int"/>
<While Condition="{power} < 100">
<Multiply Var="power" Value="2"/>
<Log Message="Power is now: {power}"/>
</While>
5. Error Handling
Handle potential errors gracefully using Try/Catch blocks. Useful for file operations or network requests.
<TryCatch ErrorVar="errMsg">
<Try>
<!-- Risky operation -->
<ReadFile Path="config.json" ToState="config"/>
<Toast Message="Config loaded!"/>
</Try>
<Catch>
<!-- Handle error -->
<Alert Message="Failed to load config: {errMsg}" Icon="Error"/>
<Set Var="config" Value="{}"/>
</Catch>
<Finally>
<!-- Always runs -->
<Hide Control="loadingSpinner"/>
</Finally>
</TryCatch>