C PROGRAMMING NOTES, IOE
5.1. Introduction to Simple and Compound Statement
In C programming, a statement or an instruction is a basic building block of a program that expresses an action to be carried out. There are two main types of statements: simple statements and compound statements.
Simple Statement:
A simple statement is the most basic type of statement that performs a single action. It typically ends with a semicolon (;).
Example:
#include <stdio.h>
int main() {
int x = 5; // Simple statement assigning a value to a variable
printf("The value of x is %d\n", x); // Simple statement using printf
return 0; // Simple statement with a return statement
}
Compound Statement (or Block):
A compound statement, also known as a block, is a group of simple statements enclosed in curly braces {}. It allows multiple statements to be grouped together and treated as a single unit.
Example:
#include <stdio.h>
int main() {
int x = 5; // Simple statement
{
// Compound statement (block) begins
int y = 10; // Simple statement within the block
printf("The sum of x and y is %d\n", x + y); // Another simple statement within the block
// Compound statement (block) ends
}
// y is not accessible here because it is declared within the block
return 0;
}