Control Statements in C - Quick Notes
1. Simple if Statement:
Executes code if condition is true.
Example:
if (age >= 18) {
printf("Eligible to vote");
2. if...else Statement:
Executes one block if condition is true, another if false.
Example:
if (age >= 18) {
printf("Adult");
} else {
printf("Minor");
3. Nested if...else:
if...else inside another if...else.
Example:
if (marks >= 90) {
printf("Grade A");
} else {
if (marks >= 75) {
printf("Grade B");
4. else if Ladder:
Multiple conditions in sequence.
Example:
Control Statements in C - Quick Notes
if (marks >= 90)
printf("A");
else if (marks >= 75)
printf("B");
else
printf("C");
5. switch Statement:
Selects block based on variable value.
Example:
switch (day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
default: printf("Other day");
6. goto Statement:
Jumps to a labeled part of code.
Example:
goto end;
// code skipped
end: printf("Jumped here");
7. Entry vs Exit Control Loop:
Entry control: condition checked before (while, for).
Exit control: condition checked after (do...while).
8. while Loop:
Repeats while condition is true.
Example:
int i = 1;
Control Statements in C - Quick Notes
while (i <= 5) {
printf("%d", i);
i++;
9. for Loop:
Loop with init, condition, update in one line.
Example:
for (int i = 1; i <= 5; i++) {
printf("%d", i);
10. Nested for Loops:
for loop inside another.
Example:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
printf("%d %d\n", i, j);
11. do...while Loop:
Executes at least once.
Example:
int i = 1;
do {
printf("%d", i);
i++;
} while (i <= 5);
12. break and continue:
Control Statements in C - Quick Notes
break: exits loop early.
continue: skips to next iteration.
Example:
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
if (i == 5) break;
printf("%d", i);