Learn

When iterating through a sequence, we might not need to use every single value. In these cases, it’d be nice to just tell our loop to skip a value. That’s where continue comes in.

If we wanted to only print out all the characters except for vowels ("a", "e", "i", "o", "u") in a String:

let challenge = "bring it" for char in challenge { switch char { case "a", "e", "i", "o", "u": continue default: print(char) } }

In the example above, we have a switch statement which checks char and if it is any of the following characters: "a", "e", "i", "o", and "u". Inside our case statement, if the character matches, we skip over it with continue. If our character is not a vowel, then we print it out.

When we run our code, it prints out:

b r n g t

Instructions

1.

Currently, in our for-in loop we print out every single number in the range, 1...9. However, we don’t want to print out the odd numbers.

Inside the for-in loop, add the code needed to skip over the odd numbers by adding:

  • An if statement that checks if num is an odd number.
  • Inside the body of the if statement, the continue keyword.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?