while statement

The while loop is a control structure in Tipcontrol and similar to languages like C++,it is used to repeat a block of code as long as a specified condition evaluates to true. It is particularly useful when the number of iterations is not known beforehand and should be determined dynamically at runtime.

Structure of a while Loop

Components:

  1. Condition: An expression evaluated before each iteration of the loop. If the condition evaluates to true (non-zero), the code block inside the loop is executed. If it evaluates to false (zero), the loop terminates.
  2. Code Block: The block of code that executes during each iteration while the condition is true. This block is enclosed in curly braces {}.

Simple Example

    counter = 1; // Initialization

    // Loop while counter is less than or equal to 5

    while (counter <= 5) {

        lib.log(“Count: “+ counter);

        counter+=1; // Update the counter

    };

Explanation:

  • Initializationcounter = 1; initializes a counter variable.
  • Conditioncounter <= 5 checks if the counter is less than or equal to 5.
  • Code Block: The statement lib.log("Count: “+ counter); is executed as long as the condition is true.
  • Updatecounter+=1 increments the counter at the end of each iteration to eventually make the condition false and terminate the loop.