Control Statements
Decision making, iterative loops, and jumping statements in C
Decision Making / Conditional Statements
C programs normally execute sequentially. When conditional criteria must be evaluated to alter the flow of execution, C provides decision making statements: if, if-else, nested if-else, and switch-case.
if Statement
Checks a condition first. If true, the enclosed block executes:
#include <stdio.h>
void main() {
int a = 5;
if (a > 4) {
printf("\nValue of A is greater than 4!");
}
if (a == 4) {
printf("\nValue of A is 4!");
}
}Value of A is greater than 4!
if-else Statement
Executes the true branch if condition holds, otherwise executes the else block:
#include <stdio.h>
void main() {
int no;
printf("Enter number: ");
scanf("%d", &no);
if (no % 2 == 0) {
printf("\n\nNumber is Even!");
} else {
printf("\n\nNumber is Odd!");
}
}Enter number: 11 Number is Odd!
Nested if-else Statement
Nesting an if-else structure inside another if or else block:
#include <stdio.h>
void main() {
int no;
printf("Enter number: ");
scanf("%d", &no);
if (no % 2 == 0) {
printf("\n\nNumber is Even!");
} else {
if (no == 0) {
printf("\n\nIt is 0!");
} else {
printf("\n\nNumber is less than 0 or odd!");
}
}
}Enter number: 0 Number is Even!
switch-case Statement
A multi-way branching statement that matches an expression against constant case values to avoid deep nested if ladders:
- The case label must be an integer or character constant.
- Each compound case statement should include a break statement to prevent fall-through.
- Case labels must end with a colon (:).
#include <stdio.h>
void main() {
int no;
printf("Enter any number from 1 to 3: ");
scanf("%d", &no);
switch (no) {
case 1:
printf("\nIt is 1");
break;
case 2:
printf("\nIt is 2");
break;
case 3:
printf("\nIt is 3");
break;
default:
printf("\nInvalid Number!");
}
}Enter any number from 1 to 3: 3 It is 3 Enter any number from 1 to 3: 5 Invalid Number!
Looping Statements (Iterative)
A loop executes a block of code repeatedly as long as a test condition evaluates to true.
Entry Controlled Loop
Condition is tested before entering the loop body (e.g., while and for loops). If false initially, body never runs.
Exit Controlled Loop
Loop body executes first, then condition is evaluated (e.g., do-while loop). Always runs at least once.
while Loop (Entry Controlled)
Repeats a block of statements as long as the condition remains true:
#include <stdio.h>
void main() {
int a = 1;
while (a <= 5) {
printf("Hello\n");
a += 1; // i.e., a = a + 1
}
}Hello Hello Hello Hello Hello
for Loop (Entry Controlled)
Combines initialization, test condition, and step increment in a compact header:
#include <stdio.h>
void main() {
int i;
for (i = 0; i < 5; i++) {
printf("Hello\n");
}
}Hello Hello Hello Hello Hello
do-while Loop (Exit Controlled)
Guarantees that the loop body executes at least once before checking the condition at the end:
#include <stdio.h>
void main() {
int a = 1;
do {
printf("Hello\n");
a += 1;
} while (a <= 5);
a = 6;
do {
printf("Technowell\n");
a += 1;
} while (a <= 5);
}Hello Hello Hello Hello Hello Technowell
Break Statement
Causes immediate termination of the innermost enclosing loop or switch construct:
#include <stdio.h>
void main() {
int i;
for (i = 1; ; i++) {
if (i > 5) {
break; // Exits the loop when i exceeds 5
}
printf("%d ", i);
}
}1 2 3 4 5
Continue Statement
Skips the remaining statements in the current iteration and jumps directly to the next iteration:
#include <stdio.h>
void main() {
int i;
for (i = 1; i <= 8; i++) {
if (i == 6) {
continue; // 6 is omitted
}
printf("%d\n", i);
}
}1 2 3 4 5 7 8
Goto Statement
Transfers program execution control unconditionally to a specified label. Useful for breaking out of deeply nested loops:
#include <stdio.h>
void main() {
int i = 1, j;
while (i <= 3) {
for (j = 1; j <= 3; j++) {
printf("* ");
if (j == 2) {
goto stop; // Jump out of both loops
}
}
i = i + 1;
}
stop:
printf("\n\nExited!\n");
}* * Exited!