Do-While Loop in C Programming
Course: [Link]. Computer Science (Cyber Security)
Subject: C Programming
Year: First Year
Submitted by: ____________
Introduction to Loops in C
Loops are a fundamental concept in programming that allow us to execute a block of code
repeatedly until a certain condition is met. They help reduce code redundancy and make
programs more efficient. In C programming, there are three main types of loops:
1. for loop
2. while loop
3. do-while loop
Loops are particularly useful in scenarios such as processing arrays, repeating calculations,
or taking user input multiple times. Without loops, programmers would have to write
repetitive code manually, which is inefficient and error-prone.
Table: Types of Loops in C
| Loop Type | Condition Check | Execution Guaranteed |
|-----------|----------------|-------------------|
| for | Entry-controlled | May not execute |
| while | Entry-controlled | May not execute |
| do-while | Exit-controlled | Executes at least once |
Do-While Loop: Definition & Concept
A do-while loop in C is an exit-controlled loop. This means that the loop body executes at
least once before the condition is evaluated. This is different from entry-controlled loops
like while and for loops, where the condition is checked first.
Characteristics:
- The loop executes at least once.
- The condition is checked after executing the loop body.
- Ideal for menu-driven programs or tasks requiring initial execution.
Flow of execution:
Start -> Execute statements -> Check condition -> Repeat if true -> End
Syntax of Do-While Loop in C
The general syntax of a do-while loop in C is:
do {
// statements to execute
} while (condition);
Explanation:
- 'do' starts the loop.
- Statements inside {} form the loop body.
- 'while(condition);' checks the condition after execution.
- If true, the loop repeats; otherwise, it exits.
Flowchart representation: Start -> Execute statements -> Check condition -> Repeat if true -
> End
Simple Example Programs
Example 1: Print numbers 1 to 5
#include <stdio.h>
int main() {
int i=1;
do {
printf("%d\n", i);
i++;
} while(i<=5);
return 0;
}
Output: 1 2 3 4 5
Example 2: Sum of numbers until user enters 0
#include <stdio.h>
int main() {
int num, sum=0;
do {
printf("Enter a number: ");
scanf("%d", &num);
sum += num;
} while(num != 0);
printf("Sum = %d", sum);
return 0;
}
Intermediate Example Programs
Example 1: Menu-driven program
#include <stdio.h>
int main() {
int choice;
do {
printf("Menu:\n1. Option1\n2. Option2\n3. Exit\nEnter choice: ");
scanf("%d", &choice);
} while(choice != 3);
return 0;
}
Example 2: Password validation
#include <stdio.h>
int main() {
char pass[20];
do {
printf("Enter password: ");
scanf("%s", pass);
} while(strcmp(pass, "1234")!=0);
printf("Access Granted");
return 0;
}
Comparison with While and For Loops
Comparison Table:
| Loop Type | Condition Checked | Executes at least once | Use Case |
|-----------|-----------------|----------------------|---------|
| for | Entry | No | Known iterations |
| while | Entry | No | Unknown iterations |
| do-while | Exit | Yes | Menu-driven, initial execution required |
Example Problem: Print numbers 1 to 3 using all loops.
- for loop: executes 3 times
- while loop: executes 3 times
- do-while loop: executes at least once even if condition false
Practical Applications / Real-Life Scenarios
- Repeating menu display until user chooses exit
- Input validation such as password checks
- Tasks repeated until user confirmation
- Repeated logging or monitoring tasks in cybersecurity
Common Mistakes & Tips
- Forgetting semicolon after while(condition)
- Incorrect condition causing infinite loops
- Uninitialized variables leading to unexpected behavior
- Best practices: test loops with boundary conditions, keep loop body simple
Advantages & Disadvantages
Advantages:
- Guarantees at least one execution
- Simplifies menu-driven programs
- Easy to understand
Disadvantages:
- May execute unnecessarily if condition false initially
- Harder to predict iterations compared to for loop
Conclusion
Do-while loops are essential in C programming for cases where at least one execution is
required. They improve code efficiency, reduce redundancy, and are ideal for interactive
programs. Mastery of do-while loops strengthens programming logic and problem-solving
skills in C.
References / Bibliography
1. Programming in C by K.R. Venugopal & Rajkumar
2. Let Us C by Yashavant Kanetkar
3. [Link]
4. [Link]