give me an example of a while loop that prints 1 to 5 in swift

Here's an example of a while loop in Swift that prints numbers from 1 to 5:

main.swift
var count = 1

while count <= 5 {
    print(count)
    count += 1
}
68 chars
7 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.

related categories

gistlibby LogSnag