# Code Block

> A group of lines that run as one, like the body of a loop.

**Field:** Software Development  
**Category:** Programming Concepts  
**Also known as:** Block of Code, Block (Programming), Compound Statement

## Think of it like

It is like a box of toys. You carry the whole box, not each toy one by one.

## Definition

A code block is a group of lines that run together as one unit. The body of an if, a loop or a function is a block. Many languages mark a block with curly braces, { and }. Python marks it by pushing the lines in. Variables made inside a block often live only inside that block.

## Example

In Emma's game, two lines sit in the if block. They run only when the player has no lives left.

## Example code

```javascript
if (lives === 0) {
  showGameOver();
  stopMusic();
}
```

The two lines inside the braces are one block. They run together or not at all.

## Why it matters

Blocks decide which lines belong to which choice or loop. Get the edge of a block wrong and the wrong lines run.

## Common mistake

Leaving out the braces around a one-line if, then adding a second line. Only the first line is in the block.

## Three principles

- **Runs together.** Its lines act as one unit.
- **Marked edges.** Braces or pushed-in lines show it.
- **Has its own scope.** Names made inside may stay inside.

## Related terms

- [Statement](https://learn.benibix.com/term/development/statement)
- [Scope](https://learn.benibix.com/term/development/scope)
- [Indentation](https://learn.benibix.com/term/development/indentation)
- [Conditional Statement](https://learn.benibix.com/term/development/conditional-statement)
- [Loop](https://learn.benibix.com/term/development/loop)
- [Function](https://learn.benibix.com/term/development/function)

## Taught in

- [Code Blocks and Indentation](https://learn.benibix.com/learn/development-concepts/code-blocks-and-indentation): Programming Basics, 4/18

![Three boxes, one inside the next. The outer box is a codebase holding a project folder and many files. One file, cart.py, is a module. Inside it, the lines under an if sit pushed in by four spaces, and a bracket marks them as one code block.](https://learn.benibix.com/diagrams/dev7-code-parts.svg)

---

Source: https://learn.benibix.com/term/development/code-block
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
