When one decision needs several actions
Braces turn several statements into one, and nested decisions turn into a readable ladder — if you write the algorithm before the code.
After this lesson
You should be able to
- Use a compound statement so that a branch controls more than one action.
- Write an else if ladder for mutually exclusive cases.
- Turn a decision step written in words into correct C.
Braces make many statements into one
An if controls exactly one statement. To control several, wrap them in braces — that block is then a single compound statement. Without braces only the first line belongs to the if; the rest run unconditionally, no matter how they are indented.
C does not care about indentation; the compiler reads braces. This is the bug that looks correct on screen and behaves wrongly at runtime, so always use braces when a branch has more than one line.
if (marks >= 35) {
printf("Result: Pass\n");
passed = passed + 1;
} else {
printf("Result: Fail\n");
failed = failed + 1;
}The else if ladder
For grades you need several mutually exclusive ranges. An else if ladder tests them in order and stops at the first match, so each test only needs to rule out what the tests above already handled — marks >= 70 is enough, because anything 80 or above was already caught.
Order is the whole design. If you put marks >= 35 first, every passing student gets the lowest grade, because the first true test wins and the rest are never reached.
if (marks >= 80)
grade = 'A';
else if (marks >= 70)
grade = 'B';
else if (marks >= 60)
grade = 'C';
else if (marks >= 35)
grade = 'D';
else
grade = 'F';Write the decision step before the code
A decision step is the plain-language version of a branch: if the marks are below 35, the student fails; otherwise compute the grade band. Writing that sentence first is not busywork — it is where you notice a missing case, and fixing it in one sentence is far cheaper than fixing it in twenty lines.
The order of your written steps becomes the order of your ladder. If the sentences are ambiguous about which case wins, the code will be too.
Try it yourself
Read marks out of 100 and print both the grade and a remark. Reject any value below 0 or above 100 before grading.
Need a hint?
Validate first and stop. Two actions per branch means you need braces.
Check the worked solution
Validation goes first so the grading ladder can assume clean input — that separation is what keeps the ladder readable. Each branch does two things, so braces are required; without them only the printf would be conditional and every student would be counted as distinction.
#include <stdio.h>
int main(void)
{
int marks;
char grade;
printf("Enter marks (0-100): ");
scanf("%d", &marks);
if (marks < 0 || marks > 100) {
printf("Invalid marks\n");
return 1;
}
if (marks >= 80) {
grade = 'A';
printf("Distinction\n");
} else if (marks >= 35) {
grade = 'P';
printf("Passed\n");
} else {
grade = 'F';
printf("Needs another attempt\n");
}
printf("Grade: %c\n", grade);
return 0;
}Quick check
Without braces, how many statements does an if control?
Why this lesson exists
Syllabus mapping
if Statements with Compound Statements · Decision Steps in Algorithms · Control Structures
Maps to course outcomes CO1, CO2, CO4.