Topics List
Basic Concepts

Loops
Exploring Iteration and Control Structures

Loops are essential in programming as they allow us to execute a block of code repeatedly. In Kenzie, there are several types of loops, each serving a specific purpose and offering different ways to control the flow of your program. Understanding how to use loops effectively can significantly enhance your ability to write efficient and powerful code.
The following examples demonstrate various ways to use loops in Kenzie to iterate over a range of values or conditionally execute code.
  1. Printing Values from 1 to 100:

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

    This loop iterates from 1 to 100 and prints each value to the console.

  2. Changing the Increment:

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

    This loop iterates from 1 to 100 with an increment of 3.11 and prints each value to the console.

  3. Values with Intervals of 0.2:

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

    This loop iterates from 10 to 20 with an increment of 0.2 and prints each value to the console.

  4. Printing Values Until i is Equal to 5:

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

    This loop repeatedly increments the variable i by 1 and prints its value until i equals 5.

  5. Printing Values While i is Less Than 5:

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

    This loop repeatedly increments the variable i by 1 and prints its value as long as i is less than 5.

In Lua (fully compatible with Kenzie), you can also use them as follows:
  1. For Loop:
    The for loop in Lua has two forms: numeric and generic. The numeric for loop is used to iterate over a numeric range, similar to the examples you provided earlier. The generic for loop is used to iterate over all values returned by an iterator function.
    Numeric For Loop:

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

    This loop iterates from start to finish with an optional step value.

  2. Generic For Loop:

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

    This loop iterates over all key-value pairs in a table.

  3. While Loop:
    The while loop repeatedly executes a block of code as long as a given condition is true.

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

    This loop continues to execute as long as condition evaluates to true.

  4. Repeat...Until Loop:
    The repeat...until loop is similar to a while loop, but the condition is evaluated after the loop body is executed, so the loop always runs at least once.

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

    This loop repeats until condition is true.

These loop constructs provide flexibility for iterating over data structures, controlling program flow, and implementing repetitive tasks in Kenzie / Lua programs.
Deciding which loop to use in a given situation depends on the specific requirements and conditions of your code. Here´s a detailed guide on when to use each loop option:
  1. For Loop:
    • Use Case: When you know the exact number of iterations needed.

    • Syntax: for variable = start_value, end_value [, step_value] do ... end

    • Example: Looping through a fixed number of elements in an array or iterating a specific number of times.

  2. While Loop:
    • Use Case: When the number of iterations is not known beforehand and depends on a condition.

    • Syntax: while condition do ... end

    • Example: Performing a task until a certain condition is no longer met, such as processing user input until they enter a specific value.

  3. Repeat...Until Loop:
    • Use Case: When you want to execute the loop body at least once before checking the condition.

    • Syntax: repeat ... until condition

    • Example: Validating user input where you want to ensure the input is correct before proceeding.

Choosing the Right Loop:


  • If you know the exact number of iterations, use a for loop.

  • If the number of iterations is based on a condition, use a while loop.

  • If you need to execute the loop body at least once, use a repeat...until loop.

Example Scenario:
Let´s say you want to iterate over a list of items until you find a specific item or reach the end of the list. In this case, you would use a while loop because the number of iterations is based on the condition of finding the item.

Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

In this example, the while loop iterates over the items array until it finds the targetItem or reaches the end of the array.

Loop Control Statements


  1. Break Statement:
    The break statement is used to exit a loop prematurely based on a certain condition.
    Example:

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

    This loop will print numbers from 1 to 4 and then exit when i becomes 5.

  2. Continue Statement:
    The continue statement is not directly available in Lua, but you can achieve similar behavior using conditional statements.
    Example:

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

    This loop will print numbers from 1 to 5 but skip printing 3.

Nested Loops


  1. Nested For Loops:
    You can nest one or more for loops inside another for loop to iterate over multidimensional arrays or for other complex iterations.
    Example:

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

    This nested loop will print combinations of i and j from 1 to 3, resulting in a total of 9 pairs.

  2. Nested While Loops:
    Similarly, you can nest while loops inside each other for more complex iterations.
    Example:

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

    This nested while loop achieves the same result as the nested for loop above.

  3. Mixed Nested Loops:
    You can also mix different types of loops inside each other, such as a for loop inside a while loop or vice versa.
    Example:

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

    This mixed nested loop will print combinations of i and j where i ranges from 1 to 3 and j ranges from 1 to 2.

These loop control statements and nested loops can help you write more flexible and powerful code to handle various looping scenarios in your programs.