I have a problem with conditions in Control Flow.

I have a fixed array is: let terms: [Int] = [1,2,3,4,5,6,7,8,9,10,11,12,13,15,18,24,36].
I wrote a function with parameter is an Int. I want to check the parameter that will map with multiple condition in there function. That's my function:

"func configTerms(with term: Int) -> [String] {

termConditions = ["A"]


switch term {

case 1...:

termConditions.append("B")

case 2...:

termConditions.append("C")

case 6... where term % 3 == 0:

termConditions.append("D")

case 12... where term % 6 == 0:

termConditions.append("E")

case 24... where term % 12 == 0:

termConditions.append("F")

default:

print("Can't match any value")

}


return termConditions

}"
I can resolve it by using "if...else" control flow but the last condition will be very long and complex.

I used "switch...case" control flow but it couldn't return result that i wanted.

Example: When input was 12, function only returned ["A","B"] instead of ["A","B","C","D","E"]

Everytime the condition matched, the "switch...case" breaked out and only returned ["A","B"].
I used fallthrough but it didn't check the next condition and executed next expression.
How can i continue using "switch...case" in this case without using "if...else"?

Accepted Reply

That's a little bit different than I guessed. I think you have no need to nest `if`s.


func configTerms2(with term: Int) -> [String] {
    var termConditions = ["A"]
    
    if case 1... = term {
        termConditions.append("B")
    }
    if case 2... = term {
        termConditions.append("C")
    }
    if case 6... = term, term % 3 == 0 {
        termConditions.append("D")
    }
    if case 12... = term, term % 6 == 0 {
        termConditions.append("E")
    }
    if case 24... = term, term % 12 == 0 {
        termConditions.append("F")
    }
    return termConditions
}

Replies

How can i continue using "switch...case" in this case without using "if...else"?


The answer is simple. You cannot. Use `if`. `switch`-statement is intended to match only one case. It may not be what you want.

I've resolved this problem. It's seem to be quite ugly but the result is alway true. Below is my code:

func configTerms(with term: Int) -> [String] {

termConditions = ["A"]

switch term {

case 24... where term % 12 == 0:

termConditions.append("B")

termConditions.append("C")

termConditions.append("D")

termConditions.append("E")

termConditions.append("F")

case 12... where term % 6 == 0:

termConditions.append("B")

termConditions.append("C")

termConditions.append("D")

termConditions.append("E")

case 6... where term % 3 == 0:

termConditions.append("B")

termConditions.append("C")

termConditions.append("D")

case 2...:

termConditions.append("B")

termConditions.append("C")

case 1...:

termConditions.append("B")

default:

print("Can't match any value")

}

return termConditions

}

If u have better answer. Plese let me know. Thanks for reply!

If u have better answer. Plese let me know.

For me, using-`if` seems to be super better than using such an ugly `switch` forcefully. You know how you can write it in `if`s.

Yes. i know and i tried. It's a nested control flow with 5 grades. Below is my code in "if...else":

if term >= 1{

termConditions.append("B")

if term >= 2{

termConditions.append("C")

if term >= 6 && term % 3 == 0 {

termConditions.append("D")

if term >= 12 && term % 6 == 0 {

termConditions.append("E")

if term >= 24 && term % 12 == 0{

termConditions.append("F")

}

}

}

}

}

return termConditions


In near future, if i add some conditions in it, it'll be more and more complex, so hard to maintain.
Thanks for your advice. I'll think more to take the best choice.

That's a little bit different than I guessed. I think you have no need to nest `if`s.


func configTerms2(with term: Int) -> [String] {
    var termConditions = ["A"]
    
    if case 1... = term {
        termConditions.append("B")
    }
    if case 2... = term {
        termConditions.append("C")
    }
    if case 6... = term, term % 3 == 0 {
        termConditions.append("D")
    }
    if case 12... = term, term % 6 == 0 {
        termConditions.append("E")
    }
    if case 24... = term, term % 12 == 0 {
        termConditions.append("F")
    }
    return termConditions
}

Thank you so so much. I've checked and your answer is the best clearly 😀