Loops

Loops are useful when you need to repeat the same process multiple times.

In programming, there are usually several types of loops available, but basically, they are as follows.

Examples of Loops

Here are some examples of how loops can be used in programming.


      string = "CDEFGABC"
      target_char = "A"
      found_pos = -1
      i = 0
      length = len(string)
      repeat length times:
          if string[i] == target_char:
              found_pos = i
              break
          i = i + 1
      print(found_pos)
    

In this example, the loop iterates through each character in the string to find the position of the target character. If the character is found, the loop breaks and the position is printed. If the character is not found, the position remains -1.

Try it out:

You can experiment with loops using the editor.