> 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/decision-making-with-if-statements.md).

# Decision-making with If Statements

The previous section introduced the concept of "branching", which are a way of instructing programs to only perform certain tasks in certain situations. If you skipped it, or need a refresher, [read it here](/sonarlang/syntax-basics/conditionals.md).

The program we are going to create in this section is a simple traffic controller. We will output a colour depending on the current traffic condition, and we will progressively improve our program throughout the section.

### What Goes into a Traffic Controller?

A traffic controller is a program that displays a colour (either red, amber or green), depending on the traffic condition on a road junction or intersection.

The first traffic controller we are going to create will consist of a variable we will call **condition** and we will display:

* red, if **condition** is "stop"
* amber, if **condition** is "ready"
* green, if **condition** is "go"

In order for our controller to know what to display, we will use the **If** statement. The **If** statement is used to tell our program to do certain **only if** a certain condition is true. We use **if** statements in real life:

* "if it rains today, I will not go out"
* "if I was hungry, I would have eaten"
* "if it is a weekday, I will have to go to school"

### Our First Traffic Controller Program

We want to create a variable we will call **traffic\_controller**. Next, we will use the **if** statement to check whether **traffic\_condition** is "stop" (using the equality operator (==) we learned under **Data Types > Strings** [here](/sonarlang/syntax-basics/data-types/strings.md#equality-of-strings).

{% code lineNumbers="true" %}

```javascript
let traffic_condition = "stop"

if (traffic_condition == "stop") {
    print("red")
}

// outputs red
```

{% endcode %}

Let's see what each line of our program does.

1. creates a new variable called **traffic\_condition** which stores a value of "stop" (which is a string)
2. empty line (we use empty lines like this to make it easier to read our program)
3. check whether **traffic\_condition** has a value of "stop" or not
4. if **traffic\_condition** has a value of "stop", then print "red"
5. tell the computer that our **if** statement has ended (like a full-stop or period)
6. empty line

If we type this in a .sl file and run our program, we'll get "red" back.

### Writing the If-statement

The **If** statement can look strange when you first encounter it, but as you write more and more programs, you will become used to its strangeness and you will begin to understand why it looks the way it does.

> In programming, a "statement" is the equivalent of a verb in normal languages like English. Statements are used to perform actions. We have already seen the **Let** statement which creates variables (see [Variables](/sonarlang/syntax-basics/variables.md)).

The **If** statement has 3 parts:

1. The **if** keyword
2. The condition (or expression)
3. The decision (or consequence)

> A **keyword** is a word that has a special meaning in Sonar. You are not allowed to create variables with any keywords as their names. Examples of keywords are: let, if, else, while, for, true and false.

<img src="https://2839518068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F4Z7hJCZzGLUdOLw7EvoB%2Fuploads%2FvtX23f0WTSJAadrPJ3mn%2Ffile.drawing.svg?alt=media&amp;token=54f7380b-4fbe-41a4-b565-31731143dd98" alt="" class="gitbook-drawing">

* The **if** keyword should always be followed by a left parenthesis **(.** This tells the computer that you are about to start writing the **condition** part of your **If** statement.
* Your **condition** should always be surrounded by left and right parentheses **().**&#x20;
* After writing your **condition**, your **decision** should be written between two curly braces **{}.** This tells the computer that you are writing the **decision** part of your **If** statement.

### Completing the Traffic Controller Program

Now that we know how to write **If** statements, we can complete our traffic controller program:

```javascript
let traffic_condition = "ready"

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

// outputs amber
```

Changing the value of **traffic\_controller** to either "stop" or "go" will output either "red" or "green". It is important to note that the program will never output more than one colour (because **traffic\_condition** can only have one value).
