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 variableprintf("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;
}
📝 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;
}
🎯 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!