Control Statements in C
Introduction:
Control statements are fundamental constructs in programming languages like C that enable developers to control the flow of execution based on certain conditions or loops. These statements provide decision-making and looping capabilities, allowing programmers to create flexible and interactive programs. In this article, we will explore various control statements in C, including if-else, switch-case, and loop statements, along with examples to demonstrate their usage.
1. If-Else Statement: Making Decisions Based on Conditions
The if-else statement is used to make decisions in a program based on the evaluation of a condition. It allows the program to execute different blocks of code depending on whether the condition is true or false.
Example Code:
#include int main() {
int num = 10;
if (num > 0) {
printf("The number is positive.\n");
}
else {
printf("The number is not positive.\n");
}
return 0;
}
Output:
“`
The number is positive.
“`
In the above example, the if condition checks whether the variable `num` is greater than 0. Since the condition is true, the code within the if block is executed, resulting in the corresponding output.
2. Switch-Case Statement: Handling Multiple Conditions
The switch-case statement allows programmers to evaluate an expression against multiple possible values. It provides a concise way to handle multiple conditions without using multiple if-else statements.
Example Code:
#include int main() {
int choice;
printf("Enter a number between 1 and 3: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("You entered 1.\n");
break;
case 2:
printf("You entered 2.\n");
break;
case 3:
printf("You entered 3.\n");
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
Output 1:
“`
Enter a number between 1 and 3: 2
You entered 2.
“`
Output 2:
“`
Enter a number between 1 and 3: 4
Invalid choice.
“`
In this example, the user is prompted to enter a number between 1 and 3. The switch statement evaluates the value of `choice` and executes the corresponding case block. If none of the cases match, the default block is executed.
3. Loop Statements: Repeating Execution
Loop statements allow for the repeated execution of a block of code as long as a certain condition is satisfied. C provides three types of loop statements: while, do-while, and for loops.
Example Code:
#include int main() {
int i;
printf("Counting from 1 to 5 using a while loop:\n");
i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
printf("\n");
printf("Counting from 1 to 5 using a do-while loop:\n");
i = 1;
do {
printf("%d ", i);
i++; }
while (i <= 5);
printf("\n");
printf("Counting from 1 to 5 using a for loop:\n");
for (i = 1; i <= 5; i++) {
printf("%d ", i); }
printf("\n");
return 0;
}
Output:
“`
Counting from 1 to 5 using a while loop:
1 2 3 4 5
Counting from 1 to 5 using a do-while loop:
1 2 3 4 5
Counting from 1 to 5 using a for loop:
1 2 3 4 5
“`
In this example, three different loops are used to count from 1 to 5. The while loop, do-while loop, and for loop all accomplish the same task, but with different syntax.
Control Statements in C
Conclusion:
Control statements are essential in C programming as they allow programmers to control the flow of execution based on conditions and loops. In this article, we explored if-else, switch-case, and loop statements, providing examples to illustrate their usage. By mastering control statements, developers can create more interactive and dynamic programs that respond to specific conditions and repeat tasks efficiently. Understanding and utilizing control statements effectively is a fundamental skill for any C programmer.