Control structures in C are used to control the flow of execution in a program. They help in making decisions, repeating tasks, and branching to different sections of code based on conditions. C provides several types of control structures that allow developers to control the execution path. These are:
Let's dive into each of these in detail:
These structures allow the program to make decisions and execute different blocks of code based on conditions. The most common decision-making structures in C are:
if StatementThe if statement is used to test a condition. If the condition evaluates to true, the block of code inside the if statement is executed.
Syntax:
if (condition) {
// code to be executed if the condition is true
}
Example:
int x = 10;
if (x > 5) {
printf("x is greater than 5\n");
}
Output: x is greater than 5
if-else StatementThe if-else statement allows you to execute one block of code if the condition is true and another block if it is false.
Syntax:
if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
Example:
int x = 3;
if (x > 5) {
printf("x is greater than 5\n");
} else {
printf("x is less than or equal to 5\n");
}
Output: x is less than or equal to 5
if-else if-else StatementThe if-else if-else statement is used when there are multiple conditions to check. It allows you to test multiple conditions sequentially.
Syntax:
if (condition1) {
// code if condition1 is true
} else if (condition2) {
// code if condition2 is true
} else {
// code if none of the above conditions are true
}
Example:
int x = 8;
if (x > 10) {
printf("x is greater than 10\n");
} else if (x == 8) {
printf("x is equal to 8\n");
} else {
printf("x is less than 8\n");
}
Output: x is equal to 8
switch StatementThe switch statement is used when you have multiple possible values for a variable, and you want to choose among them. It is typically more efficient than using multiple if-else conditions for the same variable.
Syntax:
switch (expression) {
case constant1:
// code to be executed if expression equals constant1
break;
case constant2:
// code to be executed if expression equals constant2
break;
default:
// code to be executed if no case matches
}
Example:
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day\n");
}
Output: Wednesday
Looping structures are used to repeat a block of code multiple times as long as a certain condition is met. C provides several looping structures:
for LoopThe for loop is typically used when the number of iterations is known beforehand. It consists of three parts: initialization, condition, and update.
Syntax:
for (initialization; condition; update) {
// code to be executed
}
Example:
for (int i = 1; i <= 5; i++) {
printf("i = %d\n", i);
}
Output:
i = 1
i = 2
i = 3
i = 4
i = 5
while LoopThe while loop is used when the number of iterations is not known in advance, but the loop continues as long as a condition is true.
Syntax:
while (condition) {
// code to be executed
}
Example:
int i = 1;
while (i <= 5) {
printf("i = %d\n", i);
i++;
}
Output:
i = 1
i = 2
i = 3
i = 4
i = 5
do-while LoopThe do-while loop is similar to the while loop, but it guarantees that the code block will be executed at least once, regardless of the condition.
Syntax:
do {
// code to be executed
} while (condition);
Example:
int i = 1;
do {
printf("i = %d\n", i);
i++;
} while (i <= 5);
Output:
i = 1
i = 2
i = 3
i = 4
i = 5
Jumping structures allow you to control the flow of execution by jumping to different parts of the code. The most commonly used jumping structures in C are break, continue, and goto.
break StatementThe break statement is used to exit from a loop or switch statement prematurely. It immediately terminates the loop or switch.
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exit the loop when i is 5
}
printf("i = %d\n", i);
}
Output:
i = 1
i = 2
i = 3
i = 4
continue StatementThe continue statement skips the current iteration of a loop and moves to the next iteration. It does not terminate the loop, but it prevents the remaining code in the current iteration from executing.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip the iteration when i is 3
}
printf("i = %d\n", i);
}
Output:
i = 1
i = 2
i = 4
i = 5
goto StatementThe goto statement is used to transfer control to a specific label in the program. While it allows you to jump to any part of the program, its use is generally discouraged because it can make code harder to read and maintain.
Syntax:
goto label_name;
label_name:
// code to jump to
Example:
int i = 0;
start:
printf("i = %d\n", i);
i++;
if (i < 5)
goto start;
Output:
i = 0
i = 1
i = 2
i = 3
i = 4
Decision-Making Structures:
if, if-else, if-else if-else, switchLooping Structures:
for, while, do-whileJumping Structures:
break, continue, gotoControl structures are fundamental to programming in C as they determine the flow of control and allow you to make decisions, repeat actions, and jump to different parts of your program. By using these control structures effectively, you can create complex, efficient, and well-organized programs.
Open this section to load past papers