Swift - Statement Continue Statement

Introduction

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:

Demo

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

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.