# Nested if statement in swift function

**URL:** <https://forums.kodeco.com/t/nested-if-statement-in-swift-function/24451>\
**Category:** Swift\
**Created:** [December 24, 2016, 4:54am UTC](https://forums.kodeco.com/t/nested-if-statement-in-swift-function/24451 "2016-12-24T04:54:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zalubski](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/zalubski/32/81494_2.png) [@zalubski](https://forums.kodeco.com/u/zalubski)\
**Post date:** [December 24, 2016, 4:54am UTC](https://forums.kodeco.com/t/nested-if-statement-in-swift-function/24451/1 "2016-12-24T04:54:47Z")

</div>

The following function counts down a red to green light, then counts the reaction time for the user to hit a button after the green light is displayed.

```
    func updateCounter() {

timerInt -= 1
if timerInt == 2{
    light.image = UIImage(named: "r.png")
} else if timerInt == 1 {
    light.image = UIImage(named: "yellow.png")

} else if timerInt == 0 {

    light.image = UIImage(named: arc4random_uniform(2) == 0 ? "no.png" : "g.png")

    timer.invalidate()
    startStop.isEnabled = true
    scoreTimer = Timer.scheduledTimer(timeInterval: 0.0001, target: self, selector: #selector(ViewController.updateScoreTime), userInfo: nil, repeats: true)

}
}

```

how can i write the code so that when it states “else if timerInt == 0”. The code will change timeIntervals depending on whatever arc4random images is chosen. So that when else if timerInt == 0 and no.png the time interval is 0.01 and when it states else if timerInt == 0 and g.png the time interval is 0.0001. Thanks

---

<div class="post-metadata">

**Author:** ![fusetech](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/fusetech/32/97552_2.png) [@fusetech](https://forums.kodeco.com/u/fusetech)\
**Post date:** [December 25, 2016, 7:00am UTC](https://forums.kodeco.com/t/nested-if-statement-in-swift-function/24451/2 "2016-12-25T07:00:37Z")

</div>

I would store the boolean of whether to show no.png or g.png, and continue to use ternary operators with that boolean for the values that need to change.

`
var shouldGo = arc4random_uniform(2) == 0
light.image = UIImage(named: shouldGo ? "g.png" : "no.png")
scoreTimer = Timer.scheduledTimer(timeInterval: shouldGo ? 0.0001 : 0.01)
`

---

<div class="post-metadata">

**Author:** ![creed.erickson](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/creed.erickson/32/68079_2.png) [@creed.erickson](https://forums.kodeco.com/u/creed.erickson)\
**Post date:** [December 28, 2016, 10:24pm UTC](https://forums.kodeco.com/t/nested-if-statement-in-swift-function/24451/3 "2016-12-28T22:24:29Z")

</div>

```
func updateCounter() {

	var imageName = "no.png"
	var interval = 0.01
	let goCondtion = arc4random_uniform(2) == 0

	timerInt -= 1
	switch (timerInt) {

	case 2:
		imageName = "r.png"

	case 1:
		imageName = "yellow.png"
		
	case 0: 
		if goCondtion {
			imageName = "g.png"
			interval = 0.0001
		}
		
		timer.invalidate()
		startStop.isEnabled = true
		scoreTimer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(ViewController.updateScoreTime), userInfo: nil, repeats: true)

	default:
		// do nothing
		break
	}			
	light.image = UIImage(named: imageName)
}

```
