O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The continue statement

The continue statement tells a loop to stop executing the code block and to go to the next iteration of the loop. The following example shows how we can use this statement to print out only the odd numbers in a range:

for i in 1...10 {  
  if i % 2 == 0 { 
    continue 
  } 
  print("\(i) is odd") 
} 

In the preceding example, we looped through a range of 1 through 10. For each iteration of the for-in loop, we used the remainder (%) operator to see whether the number was odd or even. If the number is even, the continue statement tells the loop to immediately go to the next iteration of the loop. If the number is odd, we print out that the number is odd and then move on. The output of the preceding code is as follows:

1 is odd 3 is ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required