In the C programming language, decision control structures allow you to control the flow of execution based on specific conditions. These structures help make decisions and choose different paths of execution within your program. There are three main decision control structures in C: if statement, if-else statement, and switch statement. Let's explore each one:
1. if Statement:
The if statement allows you to execute a block of code if a condition is true. It has the following syntax:
```c
if (condition) {
// code to execute if the condition is true
}
```
Example:
```c
int x = 5;
if (x > 0) {
printf("x is positive");
}
```
2. if-else Statement:
The if-else statement provides an alternative block of code to execute if the condition is false. It has the following syntax:
```c
if (condition) {
// code to execute if the condition is true
}
else {
// code to execute if the condition is false
}
```
Example:
```c
int x = -5;
if (x > 0) {
printf("x is positive");
}
else {
printf("x is non-positive");
}
```
3. switch Statement:
The switch statement allows you to choose from multiple options based on the value of an expression. It has the following syntax:
```c
switch (expression) {
case value1:
// code to execute if expression matches value1
break;
case value2:
// code to execute if expression matches value2
break;
// more cases...
default:
// code to execute if expression does not match any case
}
```
Example:
```c
int choice = 2;
switch (choice) {
case 1:
printf("Option 1 selected");
break;
case 2:
printf("Option 2 selected");
break;
default:
printf("Invalid option");
}
```
These decision control structures allow you to write code that can make decisions based on conditions and choose different paths of execution. By utilizing these structures, you can control the flow of your program and make it more dynamic and responsive to different scenarios.
Certainly! Here are examples of decision control structures in C, starting from basic to more advanced scenarios:
1. Basic if Statement:
```c
int x = 10;
if (x > 0) {
printf("x is positive");
}
```
2. if-else Statement:
```c
int x = -5;
if (x > 0) {
printf("x is positive");
}
else {
printf("x is non-positive");
}
```
3. Nested if-else Statement:
```c
int x = 10;
if (x > 0) {
if (x % 2 == 0) {
printf("x is a positive even number");
}
else {
printf("x is a positive odd number");
}
}
else {
printf("x is non-positive");
}
```
4. if-else if-else Ladder:
```c
int marks = 85;
if (marks >= 90) {
printf("Grade: A");
}
else if (marks >= 80) {
printf("Grade: B");
}
else if (marks >= 70) {
printf("Grade: C");
}
else if (marks >= 60) {
printf("Grade: D");
}
else {
printf("Grade: F");
}
```
5. switch Statement:
```c
int choice = 2;
switch (choice) {
case 1:
printf("Option 1 selected");
break;
case 2:
printf("Option 2 selected");
break;
default:
printf("Invalid option");
}
```
6. Advanced switch Statement:
```c
char grade = 'B';
switch (grade) {
case 'A':
case 'a':
printf("Excellent");
break;
case 'B':
case 'b':
printf("Good");
break;
case 'C':
case 'c':
printf("Average");
break;
case 'D':
case 'd':
printf("Below Average");
break;
default:
printf("Invalid grade");
}
```
These examples demonstrate various scenarios where decision control structures in C can be used to handle different conditions and make appropriate decisions based on those conditions. By utilizing these structures effectively, you can write more flexible and responsive programs.
Comments
Post a Comment