2.4) Control structures
Control structures in C are essential for directing the flow of execution in a program. They allow you to make decisions, perform repetitive tasks, and create logical structures.
C provides three primary types of control structures: selection structures, iteration structures, and jump structures.
Table of Contents
Selection Structures
Selection structures are used to make decisions based on conditions. The most common selection structure is the if
statement, which allows you to execute a block of code if a given condition is true.
if Statement
if (condition) {
// Code to be executed if the condition is true
}
#include <stdio.h>
int main() {
int num = 7;
if (num % 2 == 0) {
printf("Number is even.\n");
}
return 0;
}
if-else Statement
if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
#include <stdio.h>
int main() {
int age = 18;
if (age >= 18) {
printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote.\n");
}
return 0;
}
if-else if Ladder
if (condition1) {
// Code to be executed if condition1 is true
} else if (condition2) {
// Code to be executed if condition2 is true
} else {
// Code to be executed if none of the conditions is true
}
Iteration Structures
Iteration structures, also known as loops, allow you to execute a block of code repeatedly as long as a given condition is true. C provides three types of loops: while
, for
, and do-while
.
while Loop
while (condition) {
// Code to be executed repeatedly while the condition is true
}
#include <stdio.h>
int main() {
int count = 0;
while (count < 5) {
printf("Count: %d\n", count);
count++;
}
return 0;
}
for Loop
for (initialization; condition; increment/decrement) {
// Code to be executed repeatedly as long as the condition is true
}
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("Square of %d is %d\n", i, i * i);
}
return 0;
}
do-while Loop
do {
// Code to be executed at least once and then repeatedly while the condition is true
} while (condition);
#include <stdio.h>
int main() {
int num;
do {
printf("Enter a positive number: ");
scanf("%d", &num);
} while (num <= 0);
printf("You entered a positive number: %d\n", num);
return 0;
}
Jump Structures
Jump structures allow you to control the flow of execution by transferring control to specific parts of your program. The primary jump structures are break
, continue
, and return
.
break Statement
The break
statement is used to exit a loop prematurely, regardless of whether the loop condition is satisfied.
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 6) {
break; // Exit the loop when i becomes 6
}
printf("%d\n", i);
}
return 0;
}
continue Statement
The continue
statement is used to skip the rest of the loop’s body for the current iteration and proceed to the next iteration.
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip iteration when i is 3
}
printf("%d\n", i);
}
return 0;
}
return Statement
The return
statement is used to exit a function and return a value to the calling code. It can also be used to end the execution of a program.
switch Statement
switch (expression) {
case value1:
// Code to be executed if expression is equal to value1
break;
case value2:
// Code to be executed if expression is equal to value2
break;
// More cases...
default:
// Code to be executed if none of the cases match
}
#include <stdio.h>
int main() {
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Good job!\n");
break;
case 'C':
printf("Passing grade.\n");
break;
default:
printf("You need to work harder.\n");
}
return 0;
}
By using control structures effectively, you can create programs that respond dynamically to different situations, handle data efficiently, and ensure logical and organized execution paths.