> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Dart If Statement

The `if` statement in Dart is a synchronous control flow construct that dictates the execution path of a program by evaluating a boolean expression. If the expression resolves to `true`, the lexically scoped block of code immediately following the condition is executed.

## Standard Syntax

The standard `if` statement can be chained with `else if` for sequential condition evaluation and terminated with an optional `else` block as a fallback execution path.

```dart theme={"dark"}
void main() {
  bool isPrimary = false;
  bool isSecondary = true;

  if (isPrimary) {
    print('Primary condition met.');
  } else if (isSecondary) {
    print('Secondary condition met.');
  } else {
    print('Fallback execution path.');
  }
}
```

## Strict Boolean Evaluation

Unlike languages that utilize "truthy" or "falsy" type coercion (such as JavaScript or C), Dart enforces strict static type checking. The condition within the `if` clause **must** evaluate explicitly to a `bool` type.

Attempting to evaluate a non-boolean type, such as an integer or a non-null object, results in a compile-time error.

```dart theme={"dark"}
void main() {
  int statusCode = 200;

  // INVALID: if (statusCode) { print('Success'); } // Results in a compile-time error

  // VALID: Explicit boolean evaluation
  if (statusCode == 200) {
    print('Status is OK.');
  }
}
```

## Block Scope and Braces

Variables declared within the curly braces `{}` of an `if`, `else if`, or `else` block are lexically scoped to that specific block and cannot be accessed externally.

Dart permits the omission of curly braces if the execution block consists of a single statement. However, the official Dart style guide strongly recommends using braces for all control flow structures to prevent dangling-else ambiguity and improve maintainability.

```dart theme={"dark"}
void main() {
  bool condition = true;

  void executeStatement() {
    print('Statement executed.');
  }

  // Valid single-line syntax (braces omitted)
  if (condition) executeStatement();

  // Recommended syntax
  if (condition) {
    executeStatement();
  }
}
```

## Pattern Matching (`if-case`)

Introduced in Dart 3, the `if-case` statement extends the standard `if` construct by integrating pattern matching. It evaluates whether a given expression matches a specific pattern, rather than evaluating a boolean expression. If the match is successful, it can simultaneously extract and bind values to local variables scoped to the `if` block.

The `if-case` construct can also be chained with standard `else if` and `else` blocks, and supports guard clauses using the `when` keyword to apply additional boolean constraints to a matched pattern.

```dart theme={"dark"}
void main() {
  Object responseData = [200, 404];

  // Evaluates if 'responseData' matches a list containing exactly two integers
  if (responseData case [int x, int y]) {
    // 'x' and 'y' are now bound and lexically scoped to this block
    print('Matched integers: $x and $y');
  } else {
    print('Pattern did not match.');
  }
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Dart Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
