-2

swift: about ternary operator Question. Why my code is error code??? Please tell me why I'm wrong.


var arr = [0,1,2,3,4,5,6,7,8]

var result = 0;

for a in 0..<arr.count{
    for b in 1..<arr.count - 1{
        for c in 2..<arr.count - 2 {
            arr[a] + arr[b] + arr[c] <= input[1] ? result = arr[a] + arr[b] +arr[c] : continue
        }
    }
}

[this is my error] [1]: https://i.stack.imgur.com/UdiUB.png

sweetSwift
  • 37
  • 5
  • 1
    It looks like this code should have quite a few errors — for instance, `{0,1,2...}` is not valid syntax for an array. However, when you share code like this, please include the actual error message text you're seeing so you can get specific help. – Itai Ferber Apr 20 '22 at 16:36
  • Oops, I'm sorry I write wrong syntax. – sweetSwift Apr 20 '22 at 16:44
  • 1
    You can't mix type/behaviour like that in a ternary operator (assignment : continue), rewrite the code without it. Normally you write it as `result = ? : ` – Joakim Danielson Apr 20 '22 at 17:07
  • 1
    @sweetSwift Just a heads-up that general, it's preferred that you include the text of your error directly in the post rather than an image. (Both for accessibility, and since links break over time.) – Itai Ferber Apr 20 '22 at 17:30
  • thank you for telling me. – sweetSwift Apr 21 '22 at 21:18

1 Answers1

3

In Swift, the ternary condition operator is an expression which takes the form

<condition> ? <expression if true> : <expression if false>

Expressions are part of larger statements, and the ternary specifically is one which evaluates to either the expression after the ?, or the one after the : depending on the truth of the condition.

continue, however, is not an expression but a statement on its own, which means that it cannot be on either side of the ternary.

Thinking about this another way: expressions evaluate to some value (e.g., can be put on the right-hand-side of an assignment, like x = <some expression>), while statements do not (e.g., it doesn't make sense to write x = continue).

You will need to express this in the form of a regular if-statement then:

if arr[a] + arr[b] + arr[c] <= input[1] {
    result = arr[a] + arr[b] +arr[c]
} else {
    continue
}

Note that the above code might be grammatically correct (in that it will compile), but it is unlikely to be what you mean: the loop will automatically continue at the end of execution even if arr[a] + arr[b] + arr[c] <= input[1] by default, which means that your result may get overwritten later in the loop. It seems likely that you mean something like

outer_loop: for a in 0 ..< arr.count {
    for b in 1 ..< arr.count - 1 {
        for c in 2 ..< arr.count - 2 {
            if arr[a] + arr[b] + arr[c] <= input[1] {
                result = arr[a] + arr[b] + arr[c]

                // `break` would only exit the `c` loop, but with this label
                // we can exit all loops at once.
                break outer_loop
            }
        }
    }
}
Itai Ferber
  • 28,308
  • 5
  • 77
  • 83
  • I understand the condition of expression and ternary operator well. thank you. Additionally, thank you for letting us know about outer_loop. – sweetSwift Apr 21 '22 at 21:23