๐Ÿงฑ C Blocks and Statements

In C programming, a statement is a single line of code that performs a specific task โ€” like declaring a variable or calling a function. A block is a group of statements enclosed in curly braces { }.

Blocks are commonly used to group statements in functions, conditionals (if), loops (for, while), and more.

๐Ÿ”น What is a Statement in C?

A statement performs a task and ends with a semicolon (;). For example:

  • int a = 5; โ† declares and assigns a variable
  • printf("Hello"); โ† prints a message

๐Ÿ”น What is a Block in C?

A block is a group of statements inside { }. It allows multiple lines of code to be treated as one unit.

๐Ÿ“ Example: Block Inside If Statement

This example shows how a block groups two statements inside an if condition.

#include <stdio.h>

int main() {
    int number = 10;

    if (number > 5) {
        printf("The number is greater than 5.\n");
        printf("This is part of the block.\n");
    }

    return 0;
}
  

Try It Now

๐Ÿ“ Example: Block Inside a Loop

This block executes multiple statements during each loop iteration.

#include <stdio.h>

int main() {
    for (int i = 1; i <= 3; i++) {
        printf("Iteration %d\n", i);
        printf("This is a statement inside the loop block.\n");
    }

    return 0;
}
  

Try It Now

๐ŸŽฏ Why Use Blocks?

  • Blocks allow you to run multiple statements under one control structure (like if, for, while).
  • They make code cleaner, more readable, and structured.
  • Every function in C is also a block!

๐Ÿงช Practice Time

Try adding an else block to the if example. Play with different conditions and see how blocks behave!