Control System in C [Lecture 27]

Опубликовано: 15 Май 2026
на канале: Master C Programming with M. Z. Rub
272
2

n C programming, control statements are used to control the flow of execution in a program based on certain conditions or criteria. These statements allow you to make decisions, perform repetitive tasks, and alter the normal sequential flow of execution. The three main types of control statements in C are:

Conditional Statements:
if statement: It is used to execute a block of code only if a specified condition is true.
if-else statement: It allows you to execute one block of code if the condition is true, and another block if the condition is false.
nested if-else statement: You can have multiple if-else statements nested inside one another to handle more complex conditions.
Example:

c
Copy code
int num = 10;
if (num grtr than 0) {
printf("Number is positive\n");
}
else if (num less than 0) {
printf("Number is negative\n");
}
else {
printf("Number is zero\n");
}
Looping Statements:
while loop: It repeatedly executes a block of code as long as the given condition remains true.
do-while loop: It is similar to the while loop, but it guarantees the execution of the block of code at least once before checking the condition.
for loop: It allows you to specify the initialization, condition, and increment/decrement in a single line, and repeatedly executes the block of code until the condition becomes false.
break statement: It is used to exit the current loop or switch statement.
continue statement: It is used to skip the remaining statements in the current iteration of a loop and move to the next iteration.
Example:

c
Copy code
int i;
for (i = 1; i less= 5; i++) {
printf("%d\n", i);
}

int j = 0;
while (j less than 10) {
if (j == 5) {
j++;
continue;
}
printf("%d\n", j);
j++;
}

int k = 0;
do {
printf("%d\n", k);
k++;
} while (k less than 5);
Switch Statement:
switch statement: It allows you to select one of several code blocks to execute based on the value of a variable or an expression.
case labels: They are used within the switch statement to define specific values or ranges of values to match against the switch expression.
default label: It is used to specify the block of code to be executed if none of the case labels match the switch expression.
Example:

c
Copy code
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Good job!\n");
break;
case 'C':
printf("Well done!\n");
break;
default:
printf("Invalid grade\n");
}
These control statements provide powerful tools for controlling the flow of execution in a C program based on specific conditions or criteria.