C Statements
What is a Statement?
A statement in C is a complete instruction for the compiler. It usually ends with a semicolon (`;`).
Statements can be simple (like assigning a value) or complex (like control flow that directs execution).
Blocks (compound statements) use `{ ... }` and are treated as a single statement; **do not** put a semicolon after a block when used with `if/for/while`.
#include <stdio.h>
int main(void) {
int x = 5; // declaration + assignment
x = x + 2; // expression statement
printf("%d\n", x);
return 0;
}
7
Expression Statements
An expression statement performs an action, such as arithmetic or function calls.
Examples include assignments (`x = 5;`), increments (`i++;`), or calls (`printf(...);`).
#include <stdio.h>
int main(void) {
int a = 10;
a++; // increment
printf("%d\n", a);
return 0;
}
11
Compound Statements (Blocks)
Compound statements group multiple instructions using `{ }`. These are useful in functions or control structures like `if` and `for`.
A block is treated as a single statement, even though it contains many lines. Do not write a semicolon after the closing brace when the block is controlled by `if/for/while`.
#include <stdio.h>
int main(void) {
{
int y = 3;
int z = 4;
printf("%d\n", y + z);
}
return 0;
}
7
Control Flow Statements
Control flow statements determine how the program executes based on conditions and loops:
- **if/else**: Decision making.
- **switch**: Multi-way branching.
- **for, while, do-while**: Loops for repetition.
#include <stdio.h>
int main(void) {
for (int i = 1; i <= 3; i++) {
printf("Iteration %d\n", i);
}
int n = 2;
switch (n) {
case 1: printf("One\n"); break;
case 2: printf("Two\n"); break;
default: printf("Other\n");
}
return 0;
}
Iteration 1 Iteration 2 Iteration 3 Two
Jump Statements (break, continue, return, goto)
`break` exits the nearest loop or `switch`; `continue` skips to the next loop iteration; `return` exits a function; `goto` jumps to a labeled statement (use sparingly; `switch` case/default labels are the most common labeled statements).
#include <stdio.h>
int main(void) {
for (int i = 1; i <= 5; i++) {
if (i == 3) continue; // skip printing 3
if (i == 5) { printf("break at %d\n", i); break; }
printf("%d ", i);
}
printf("\n");
// Labeled statement example (commonly seen with switch)
goto done;
printf("This line is skipped.\n");
done:
printf("Finished with return.\n");
return 0;
}
1 2 4 break at 5 Finished with return.
Empty (Null) Statement & Semicolon Traps
A lone semicolon `;` is a valid empty statement. It's useful for loops with empty bodies, but can cause bugs if used accidentally (e.g., after `if`).
#include <stdio.h>
int main(void) {
// Intentional empty loop body to find length index
const char *s = "hello";
int i = 0;
for (i = 0; s[i] != '\0'; i++) ; // empty body on purpose
printf("length=%d\n", i);
int flag = 1;
// BUG: stray semicolon makes the if do nothing; block always runs
if (flag); { printf("This prints regardless of flag! (buggy)\n"); }
// Correct form (no stray semicolon):
if (flag) { printf("This prints when flag is true.\n"); }
return 0;
}
length=5 This prints regardless of flag! (buggy) This prints when flag is true.
Comparison of Statement Types
The table below summarizes the main categories of statements in C.
Type | Example | Purpose |
---|---|---|
Expression | x = x + 1; | Performs a calculation or function call |
Compound (Block) | { int a=1; int b=2; } | Groups multiple statements |
Control Flow | if (x>0) { ... } | Alters execution order |
Jump | break; continue; return; | Transfers control |
Labeled | case 1: ...; default: ...; | Targets for switch/goto |
Empty (Null) | ; | Intentional no-op / loop sentinel |