Control Structures in App Inventor 2

To create apps that do more than just display information, you need to control the flow of execution—the order in which instructions (blocks) are run. Control Structures allow you to make decisions, repeat actions, and respond to events, making your apps dynamic and interactive.

Sequential Execution

The most basic flow is sequential execution. Inside event handlers (when ... do) or procedures, blocks are typically executed one after another, from top to bottom, in the order they are connected, unless a control structure alters this flow.

Conditional Execution (Selection)

These structures allow your app to make decisions and execute different code blocks based on whether certain conditions are true or false. They are primarily found in the Control category.

1. if then

  • Description: Executes the blocks connected in the then socket only if the specified condition (test) evaluates to true. If the condition is false, these blocks are skipped.
  • Block: if [test] then [do]
  • Example: if Score > 100 then [Show "High Score!" message]

2. if then else

  • Description: Executes the then blocks if the condition (test) is true, otherwise executes the else blocks. Exactly one of the two branches (then or else) will be executed.
  • Block: if [test] then [do] else [do]
  • Example: if IsUserLoggedIn = true then [Show Welcome Message] else [Show Login Button]

3. if then elseif else (Multiple Conditions)

  • Description: Evaluates multiple conditions in order. It executes the blocks corresponding to the first condition that evaluates to true. If none of the if or elseif conditions are true, the else blocks are executed (if an else part is included). You can add elseif and else parts by clicking the blue gear icon (mutator) on the if block.
  • Block: (Configured using the mutator) if [test1] then [do1] elseif [test2] then [do2] else [do3]
  • Example: if Temperature > 30 then [Show "It's hot!"] elseif Temperature < 10 then [Show "It's cold!"] else [Show "It's comfortable."]

Repetitive Execution (Iteration / Loops)

Loops are used to execute the same or similar blocks of code multiple times. They are also found mainly in the Control category.

1. for each item in list

  • Description: Repeats a set of blocks for every element in a specified list. In each iteration, the current element from the list is assigned to the loop variable (e.g., item).
  • Block: for each [variable] in list [list] do [do]
  • Example: for each name in userList do [Display name on a label]

2. for each key with value in dictionary

  • Description: Repeats a set of blocks for every key-value pair in a specified dictionary. In each iteration, the current key and its corresponding value are assigned to the specified loop variables (e.g., key, value).
  • Block: for each [keyVariable], [valueVariable] in dictionary [dictionary] do [do]
  • Example: for each key, value in settingsDictionary do [Save key and value to TinyDB]

3. for each number from to by (Counted Loop)

  • Description: Repeats a set of blocks, incrementing a counter variable (number) from a starting value (from) up to an ending value (to), using a specified step (by). You can access the current value of the counter variable within the loop.
  • Block: for each [variable] from [start] to [end] by [step] do [do]
  • Example: for each i from 1 to 5 by 1 do [Create Button component] (Creates 5 buttons)

4. while test do

  • Description: Repeats a set of blocks in the do socket as long as the specified condition (test) remains true. The condition is checked before each iteration. If the condition is initially false, the loop body will never execute. Be careful to ensure the condition eventually becomes false to avoid infinite loops.
  • Block: while [test] do [do]
  • Example: while IsGameRunning = true do [Move Sprite, Check Collisions]

5. break (Exiting Loops)

  • Description: Can be used inside any of the loop blocks above (added via the blue gear icon/mutator). When the break block is executed, it immediately terminates the innermost loop it is currently inside, and control transfers to the block immediately following the loop.
  • Block: (Added within the loop block’s mutator) break
  • Example: Searching for an item in a list and stopping the loop as soon as it’s found.

Event Handling

App Inventor programming is event-driven. This means that the execution of your code is often triggered by “events," such as user actions (clicking a button, swiping the screen) or system occurrences (screen initializing, sensor receiving data, timer firing).

  • Block: when [Component].[Event] do [do] (e.g., when Button1.Click do, when Screen1.Initialize do, when Clock1.Timer do)
  • Description: When a specific event occurs, the blocks inside the corresponding when ... do event handler are executed. Much of your app’s logic will reside within these event handlers, acting as starting points for sequences of actions.

Procedures (Code Reusability)

When you need to perform the same sequence of actions in multiple places or want to organize complex code, you can define and use procedures (similar to functions or methods in other languages).

  • Blocks:
    • procedure [name] do [do]: Defines a named block of code that does not return a result.
    • procedure [name] result [do]: Defines a named block of code that performs actions and returns a result value.
    • call [procedure name]: Executes a defined procedure.
  • Description: Procedures improve code reusability, make the Blocks editor cleaner, and enhance program readability.

Screen Navigation

For apps with multiple screens, these control structures manage movement between them.

  • Blocks:
    • open another screen screenName [screenName]
    • open another screen with start value screenName [screenName] startValue [value]
    • close screen
    • close screen with value result [value]
    • close application
  • Description: Allows you to open other screens, close the current screen, or exit the entire application. You can pass values (text, numbers, lists, etc.) when opening a screen and return values when closing one.

Summary

Understanding and using control structures allows you to dictate how your App Inventor app behaves:

  • Conditional (if): Change behavior based on conditions.
  • Loops (for, while): Repeat actions efficiently.
  • Event Handling (when): Respond to user input and system events.
  • Procedures: Organize and reuse code.
  • Screen Navigation: Move between different parts of your app.

By combining these structures, you can build apps ranging from simple utilities to complex, interactive applications. Experimenting with them is the best way to master controlling the flow of your App Inventor programs.