for, switch, do...while, break, continue, Logical OperatorsProgram control structures allow you to control the flow of a program by executing certain blocks of code based on conditions or repeatedly executing them. In C programming, the main program control structures include the for loop, switch statement, do...while loop, and keywords like break, continue, and logical operators. Each of these structures provides different ways to manage the execution flow, whether for repeated actions or decision-making.
Let's go through each of these program control structures and concepts in detail.
for LoopThe for loop is used for repeating a block of code a specific number of times. It’s ideal when the number of iterations is known in advance.
for (initialization; condition; increment/decrement) {
// Code to be executed
}
i = 0).i++).#include <stdio.h>
int main() {
// Print numbers from 1 to 5
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
i = 1 and runs while i <= 5.i is incremented by 1.1
2
3
4
5
switch StatementThe switch statement is used to handle multiple conditions. It is often used when you need to select one of many code blocks to execute, based on the value of a single variable.
switch (expression) {
case constant1:
// Code to be executed if expression == constant1
break;
case constant2:
// Code to be executed if expression == constant2
break;
default:
// Code to be executed if expression doesn't match any case
}
case corresponds to a possible value of the expression. If a match is found, the corresponding block is executed.break statement: Exits the switch statement after the matching case is executed.default: Optionally executed if none of the case values match.#include <stdio.h>
int main() {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
switch statement checks the value of day.day = 3, the program prints "Wednesday".break statement ensures the program exits the switch block after executing the matching case.Wednesday
do...while LoopThe do...while loop is similar to the while loop, but with a key difference: the condition is checked after the loop body is executed. This means the loop will always execute at least once, even if the condition is false initially.
do {
// Code to be executed
} while (condition);
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
return 0;
}
1
2
3
4
5
break StatementThe break statement is used to terminate the execution of a loop or switch statement immediately, even if the loop or switch condition hasn’t been satisfied.
switch statements: To stop checking other case values.#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 6) {
break; // Exit the loop when i is 6
}
printf("%d\n", i);
}
return 0;
}
i equals 6, the break statement terminates the loop early.1
2
3
4
5
continue StatementThe continue statement is used to skip the current iteration of a loop and proceed to the next iteration, without executing the remaining statements in the loop body for the current iteration.
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue; // Skip the rest of the code for i == 5
}
printf("%d\n", i);
}
return 0;
}
i == 5, the continue statement is triggered, which causes the loop to skip the printf for that iteration.1
2
3
4
6
7
8
9
10
Logical operators are used to combine multiple conditions. They evaluate expressions that return boolean values (true or false) and return true or false based on the combination of conditions.
&& (Logical AND):
|| (Logical OR):
! (Logical NOT):
#include <stdio.h>
int main() {
int a = 5, b = 10, c = 20;
if (a < b && b < c) {
printf("a is less than b, and b is less than c\n");
}
if (a > b || b < c) {
printf("Either a is greater than b, or b is less than c\n");
}
if (!(a > b)) {
printf("a is not greater than b\n");
}
return 0;
}
if checks if both a < b and b < c are true (which they are).if checks if either a > b or b < c is true (which b < c is).if uses the ! operator to check if a is not greater than b.a is less than b, and b is less than c
Either a is greater than b, or b is less than c
a is not greater than b
for loop: Ideal for when the number of iterations is known.switch statement: Used for handling multiple possible values of a single variable.do...while loop: Ensures the loop executes at least once before checking the condition.break statement: Immediately exits the loop or switch block.continue statement: Skips the current iteration of the loop and continues with the next iteration.These program control structures provide flexibility and efficiency in managing the flow of your program. Whether you're repeating tasks or making decisions, these tools are essential for writing clean and effective C programs.
Open this section to load past papers