Swift - Statement Continue Statement


The Continue statement stops the execution of the rest of the statements in this loop and goes on to the next iteration.

The following snippet shows how count the number of characters in a string, excluding the spaces using the Continue statement:


//count the number of characters (excluding spaces)
var c :Character/*from   w  ww .  j a v a 2s.c o m*/
var count = 0
for c in "This is a string" {
    if c == " " {

print("Number of characters is \(count)")

Here, when a space is encountered, the Continue statement transfers the execution to the next iteration of the loop, effectively bypassing the statement where the count variable is incremented.