# Continue only allowed inside a loop?

**URL:** <https://forums.swift.org/t/continue-only-allowed-inside-a-loop/30331>\
**Category:** Using Swift\
**Created:** [October 30, 2019, 6:23pm UTC](https://forums.swift.org/t/continue-only-allowed-inside-a-loop/30331 "2019-10-30T18:23:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mango](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mango/32/8501_2.png) [@mango](https://forums.swift.org/u/mango)\
**Post date:** [October 30, 2019, 6:23pm UTC](https://forums.swift.org/t/continue-only-allowed-inside-a-loop/30331/1 "2019-10-30T18:23:04Z")

</div>

I am trying to practice using guard statements, but I keep getting an error message in XCode, which says: "'continue' is only allowed inside a loop."

Could someone please let me know why I am getting this message? I don't understand what is wrong with my code. Thank you.

```
func calculateFibisAge() {
var fibisAge = 0
let fibisHair = "White"

if fibisAge != 100 && fibisHair != "Pink" {
    
guard fibisAge != 35 else {print("Fibi is now an adult")}

fibisAge += 1
continue
}

}
calculateFibisAge()

```

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [October 30, 2019, 6:24pm UTC](https://forums.swift.org/t/continue-only-allowed-inside-a-loop/30331/2 "2019-10-30T18:24:33Z")

</div>

What do you want the program to do at `continue`?

---

<div class="post-metadata">

**Author:** ![mango](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mango/32/8501_2.png) [@mango](https://forums.swift.org/u/mango)\
**Post date:** [October 30, 2019, 6:25pm UTC](https://forums.swift.org/t/continue-only-allowed-inside-a-loop/30331/3 "2019-10-30T18:25:54Z")

</div>

I'd like for it to check again for Fibi's age (that it's not equal to 35) and then keep adding 1 to Fibi's age.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [October 30, 2019, 6:29pm UTC](https://forums.swift.org/t/continue-only-allowed-inside-a-loop/30331/4 "2019-10-30T18:29:38Z")

</div>

To go back at the guard’s first expression? In that case, you’re repeating a portion of code, and need a “loop”.

```swift
while fibisAge == 35 {
  print("Fibi is now an adult")}
  fibisAge += 1
}

```

Though I couldn’t understand what your code as a whole is trying to do, so I’m not sure that’s what you want.

---

<div class="post-metadata">

**Author:** ![mango](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mango/32/8501_2.png) [@mango](https://forums.swift.org/u/mango)\
**Post date:** [October 30, 2019, 6:31pm UTC](https://forums.swift.org/t/continue-only-allowed-inside-a-loop/30331/5 "2019-10-30T18:31:00Z")

</div>

Thank you Lantua, is it possible to use a guard statement outside of the context of optionals? For example can I use a guard statement in an if statement?

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [October 30, 2019, 6:51pm UTC](https://forums.swift.org/t/continue-only-allowed-inside-a-loop/30331/6 "2019-10-30T18:51:40Z")

</div>

Guard statement can be used anywhere. The problem in your code is `continue`.

`continue` will go to the next loop, so you need a loop to go to, hence the warning.

```swift
for a in array {
  continue // go to the next `for` iteration.
}

```

`guard` is essentially a reverse `if`, so any where `if` is permitted, so will `guard`.

```swift
guard value else {
  // We go in here ONLY if `value` is FALSE
}

The main difference is that you need to get out of the current scope, using `continue`, `break`, or `return`. This is because `guard` is designed to handle something that make “the code unable to go on”.
```

---

<div class="post-metadata">

**Author:** ![mango](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mango/32/8501_2.png) [@mango](https://forums.swift.org/u/mango)\
**Post date:** [October 30, 2019, 6:57pm UTC](https://forums.swift.org/t/continue-only-allowed-inside-a-loop/30331/7 "2019-10-30T18:57:47Z")

</div>

Okay, I understand now, thank you Lantua! I see where I went wrong as well. Thank you!

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [October 30, 2019, 7:02pm UTC](https://forums.swift.org/t/continue-only-allowed-inside-a-loop/30331/8 "2019-10-30T19:02:34Z")

</div>

Be careful that you go into `guard` When the statement is `false` in contrast to `if` that goes in when the statement is `true`.

```swift
if statement {
  // when statement is TRUE
} else {
  // when statement is FALSE
}

// common for both `TRUE` and `FALSE`

```

```swift
guard statement else {
  // when statement is FALSE
}

// when statement is TRUE

```

Note that there’s no “common” code for `guard` because `guard` forces you to bail out if you go into `false` case. This is perhaps also part of the design.
