Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.
C Control Statements Explained
Learn how C control statements decide which code runs, and how compound statements (blocks) group code together. A hands-on lecture from our free C programming course for embedded systems.
Every useful program makes decisions. A thermostat firmware decides when to switch the heater on, a bootloader decides whether to jump to the application, and a UART driver decides when a buffer is full. In C, these decisions are written with C control statements. This lecture opens Chapter 5 of our free C programming course, a free C tutorial written for students who want to use C programming for embedded systems. By the end you will know the four families of control statements and you will understand the compound statement, the humble pair of curly braces that every other control statement depends on.
What You Will Learn
- Why a program needs control statements at all
- The four kinds of C control statements: if…else, goto, switch and loops
- What a compound statement (block) is and how to write one
- How blocks create a new scope for variables
- Why missing braces cause some of the most common bugs in embedded C programming
- How to compile and run every example on your own Linux machine
Prerequisites
- Basic structure of a C program (
main, statements, semicolons) - Variables, data types and operators from the earlier chapters of this free embedded C course
- A C compiler such as
gcc(every example here was written for C11)
Why C Control Statements Matter
By default, a C program runs top to bottom. The CPU fetches the first statement, executes it, moves to the next one, and so on until main ends. This is called sequential execution. Sequential code is fine for a calculator that adds two numbers, but real programs need two extra abilities:
- Selection: run a part of the program only when a condition is true.
- Repetition: run a part of the program many times.
Control statements give you both. They define the order in which instructions execute, which is also called the flow of control. When you read firmware, tracing the flow of control is the main skill you use to understand what the code does.
The Four Types of C Control Statements
The C language gives you four families of control statements. This chapter covers them one by one, so treat the table below as a map of where we are going.
| Control statement | What it does | Typical embedded use |
|---|---|---|
if...else | Chooses between two paths based on a condition | Check if a sensor value crossed a limit |
switch | Chooses one path out of many based on a value | Command decoder, state machine |
while, do...while, for | Repeats a block of code | Polling a status flag, filling a buffer |
goto | Jumps to a labelled statement | Single exit path for error cleanup |
Good to know
Two of the four families (if...else and switch) are selection statements. The loops are repetition statements. goto is an unconditional jump. Almost every piece of firmware you will read uses the first three families daily, while goto is used rarely and carefully.
Compound Statement (Block) in C
A compound statement, usually just called a block, is a group of statements wrapped in a pair of curly braces. The statements inside run one after another, exactly like normal sequential code. The general form is:
{
statement_1;
statement_2;
statement_3;
}
The important rule is this: a compound statement is treated by the compiler as a single statement. Anywhere C allows one statement, you can put a whole block instead. This is the reason if, while and for can control several lines of code even though their syntax only mentions “a statement”.
Example 1: A Plain Block
A block can appear on its own, although you will rarely write it this way. It is a good way to see the syntax before we attach it to an if.
/* cp5_1_block.c : a standalone compound statement */
#include <stdio.h>
int main(void)
{
{
int length = 4;
int width = 2;
int area = length * width;
printf("Area = %d\n", area);
}
return 0;
}
Build and run it:
$ gcc -Wall -Wextra -std=c11 cp5_1_block.c -o cp5_1_block
$ ./cp5_1_block
Area = 8
Example 2: Blocks Create Scope
Variables declared inside a block exist only inside that block. This is called block scope. When the closing brace is reached, those variables are gone. If an inner block declares a variable with the same name as an outer one, the inner name hides the outer one until the block ends.
/* cp5_1_scope.c : block scope and name hiding */
#include <stdio.h>
int main(void)
{
int level = 10;
{
int level = 99; /* hides the outer 'level' */
printf("Inner level = %d\n", level);
}
printf("Outer level = %d\n", level);
return 0;
}
$ gcc -Wall -Wextra -std=c11 cp5_1_scope.c -o cp5_1_scope
$ ./cp5_1_scope
Inner level = 99
Outer level = 10
Example 3: Blocks Make if Statements Powerful
The most common place you will use a block is right after a control statement. Here a temperature check needs to do two things when it triggers, so both statements go inside braces.
/* cp5_1_overheat.c : a block controlled by an if */
#include <stdio.h>
int main(void)
{
int temp = 90;
if (temp > 80) {
printf("Overheat detected\n");
printf("Fan switched ON\n");
}
printf("Check complete\n");
return 0;
}
$ ./cp5_1_overheat
Overheat detected
Fan switched ON
Check complete
Example 4: Grouping Register Updates
In C programming for embedded systems, a block often groups a set of related hardware register updates. The program below simulates a control register held in a variable, so you can run it on a PC without any board.
/* cp5_1_register.c : simulated register setup block */
#include <stdio.h>
#include <stdint.h>
int main(void)
{
uint8_t ctrl = 0x00;
{
ctrl |= (1u << 0); /* enable peripheral */
ctrl |= (1u << 3); /* enable interrupt */
ctrl &= (uint8_t)~(1u << 1); /* make sure bit 1 = 0 */
}
printf("CTRL = 0x%02X\n", ctrl);
return 0;
}
$ ./cp5_1_register
CTRL = 0x09
Bit 0 and bit 3 are set, so the value is binary 1001, which is 0x09.
The Missing Braces Bug in C Control Statements
This is the most important practical lesson of the lecture. If you leave out the braces, the control statement only governs one statement. The second line is not part of the condition, no matter how you indent it.
/* cp5_1_nobrace.c : indentation does NOT create a block */
#include <stdio.h>
int main(void)
{
int temp = 50;
if (temp > 80)
printf("Overheat detected\n");
printf("Fan switched ON\n"); /* always runs! */
return 0;
}
$ ./cp5_1_nobrace
Fan switched ON
The temperature is only 50, yet the fan turns on. The compiler reads the code as an if with one statement, followed by an unrelated statement. Languages like Python use indentation to group code, but C only cares about braces.
Common Mistakes with Blocks
| Mistake | What happens | Fix |
|---|---|---|
| Relying on indentation instead of braces | Only the first statement is controlled | Always write braces |
| Unbalanced braces | Compiler error such as “expected declaration at end of input” | Use an editor that matches braces |
| Adding a semicolon after the closing brace of a plain block | Harmless empty statement, but confusing | Do not add it |
| Reusing a variable name in an inner block | Outer variable silently hidden | Use distinct names |
| Using a variable after its block ended | Compiler error: undeclared identifier | Declare it in the outer block |
Best Practices for C Control Statements
- Always use braces, even for a single statement. Safety coding standards such as MISRA C require this for a reason: a later edit that adds a second line cannot silently break the logic.
- Keep blocks short. If a block does not fit on one screen, move the code into a function.
- Declare variables close to where they are used inside the smallest block that needs them. C99 and later allow declarations anywhere in a block.
- Be consistent. Pick one brace style for the whole project and keep to it.
- Compile with warnings on.
-Wall -Wextracatches many control-flow mistakes. Newer GCC versions also offer-Wmisleading-indentation, which would have warned about the bug above.
Embedded tip
Small microcontrollers have little stack memory. Variables declared in a block normally share stack space with other blocks, so keeping variables in narrow blocks is a free way to write tidy code. Do not assume it always saves RAM, because the compiler decides the final stack layout.
Summary and Key Takeaways
- Control statements decide the order in which instructions run, which is the flow of control.
- C offers four types:
if...else,goto,switchand loops (while,do...while,for). - A compound statement is a group of statements inside
{ }and acts as a single statement. - A block has its own scope. Variables declared inside disappear at the closing brace.
- Indentation means nothing to the compiler. Only braces group statements.
With blocks understood, you are ready for the first and most used control statement. The next lecture of this free embedded C course covers if and if...else in detail, including how C decides what is true and what is false.
Quick Quiz
Try each question first, then open the answer.
Q1. How many statements does the compiler see in { a = 1; b = 2; }?
One. A compound statement is treated as a single statement.
Q2. What is the scope of a variable declared inside a block?
From its declaration to the closing brace of that block.
Q3. In the missing braces example, why does the fan message print when temp is 50?
Without braces the if controls only the first printf. The second one is a normal statement and always runs.
Q4. Name the four control statement families in C.
if...else, goto, switch and loops.
Interview Questions
1. What is a compound statement in C?
A sequence of statements and declarations enclosed in curly braces. It can be used wherever a single statement is allowed.
2. Does indentation affect program flow in C?
No. Only braces and statement boundaries matter to the compiler. Misleading indentation is a classic source of bugs.
3. Why do coding standards ask for braces around single statements?
So future edits cannot accidentally leave a statement outside the control structure. It removes a whole class of bugs.
4. Can an inner block redeclare a variable from an outer block?
Yes, and it hides the outer one inside that block. It is legal but discouraged.
5. What is the difference between selection and repetition statements?
Selection statements choose which code runs once. Repetition statements run the same code several times.
Frequently Asked Questions
What are control statements in C?
They are statements that change the normal top-to-bottom flow of a program. C has if…else, switch, loops and goto.
What is a block in C programming?
A block is a group of statements inside curly braces that the compiler treats as one statement.
Do I need braces for a single statement after if?
The language does not require them, but good practice and safety standards strongly recommend them.
Is this a free C programming course?
Yes. EmbeddedPathashala offers this C programming course free, with examples aimed at embedded systems students.
Do I need a microcontroller board to follow this free C tutorial?
No. Every example runs on a normal Linux PC using gcc. Later lectures show how the same ideas apply on embedded targets.
Is goto bad in embedded C programming?
Not always. Overusing it makes code hard to follow, but a single cleanup label for error handling is a common and accepted pattern.
Suggested Images
- Flow of control diagram: sequential, selection and repetition. ALT: “C control statements flow of control in a free C programming course”
- Block scope illustration with nested braces. ALT: “compound statement block scope in embedded C programming”
- Terminal screenshot of the missing braces bug. ALT: “missing braces bug in a free C tutorial for embedded systems”
Continue Your Free Embedded C Course
Next up: the if and if…else statements. Keep building your skills with the free C programming course from EmbeddedPathashala.
Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.

1 Comment