> For the complete documentation index, see [llms.txt](https://docs.launchboard.app/cxd/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.launchboard.app/cxd/intro-to-informatics-and-web-development/1-7-if...else-statements.md).

# 1-7 If...Else Statements

**If...Else Statements** are **conditional statements** that are used to perform different actions based on different conditions. Conditional statements allow a computer program to make decisions based on the given conditions.

## Basic Syntax

```
if (condition) {
    code to be executed if the condition is true;
} else { 
    code to be executed if the condition is false;
}
```

### Example:

```
if (score = 10) {
    scoreMessage = 'Great job!';
} else {
    scoreMessage = 'Better luck next time!';
}
```

Based on this code, if the score value is 10 (meaning the condition is True), "Great job!" will display. If the score is anything besides 10 (meaning the condition is False), "Better luck next time!" will display.

## Else If Statements

When you need to have multiple conditions to check against, you can use an Else If statement. The syntax is a little different than a basic If...Else statement.

```
if (condition1) {
    code to be executed if condition1 is true;
} else if (condition2) {
    code to be executed if the condition1 is false and condition2 is true;
} else {
    code to be executed if the condition1 is false and condition2 is false;
}
```

### Example:

```
if (energyLevel > 99) {
    energyTank = game.add.sprite(game.world.centerX, game.world.centerY, 'fullEnergyTank');
} else if (energyLevel > 20) {
    energyTank = game.add.sprite(game.world.centerX, game.world.centerY, 'lowEnergyTank');
    screenMessage = 'Your energy tanks are getting low!';
} else {
    energyTank = game.add.sprite(game.world.centerX, game.world.centerY, 'emptyEnergyTank');
    screenMessage = 'Game Over';
}
```

Based on the If Else statement above, if the value of energyLevel is greater than 99, the fullEnergyTank image will display and nothing else will happen. If energyLevel isn't greater than 99 (less than or equal to 99), it will check against the 2nd condition. If the value of energyLevel is greater than 20, the lowEnergyTank image will display and a message will display on the screen saying the energy tanks are low. If both of the conditions above were false, the emptyEnergyTank image will display, and the screen will display a Game Over message.

[More Information](https://www.w3schools.com/js/js_if_else.asp)


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.launchboard.app/cxd/intro-to-informatics-and-web-development/1-7-if...else-statements.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
