Lesson 3 of 8Article16 min
Control Flow and Loops
Control statements drive branching logic. Use switch when comparing one variable against multiple discrete values.
if-else and switch
Control statements drive branching logic. Use switch when comparing one variable against multiple discrete values.
Conditional logic
int marks = 82;
if (marks >= 90) {
printf("Grade A\n");
} else if (marks >= 75) {
printf("Grade B\n");
} else {
printf("Grade C\n");
}for, while, and do-while
- for is best when iteration count is known.
- while suits condition-driven loops.
- do-while runs at least once.
Loop examples
for (int i = 0; i < 3; i++) {
printf("for: %d\n", i);
}
int count = 0;
while (count < 3) {
printf("while: %d\n", count);
count++;
}