> For the complete documentation index, see [llms.txt](https://icheka-ozuru.gitbook.io/sonarlang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://icheka-ozuru.gitbook.io/sonarlang/syntax-basics/conditionals/better-decision-making-with-if...else-statements.md).

# Better Decision-making with If...Else Statements

In the previous section, we learned how to use the **If** statement in Sonar (read it [here](/sonarlang/syntax-basics/conditionals/decision-making-with-if-statements.md), if you skipped that, or if you need a refresher).

In this section, we will build on our knowledge of **If** statements to make our programs make more complex decisions using the **If...Else** statement.

Sometimes, we make sentences that resemble:

* "if it rains today, I will not go out; but if it doesn't, I will"
* "if today is a weekday, I will have to go to school; but if it isn't, I will go out to play"
* "if I click the Netflix icon, my iPhone will start the Netflix app; but if I click the TikTok icon, my iPhone will start the TikTok app"

Each of these sentences have two conditions that are related. The two conditions are "opposites" (i.e they cannot be true at the same time).

* it cannot rain and not rain at the same time
* it cannot be a weekday and the weekend at the same time
* you cannot click the Netflix app and the TikTok app at the same time (well, you can try –– but you can't; trust me, I tried)

Each of these sentences look like this:

<img src="/files/Sq8kpjDakiwViH7uNu8B" alt="" class="gitbook-drawing">

For example:

<img src="/files/kUmNAmw35P4uRVXUkM9u" alt="" class="gitbook-drawing">

### Examples with If-Else

Many countries have a legal drinking age of 18 years. We can write a program that checks whether a person is able to drink alcohol or not.

{% code lineNumbers="true" %}

```javascript
let adult_age = 18
let age = 17

if (age < adult_age) {
    print("You are not an adult")
} else {
    print("Here. Have a beer.")
}

// outputs You are not an adult
```

{% endcode %}

If we change `age` to a number greater than or equal to 18, the person will be allowed to drink:

{% code lineNumbers="true" %}

```javascript
let adult_age = 18
let age = 18 // any number from 18 upwards

if (age < adult_age) {
    print("You are not an adult")
} else {
    print("Here. Have a beer.")
}

// outputs Here. Have a beer.
```

{% endcode %}

### An Improved Traffic Controller with If-Else

We can make our traffic controller better by replacing our **If** statements with **If-else**:

{% code lineNumbers="true" %}

```javascript
let traffic_condition = "ready"

if (traffic_condition == "stop") {
    print("red")
} else {
    if (traffic_condition == "ready") {
        print("amber")
    } else {
        if (traffic_condition == "go") {
        print("green")
    }
}
```

{% endcode %}

Take a minute to think about our improved program to be certain you understand it.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://icheka-ozuru.gitbook.io/sonarlang/syntax-basics/conditionals/better-decision-making-with-if...else-statements.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
