Here's an example of a while loop in Swift that prints numbers from 1 to 5:
main.swift68 chars7 lines
This loop initializes a variable count
with an initial value of 1. While count
is less than or equal to 5, it will keep executing the loop body. Inside the loop, we print the value of count
and then increment it by 1 using count += 1
. The loop will continue until count
reaches 6, at which point the condition count <= 5
becomes false, and the loop will terminate.
gistlibby LogSnag