---
title: Use curly braces for all flow control structures
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Static Code Analysis (SAST) > SAST
  Rules > Use curly braces for all flow control structures
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Use curly braces for all flow control structures

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

## Metadata{% #metadata %}

**ID:** `dart-code-quality/curly-braces-in-flow-control-structures`

**Language:** Dart

**Severity:** Notice

**Category:** Code Style

## Description{% #description %}

Omitting curly braces around single-statement bodies in if, else, for, and while blocks is a common source of bugs. When a second statement is added later, it may appear to be inside the block but is not.

Always wrap control flow bodies in curly braces, even when there is only one statement, to prevent accidental logic errors.

## Non-Compliant Code Examples{% #non-compliant-code-examples %}

```dart
void main() {
  var x = 0;

  while (true)
    x++;

  for (var i = 0; i < 42; i++)
    x++;

  for (var item in [1, 2, 3])
    x += item;

  if (true)
    x++;
  else
    x--;

  if (x < 1)
    x = 1;
  else if (x > 10)
    x = 10;

  do
    x++;
  while (x < 10);
}
```

## Compliant Code Examples{% #compliant-code-examples %}

```dart
void main() {
  var x = 0;

  while (true) {
    x++;
  }

  for (var i = 0; i < 42; i++) {
    x++;
  }

  for (var item in [1, 2, 3]) {
    x += item;
  }

  if (true) {
    x++;
  } else {
    x--;
  }

  if (true) {
    x++;
  } else if(true) {
    x--;
  }

  do {
    x++;
  } while (x < 10);
}
```
  Seamless integrations. Try Datadog Code SecurityDatadog Code Security 
{% icon name="icon-external-link" /%}
 