Control Flow
## Learning Objectives
- Master if, else if, else statements
- Understand switch statements
- Learn for, while, do-while loops
- Use break and continue
## if Statement
### Basic if
```c
int age = 18;
if (age >= 18) {
printf("Adult\n");
}
```
### if-else
```c
int age = 15;
if (age >= 18) {
printf("Adult\n");
} else {
printf("Minor\n");
}
```
### if-else if-else
```c
int score = 85;
char grade;
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
} else if (score >= 60) {
grade = 'D';
} else {
grade = 'F';
}
printf("Grade: %c\n", grade);
```
## Ternary Operator
```c
int age = 20;
int status = (age >= 18) ? 1 : 0;
```
## switch Statement
### Basic switch
```c
int day = 3;
char *dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
case 7:
dayName = "Sunday";
break;
default:
dayName = "Invalid";
}
printf("%s\n", dayName);
```
### Fall-through
```c
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
days = 28;
break;
}
```
## for Loop
### Basic for
```c
for (int i = 0; i < 5; i++) {
printf("%d\n", i); // 0, 1, 2, 3, 4
}
```
### Loop Components
```c
// for (init; condition; increment)
for (int i = 0; i < 5; i++) {
// body
}
```
Is equivalent to:
```c
int i = 0; // init
while (i < 5) { // condition
// body
i++; // increment
}
```
### Infinite Loop
```c
for (;;) {
// Runs forever
}
while (1) {
// Also runs forever
}
```
### Comma Operator in for
```c
for (int i = 0, j = 10; i < 5; i++, j--) {
printf("%d - %d\n", i, j);
}
```
## while Loop
### Basic while
```c
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
```
### while with Condition First
```c
#include
int main(void) {
char input[100];
while (scanf("%99s", input) == 1) {
if (strcmp(input, "quit") == 0) {
break;
}
printf("You entered: %s\n", input);
}
return 0;
}
```
## do-while Loop
### Executes at Least Once
```c
int i = 0;
do {
printf("%d\n", i);
i++;
} while (i < 5);
```
### Menu Example
```c
#include
int main(void) {
int choice;
do {
printf("1. New Game\n");
printf("2. Load Game\n");
printf("3. Quit\n");
printf("Choose: ");
scanf("%d", &choice);
} while (choice < 1 || choice > 3);
return 0;
}
```
## break and continue
### break - Exit Loop
```c
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Exit when i is 5
}
printf("%d\n", i); // 0, 1, 2, 3, 4
}
```
### continue - Skip Iteration
```c
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue; // Skip when i is 2
}
printf("%d\n", i); // 0, 1, 3, 4
}
```
## Nested Loops
```c
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("(%d,%d) ", i, j);
}
printf("\n");
}
```
Output:
```text
(0,0) (0,1) (0,2)
(1,0) (1,1) (1,2)
(2,0) (2,1) (2,2)
```
### Break from Nested Loop
```c
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == 2) {
break; // Breaks inner loop only
}
printf("%d,%d\n", i, j);
}
}
```
## goto Statement
### Basic goto
```c
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j == 2) {
goto done; // Jumps to label
}
}
}
done:
printf("Found it!\n");
```
### Avoid goto
**Note:** goto makes code hard to follow. Use break/continue with flags instead.
## Common Patterns
### Sum 1 to N
```c
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
```
### Find in Array
```c
int numbers[] = {3, 7, 2, 9, 4};
int target = 9;
int found = 0;
for (int i = 0; i < 5; i++) {
if (numbers[i] == target) {
found = 1;
break;
}
}
```
### Count Matches
```c
int numbers[] = {1, 2, 3, 2, 4, 2, 5};
int target = 2;
int count = 0;
for (int i = 0; i < 7; i++) {
if (numbers[i] == target) {
count++;
}
}
```
### FizzBuzz
```c
for (int i = 1; i <= 20; i++) {
if (i % 15 == 0) {
printf("FizzBuzz\n");
} else if (i % 3 == 0) {
printf("Fizz\n");
} else if (i % 5 == 0) {
printf("Buzz\n");
} else {
printf("%d\n", i);
}
}
```
## Summary
- if/else if/else for conditional logic
- switch for multiple discrete values
- for loop for known iterations
- while loop for condition-based iteration
- do-while for at-least-once iteration
- break exits loop; continue skips iteration
- Avoid goto (makes code hard to follow)
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →