Skip to content
BENIBIX Learn

Code Block

Development🧩 Programming ConceptsPrograms

Also known as Block of Code, Block (Programming), Compound Statement

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

Think of it like

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

Example

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

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.

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 code

javascript
if (lives === 0) {
  showGameOver();
  stopMusic();
}
The two lines inside the braces are one block. They run together or not at all.
The inner box is a code block. Its lines run together under the if.

Common mistake

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

Key takeaways

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

Learn it properly