Decision making and loops are fundamental concepts in C programming that allow you to control the flow of your program. This guide will cover various decision-making constructs and looping mechanisms available in C.
#### Table of Contents
1. Decision Making in C
- If Statement
- If-Else Statement
- Nested If-Else Statement
- Switch Statement
2. Loops in C
- For Loop
- While Loop
- Do-While Loop
### 1. Decision Making in C
Decision-making statements allow your program to execute certain sections of code based on specific conditions.
#### If Statement
The `if` statement executes a block of code if a specified condition is true.
**Syntax:**
```c
if (condition) {
// code to be executed if condition is true
}
```
**Example:**
```c
#include
int main() {
int number = 10;
if (number > 0) {
printf("The number is positive.\n");
}
return 0;
}
```
#### If-Else Statement
The `if-else` statement executes one block of code if a condition is true, and another block if the condition is false.
**Syntax:**
```c
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
```
**Example:**
```c
#include
int main() {
int number = -10;
if (number > 0) {
printf("The number is positive.\n");
} else {
printf("The number is not positive.\n");
}
return 0;
}
```
#### Nested If-Else Statement
You can nest `if-else` statements within each other to handle more complex conditions.
**Syntax:**
```c
if (condition1) {
// code to be executed if condition1 is true
if (condition2) {
// code to be executed if condition2 is true
} else {
// code to be executed if condition2 is false
}
} else {
// code to be executed if condition1 is false
}
```
**Example:**
```c
#include
int main() {
int number = 0;
if (number > 0) {
printf("The number is positive.\n");
} else if (number < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
return 0;
}
```
#### Switch Statement
The `switch` statement executes one block of code among many options based on the value of an expression.
**Syntax:**
```c
switch (expression) {
case value1:
// code to be executed if expression equals value1
break;
case value2:
// code to be executed if expression equals value2
break;
default:
// code to be executed if expression doesn't match any case
}
```
**Example:**
```c
#include
int main() {
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
case 'C':
printf("Well done\n");
break;
case 'D':
printf("You passed\n");
break;
case 'F':
printf("Better try again\n");
break;
default:
printf("Invalid grade\n");
}
return 0;
}
```
### 2. Loops in C
Loops allow you to execute a block of code repeatedly based on a condition.
#### For Loop
The `for` loop is used when the number of iterations is known beforehand.
**Syntax:**
```c
for (initialization; condition; increment) {
// code to be executed
}
```
**Example:**
```c
#include
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
```
#### While Loop
The `while` loop executes a block of code as long as a specified condition is true.
**Syntax:**
```c
while (condition) {
// code to be executed
}
```
**Example:**
```c
#include
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}
```
#### Do-While Loop
The `do-while` loop is similar to the `while` loop, but it guarantees that the code block is executed at least once.
**Syntax:**
```c
do {
// code to be executed
} while (condition);
```
**Example:**
```c
#include
int main() {
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
return 0;
}
```
### Conclusion
Understanding decision making and loops in C is essential for writing programs that can handle different scenarios and perform repetitive tasks efficiently.
### Summary Table
| Topic | Description |
|----------------------------|-----------------------------------------------------------------------------|
| **If Statement** | Executes code block if condition is true. |
| **If-Else Statement** | Executes one code block if condition is true, another if false. |
| **Nested If-Else Statement** | Handles complex conditions by nesting if-else statements. |
| **Switch Statement** | Executes code block based on value of an expression. |
| **For Loop** | Repeats code block for a specified number of iterations. |
| **While Loop** | Repeats code block as long as condition is true. |
| **Do-While Loop** | Repeats code block at least once and then as long as condition is true. |
By mastering these constructs, you can create more dynamic and flexible C programs. Happy coding!
0 Comments