mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
We've talked about adding the title to the filename several times over the years and it seems really valuable. This requires us to compute a "slug" for the title spelling that can be part of the filename. Beyond that, we crossed 7000 recently, and so it seems likely that we will need to add digits sooner rather than later here, so this goes ahead and moves us to 6 digits so we don't have to adjust again for a reasonable length of time. To implement this and ensure we can sustain it going forward this adds a tool to our pre-commit that validates (and corrects if needed) the filename. In order to update everything and keep links working, there are a _lot_ of changes, but the most interesting for direct review are in `proposals/scripts`. Assisted-by: Antigravity with Gemini --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
85 lines
2.4 KiB
Markdown
85 lines
2.4 KiB
Markdown
# Conditionals
|
|
|
|
<!--
|
|
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
Exceptions. See /LICENSE for license information.
|
|
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
-->
|
|
|
|
<!-- toc -->
|
|
|
|
## Table of contents
|
|
|
|
- [Overview](#overview)
|
|
- [Alternatives considered](#alternatives-considered)
|
|
- [References](#references)
|
|
|
|
<!-- tocstop -->
|
|
|
|
## Overview
|
|
|
|
`if` and `else` provide conditional execution of statements. Syntax is:
|
|
|
|
> `if (`_boolean expression_ `) {` _statements_ `}`
|
|
>
|
|
> [ `else if (` _boolean expression_ `) {` _statements_ `}` ] ...
|
|
>
|
|
> [ `else {` _statements_ `}` ]
|
|
|
|
Only one group of statements will execute:
|
|
|
|
- When the first `if`'s boolean expression evaluates to true, its associated
|
|
statements will execute.
|
|
- When earlier boolean expressions evaluate to false and an `else if`'s
|
|
boolean expression evaluates to true, its associated statements will
|
|
execute.
|
|
- `... else if ...` is equivalent to `... else { if ... }`, but without
|
|
visible nesting of braces.
|
|
- When all boolean expressions evaluate to false, the `else`'s associated
|
|
statements will execute.
|
|
|
|
When a boolean expression evaluates to true, no later boolean expressions will
|
|
evaluate.
|
|
|
|
Note that `else if` may be repeated.
|
|
|
|
For example:
|
|
|
|
```carbon
|
|
if (fruit.IsYellow()) {
|
|
Print("Banana!");
|
|
} else if (fruit.IsOrange()) {
|
|
Print("Orange!");
|
|
} else if (fruit.IsGreen()) {
|
|
Print("Apple!");
|
|
} else {
|
|
Print("Vegetable!");
|
|
}
|
|
fruit.Eat();
|
|
```
|
|
|
|
This code will:
|
|
|
|
- Evaluate `fruit.IsYellow()`:
|
|
- When `True`, print `Banana!` and resume execution at `fruit.Eat()`.
|
|
- When `False`, evaluate `fruit.IsOrange()`:
|
|
- When `True`, print `Orange!` and resume execution at `fruit.Eat()`.
|
|
- When `False`, evaluate `fruit.IsGreen()`:
|
|
- When `True`, print `Apple!` and resume execution at
|
|
`fruit.Eat()`.
|
|
- When `False`, print `Vegetable!` and resume execution at
|
|
`fruit.Eat()`.
|
|
|
|
## Alternatives considered
|
|
|
|
- [Optional braces](/proposals/p000623-require-braces.md#optional-braces)
|
|
- [Optional parentheses](/proposals/p000623-require-braces.md#optional-parentheses)
|
|
- [`elif`](/proposals/p000623-require-braces.md#elif)
|
|
|
|
## References
|
|
|
|
- Proposal
|
|
[#285: `if` and `else`](https://github.com/carbon-language/carbon-lang/pull/285)
|
|
- Proposal
|
|
[#623: Require braces](https://github.com/carbon-language/carbon-lang/pull/623)
|