C Program to Demonstrate Control Structures with Example
Learn C step by step.
Explore blog under C language - Control Structures
Jan 22, 2026
## Chapter 5: Control Structures
**What is a Control Structure?**
We use control structures when a program needs to make decisions or perform actions based on conditions.
In simple words:
"If this happens, do this, otherwise do something else."
### 1. If-Else Statement
**What does an if statement do?**
* It checks a condition.
* If the condition is true -> the code inside will execute.
* If it is false -> the code will be skipped.
**Real-life example**
* If age >= 18 -> Adult
* If age < 18 -> Minor
```c
#include
int main() {
int userAge;
printf("Enter your age: ");
scanf("%d", &userAge);
if (userAge >= 18) {
printf("You are an adult.n");
}
return 0;
}
```
If-Else:
**What is if-else?**
* if -> when the condition is true
* else -> when the condition is false
```c
if (userAge >= 18) {
printf("You can vote.n");
} else {
printf("You cannot vote yet.n");
}
```
If-Else-If Ladder:
**What is an if-else if ladder?**
When multiple conditions need to be checked and only one condition should be true, we use an if-else if ladder.
The flow goes from top to bottom.
The first condition that is true is executed.
All others are skipped.
```c
#include
int main() {
int score;
printf("Enter your score: ");
scanf("%d", &score);
if (score >= 90) {
printf("Result: A+n");
} else if (score >= 80) {
printf("Result: An");
} else if (score >= 70) {
printf("Result: Bn");
} else if (score >= 60) {
printf("Result: Cn");
} else if (score >= 50) {
printf("Result: Dn");
} else {
printf("Result: Fn");
}
return 0;
}
```
### 2. Switch Statement
**What is a Switch Statement?**
A switch statement is used when there are fixed options for a single variable (like 1-7, menu choices, day number, option number).
It's a cleaner and easier version of if-else statements.
Comparison is only done using equality (==).
```c
#include
int main() {
int choice;
printf("Enter day number (1-7): ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Mondayn");
break;
case 2:
printf("Tuesdayn");
break;
case 3:
printf("Wednesdayn");
break;
case 4:
printf("Thursdayn");
break;
case 5:
printf("Fridayn");
break;
case 6:
printf("Saturdayn");
break;
case 7:
printf("Sundayn");
break;
default:
printf("Invalid choice!n");
}
return 0;
}
```
Important: The break statement is very important! Without it, the execution "falls through" to the next case.
### 3. Loops
**What are Loops?**
Loops are used to repeat the same task multiple times as long as the condition is true.
Examples:
* Counting from 1 to 5
* Printing a message 10 times
* Calculating the sum of numbers
While Loop
** When to use a while loop?
When the condition is known beforehand, but the exact number of iterations is not certain.
```c
#include
int main() {
int number = 1;
while (number <= 5) {
printf("Count: %dn", number);
number++;
}
return 0;
}
```
**Output:**
```
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
```
Do-While Loop
**What is a do-while loop?**
* A do-while loop executes at least once.
* The code runs first,
* then the condition is checked.
Therefore, even if the input is incorrect, there is still a chance to get input at least once.
```c
#include
int main() {
int inputvalue;
do {
printf("Enter a positive number: ");
scanf("%d", &inputvalue);
if (inputvalue <= 0) {
printf("This is not positive! Try again.n");
}
} while (inputvalue <= 0);
printf("Thank you! You entered: %dn", inputvalue);
return 0;
}
```
For Loop
**What is a for loop?**
* A for loop is used when we know beforehand how many times the loop needs to run.
* In this example, the loop will run 10 times.
* Each time the value from 1 to 10 will be printed.
```c
#include
int main() {
// Print numbers from 1 to 10
for (int num = 1; num <= 10; num++) {
printf("%d ", num);
}
printf("n");
return 0;
}
```
Description of For Loop:
```c
for (initialization; condition; increment) {
// Code to repeat
}
```
* A for loop is used when... This happens when we know how many times the task needs to be repeated.
* Each time, the next multiplication of the table is printed.
Multiplication Table Example:
```c
#include
int main() {
int value;
printf("Enter a number: ");
scanf("%d", &value);
printf("Multiplication table for %d:n", value);
for (int count = 1; count <= 10; count++) {
printf("%d x %d = %dn", value, count, value * count);
}
return 0;
}
```
### 4. Loop Control Statements
Break - Exits the loop immediately:
```c
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Stops when i is 5
}
printf("%d ", i);
}
// Output: 1 2 3 4
```
Continue - Skips the current iteration:
```c
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skips 3
}
printf("%d ", i);
}
// Output: 1 2 4 5
```
---
## Conclusion
In this article, we explored the core concepts of Explore blog under C language - Control Structures. Understanding these fundamentals is crucial for any developer looking to master this topic.
## Frequently Asked Questions (FAQs)
**Q: What is Explore blog under C language - Control Structures?**
A: Explore blog under C language - Control Structures is a fundamental concept in this programming language/topic that allows developers to perform specific tasks efficiently.
**Q: Why is Explore blog under C language - Control Structures important?**
A: It helps in organizing code, improving performance, and implementing complex logic in a structured way.
**Q: How to get started with Explore blog under C language - Control Structures?**
A: You can start by practicing the basic syntax and examples provided in this tutorial.
**Q: Are there any prerequisites for Explore blog under C language - Control Structures?**
A: Basic knowledge of programming logic and syntax is recommended.
**Q: Can Explore blog under C language - Control Structures be used in real-world projects?**
A: Yes, it is widely used in enterprise-level applications and software development.
**Q: Where can I find more examples of Explore blog under C language - Control Structures?**
A: You can check our blog section for more advanced tutorials and use cases.
**Q: Is Explore blog under C language - Control Structures suitable for beginners?**
A: Yes, our guide is designed to be beginner-friendly with clear explanations.
**Q: How does Explore blog under C language - Control Structures improve code quality?**
A: By providing a standardized way to handle logic, it makes code more readable and maintainable.
**Q: What are common mistakes when using Explore blog under C language - Control Structures?**
A: Common mistakes include incorrect syntax usage and not following best practices, which we've covered here.
**Q: Does this tutorial cover advanced Explore blog under C language - Control Structures?**
A: This article covers the essentials; stay tuned for our advanced series on this topic.