Control structures in C are used to control the flow of execution in a program.
They allow you to make decisions, repeat actions, and control the sequence of
operations. The main types of control structures in C are decision-making, loops,
and branching.
1. Decision-Making Statements
Decision-making statements are used to make decisions in your program, allowing
it to choose different paths based on conditions.
1.1 if Statement
The if statement allows you to execute a block of code only if a specific condition
is true.
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("Number is positive.\n");
}
return 0;
}
In the example above, if the condition num > 0 is true, the code inside the if block
will execute.
1.2 if-else Statement
The if-else statement allows you to execute one block of code if the condition is
true and another block if the condition is false.
, #include <stdio.h>
int main() {
int num = -5;
if (num > 0) {
printf("Number is positive.\n");
} else {
printf("Number is negative or zero.\n");
}
return 0;
}
1.3 if-else if Ladder
The if-else if ladder allows you to test multiple conditions and execute different
blocks of code based on which condition is true.
#include <stdio.h>
int main() {
int num = 0;
if (num > 0) {
printf("Number is positive.\n");
} else if (num < 0) {
printf("Number is negative.\n");
} else {
printf("Number is zero.\n");
}
return 0;
}