Loops are a type of control flow structure in programming in which a series of statements may be executed repeatedly until some condition is met.
A loop is a fundamental programming idea that is commonly used in writing programs. They pop up in SharePoint in a number of ways:
- When writing workflows
- When writing server-side (SSOM) or client-side (CCOM, JSOM, REST) code
- When writing apps, or other features requiring programming
A loop can be categorized in two different ways,
1. Entry Controlled Loops
A loop which checks for the condition before the iteration is known as Entry Controlled loops - for example
- while loop - iterates while a certain condition is true
- until loop - iterates while a certain condition is false
- for loop - iterates through numbers in a certain range. Note: not the same as C++
- foreach loop - iterates through the elements of a collections.
2. Exit Controlled Loops
A loop which checks the condition after the iteration is knows as Exit Controlled loop - for example
- do-while loop - iterates while a certain condition is true (the first iteration will run regardless of the condition)
- do-until loop - iterates while a certain condition is false (the first iteration will run regardless of the condition)
Most languages provide only a subset of loop types described above. For example: in Python there are only foreach (keyword
for) and while loops.
Break and continue
In some languages, there are two keywords that simplify the task of implementing a more advanced control flow:
continue. The former allows you to jump to the operator immediately after the loop, the latter allows you to jump to the end of the current iteration.
Example: implementation of do-until loop in Python using the
while True: // loop body if condition: break