Here's the basic structure:
```
if (condition) {
// Code to be executed if the condition is true
}
```
Let's break it down:
* `if`: This keyword signals the start of the if-then statement.
* `condition`: This is an expression that evaluates to either `true` or `false`. It could be a simple comparison (e.g., `x > 5`), a logical operation (e.g., `is_raining && temperature < 10`), or anything that results in a Boolean value.
* `{ }`: These curly braces enclose the code block that will be executed only if the condition is true.
Example:
```python
age = 18
if (age >= 18) {
print("You are eligible to vote.")
}
```
In this example:
1. The `age` variable is set to 18.
2. The `if` statement checks if `age` is greater than or equal to 18.
3. Since the condition is true, the code within the curly braces is executed, printing "You are eligible to vote."
Key Points:
* Conditionality: If-then statements provide a way for your program to follow different paths based on whether a condition is true or false.
* Code Execution: The code block within the curly braces is only executed if the condition is true.
* Flexibility: If-then statements are incredibly versatile and used in almost every programming language. You can use them to control program flow, handle errors, validate user input, and much more.
Beyond "if-then":
Programming languages often extend the basic if-then statement with additional options:
* `else`: Specifies a code block to execute if the `if` condition is false.
* `elif` (Python) / `elseif` (other languages): Allows you to check for multiple conditions in sequence.
By mastering if-then statements, you gain essential control over the logic and behavior of your programs.