Error Handling in C
Ensure robust C programs — learn error detection with return codes, errno, perror/strerror, assertions, memory and file I/O error handling with clear examples.
Explore C control structures — master if, if-else, switch, loops (for, while, do-while), nested and jump statements with clear syntax and practical examples.
Control structures are essential components of programming that determine the flow of execution in a program. They allow developers to make decisions, repeat actions, and control the logical sequence of operations. The C programming language provides a range of control structures that enable programmers to create structured and efficient code. In this article, we explore the various control structures in C with illustrative examples.
Table of contents [Show]
Conditional statements are used to make decisions based on certain conditions. The most common conditional statement in C is the if statement.
#include
int main() {
int num = 10;
if (num > 5) {
printf("The number is greater than 5.\n");
} else {
printf("The number is not greater than 5.\n");
}
return 0;
}
Loops are used to repeatedly execute a block of code as long as a certain condition is met. The two main types of loops in C are for loops and while loops.
#include
int main() {
for (int i = 1; i <= 5; i++) {
printf("Iteration %d\n", i);
}
return 0;
}
#include
int main() {
int count = 0;
while (count < 5) {
printf("Count: %d\n", count);
count++;
}
return 0;
}
#include
int main() {
int count = 0;
do {
printf("Count: %d\n", count);
count++;
}while (count < 5);
return 0;
}
| Sr. No | For loop | While loop | Do while loop |
|---|---|---|---|
| 1. | Syntax: For(initialization; condition;updating), { . Statements; } | Syntax: While(condition), { . Statements; . } | Syntax: Do { . Statements; } While(condition); |
| 2. | It is known as entry controlled loop | It is known as entry controlled loop. | It is known as exit controlled loop. |
| 3. | If the condition is not true first time than control will never enter in a loop | If the condition is not true first time than control will never enter in a loop. | Even if the condition is not true for the first time the control will enter in a loop. |
| 4. | There is no semicolon; after the condition in the syntax of the for loop. | There is no semicolon; after the condition in the syntax of the while loop. | There is semicolon; after the condition in the syntax of the do while loop. |
| 5. | Initialization and updating is the part of the syntax. | Initialization and updating is not the part of the syntax. | Initialization and updating is not the part of the syntax |
| 6. | For loop is used when we know the number of iterations means where the loop will terminate. | While loop is used when we don't know the number of iterations means where the loop will terminate. | Do while loop is used when we don't know the number of iterations means where the loop will terminate. |
The switch statement is used to select one of many code blocks to be executed, based on the value of an expression.
#include
int main() {
int day = 3;
switch (day) {
case 1:
printf("Sunday\n");
break;
case 2:
printf("Monday\n");
break;
case 3:
printf("Tuesday\n");
break;
default:
printf("Invalid day\n");
break;
}
return 0;
}
Control statements alter the normal flow of execution by allowing you to exit loops or skip iterations.
#include
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
printf("Iteration %d\n", i);
}
return 0;
}
#include
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
printf("Iteration %d\n", i);
}
return 0;
}
Control structures can be nested within each other to create complex logic flows.
#include
int main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Control structures are the building blocks of structured programming in the C language. They provide the means to implement decision-making, looping, and altering program execution. By combining different control structures, developers can create sophisticated programs that perform tasks ranging from simple calculations to intricate algorithmic operations. Understanding and effectively utilizing control structures is essential for writing efficient, readable, and functional C code that meets various programming challenges.
Ensure robust C programs — learn error detection with return codes, errno, perror/strerror, assertions, memory and file I/O error handling with clear examples.
Unlock flexibility in C—learn how to implement variadic functions using stdarg.h: handling ellipses (...), va_list, va_start, va_arg, va_end, with clear examples.
Unlock the power of C preprocessors — learn directives like #include, #define, macros, conditional compilation, and file inclusion for cleaner, flexible code.
These cookies are essential for the website to function properly.
These cookies help us understand how visitors interact with the website.
These cookies are used to deliver personalized advertisements.


