# Create a if statement with a range of ints

**URL:** <https://forums.kodeco.com/t/create-a-if-statement-with-a-range-of-ints/115876>\
**Category:** Swift\
**Created:** [July 26, 2020, 5:21am UTC](https://forums.kodeco.com/t/create-a-if-statement-with-a-range-of-ints/115876 "2020-07-26T05:21:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [July 26, 2020, 5:21am UTC](https://forums.kodeco.com/t/create-a-if-statement-with-a-range-of-ints/115876/1 "2020-07-26T05:21:10Z")

</div>

I want to make a if statement that equals several different numbers. A example is below that compels but writing score every time could take a lot of time.

```
if score == 4 || score == 5 || score == 6 {

print("Yes")

```

I want to somehow write a if statement that goes like

```
if score == {4,5,6}{
print ("yes")}

```

---

<div class="post-metadata">

**Author:** ![atfelix](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/atfelix/32/102075_2.png) [@atfelix](https://forums.kodeco.com/u/atfelix)\
**Post date:** [July 26, 2020, 5:28am UTC](https://forums.kodeco.com/t/create-a-if-statement-with-a-range-of-ints/115876/2 "2020-07-26T05:28:46Z")

</div>

The simplest way I can think of would be to use [`contains`](https://developer.apple.com/documentation/swift/sequence/2904924-contains) which comes from the [`Sequence protocol`](https://developer.apple.com/documentation/swift/sequence)

```swift
if [4, 5, 6].contains(score) {
    print("Yes")
}

```

---

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [July 26, 2020, 7:58pm UTC](https://forums.kodeco.com/t/create-a-if-statement-with-a-range-of-ints/115876/3 "2020-07-26T19:58:22Z")

</div>

that works thanks i test it.

---

<div class="post-metadata">

**Author:** ![mokhet](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mokhet/32/102075_2.png) [@mokhet](https://forums.kodeco.com/u/mokhet)\
**Post date:** [July 27, 2020, 4:48am UTC](https://forums.kodeco.com/t/create-a-if-statement-with-a-range-of-ints/115876/4 "2020-07-27T04:48:48Z")

</div>

With the provided values/sample, I would have used [contains(:)-\>Bool](https://developer.apple.com/documentation/swift/closedrange/1784630-contains) from the [RangeExpression](https://developer.apple.com/documentation/swift/rangeexpression) protocol

```auto
if Range(4...6).contains(score) { print("Yes") }

```

---

<div class="post-metadata">

**Author:** ![dleuck](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/dleuck/32/106555_2.png) [@dleuck](https://forums.kodeco.com/u/dleuck)\
**Post date:** [July 29, 2020, 6:54am UTC](https://forums.kodeco.com/t/create-a-if-statement-with-a-range-of-ints/115876/5 "2020-07-29T06:54:51Z")

</div>

@mokhet’s solution is best if your `int`s represent a range. You can shorten it to:

`if 4...6 ~= score { print("Yes") }`

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [March 20, 2021, 3:51pm UTC](https://forums.kodeco.com/t/create-a-if-statement-with-a-range-of-ints/115876/8 "2021-03-20T15:51:43Z")

</div>



---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [March 20, 2021, 4:08pm UTC](https://forums.kodeco.com/t/create-a-if-statement-with-a-range-of-ints/115876/9 "2021-03-20T16:08:49Z")

</div>

@timswift You can also use `if case` with ranges like this:

```
if case 4...6 = score {
  print("yes")
}
```

I hope it helps!
