Code block: Difference between revisions
From Coderwiki
More actions
new: info, uses, evaluation |
(No difference)
|
Latest revision as of 13:53, 21 August 2025
A code block is a section of source code that is isolated in some way from other parts of the program.
This often means that variables that are declared inside the code block cannot be referenced outside the code block. But code blocks can have other functions.
Uses of code blocks
edit edit source- Declaring the body of a subroutine
- Declaring the body of an if statement, switch statement or other conditional statement
- Declaring the body of a while loop, for loop or other iterative statement
Evaluation
edit edit sourceIn some languages such as Rust, code blocks have another use: to evaluate multiple statements down to a single value.
Usually, the last statement in the code block is taken to be the value of the code block itself. Code blocks do not always evaluate to a value though.