Conditionals in Go (challenge answer)
Tags: beginners, tutorial, learning, go
Published: 2019-06-26 07:07:44

---
title: Conditionals in Go (challenge answer)
published: true
tags: beginners, tutorial, learning, go
canonical_url: https://www.codetips.co.uk/languages/go/conditionals-in-go-challenge-answer/
---

 ![Conditionals in Go (challenge answer)](https://images.unsplash.com/photo-1513151233558-d860c5398176?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ)

At the end of the last article ( "[Conditionals in Go](https://www.codetips.co.uk/languages/go/conditionals-in-go/)" ), we said it was possible to optimise the `if/else if/else` statement further and gave you the challenge of figuring out how. If you haven't attempted it; we're about to give you the answer, so now's a good time to go and try it.

For reference, here was the `if/else if/else` statement we said could be optimised:

<!--kg-card-begin: code-->
```
if age < 13 {
  log.Println("I am considered a child")
} else if age >= 13 && age < 20 {
  log.Println("I am considered a teenager")
} else if age >= 20 && age < 70 {
  log.Println("I am considered an adult")
} else {
  log.Println("I am considered a pensioner")
}
```
<!--kg-card-end: code-->

It can be optimised further because we're still doing more checks than we need to. The following diagram shows the flow of this conditional statement:

<!--kg-card-begin: image-->

![Conditionals in Go (challenge answer)](https://www.codetips.co.uk/content/images/2019/06/conditionals-in-go-challenge-answer.jpg)

<!--kg-card-end: image-->

For example, we're checking if `age` is less than thirteen ( `age < 13` ) and, if it is, we print the "child" statement and end the conditional statement. If it isn't, we then check if age is greater than or equal to thirteen ( `age >= 13` ).

This extra step is unnecessary because we've already checked if age is less than thirteen and, if it isn't, then it has to be equal to or greater than thirteen.

Knowing this, we can simplify the flow of our conditional statement to the following:

<!--kg-card-begin: image-->

![Conditionals in Go (challenge answer)](https://www.codetips.co.uk/content/images/2019/06/conditionals-in-go-challenge-answer-simplified.jpg)

<!--kg-card-end: image-->

And therefore our `if/else if/else` statement becomes:

<!--kg-card-begin: code-->
```
if age < 13 {
  log.Println("I am considered a child")
} else if age < 20 {
  log.Println("I am considered a teenager")
} else if age < 70 {
  log.Println("I am considered an adult")
} else {
  log.Println("I am considered a pensioner")
}
```
<!--kg-card-end: code-->

If this isn't clear, feel free to [reach out to us](https://www.codetips.co.uk/contact-us/) and we'll do what we can to help you.


---

[CodeTips](https://www.codetips.co.uk) strives to help beginners, with zero or very little experience, learn to code.

We do cross-post to other sites to reach a wider audience, but why not [subscribe to our newsletter](https://mailchi.mp/92cccf7024b2/codetips) and get the newest articles straight to your mailbox? 

The original source for this content is [CodeTips](https://www.codetips.co.uk). The original content is kept up-to-date, but other sources may not be the latest version.