Files
vault/Career/Theory/Ternary Operator.md
Zaine 129ce1442b
Some checks failed
Build Quartz Notes / build (push) Failing after 20s
13
2026-07-13 09:16:09 +01:00

21 lines
1.2 KiB
Markdown
Executable File
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
note type:
- theory
date: 2026-06-19
done: true
---
The **ternary operator**, also known as the **conditional operator**, is a concise way to write an **if-else** statement in a single line by evaluating a **condition** and returning one of two values.  It is called "ternary" because it operates on **three operands**: the condition, the expression to execute if true, and the expression to execute if false. 
The standard syntax is:
```
condition ? expression_if_true : expression_if_false
```
Key characteristics include:
- **Short-Circuit Evaluation**: Only the expression corresponding to the evaluated condition is executed. 
- **Readability**: It enhances code brevity for simple logic but can reduce maintainability if nested excessively. 
- **Language Support**: It is widely supported in C-like languages (C, C++, Java, JavaScript, C#) and has variations in others (e.g., Python uses `value_if_true if condition else value_if_false`). 
- **Performance**: It generally has negligible performance differences compared to standard if-else blocks, as modern compilers optimize both similarly. 
- **Association**: In most languages, the operator is **right-associative**, allowing for chained conditions similar to `if...else if...else` chains.