- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C do while Loop
![]() Share with a Friend |
C Programming - C do while Loop
do-while Loop in C
The do-while loop is a control flow statement that is similar to the while loop but with a key difference: the condition is checked after each iteration, not before. This guarantees that the code inside the loop will be executed at least once, even if the condition is false initially.
Syntax of do-while Loop:
C
do {
// Code to be executed
} while (condition);
- do: This keyword marks the start of the loop.
- while (condition): The condition is checked after each iteration. If it is true, the loop continues; if false, the loop terminates.
How do-while Loop Works:
- Loop Body Execution: The body of the loop is executed once before checking the condition.
- Condition Check: After the loop body executes, the condition is evaluated.
- Iteration: If the condition is true, the loop executes again. If it is false, the loop exits.
Example:
C
#include <stdio.h>
int main() {
int i = 0;
// A simple do-while loop that prints numbers from 0 to 4
do {
printf("i = %d\n", i);
i++; // Increment i
} while (i < 5);
return 0;
}
Explanation:
- The loop will execute the body at least once, printing the value of i.
- After each iteration, i is incremented.
- The condition i < 5 is checked after the body of the loop.
Output:
i = 0
i = 1
i = 2
i = 3
i = 4
Key Characteristics of do-while Loop:
- Post-Test Loop: The condition is checked after the loop body is executed. This ensures that the loop body runs at least once.
- Always Executes Once: The loop body executes at least once, even if the condition is false on the first check.
- Condition Checked After Execution: If the condition is false initially, the body still executes once before the loop exits.
Example with User Input:
Here’s an example of a do-while loop with user input. The loop continues asking the user to enter a number until they enter 0.
C
#include <stdio.h>
int main() {
int number;
// Ask the user for input at least once
do {
printf("Enter a number (0 to quit): ");
scanf("%d", &number);
printf("You entered: %d\n", number);
} while (number != 0);
printf("Exited the loop.\n");
return 0;
}
Explanation:
- The loop asks the user to enter a number.
- The condition is checked after printing the number. If the user enters 0, the loop exits.
Sample Output:
Enter a number (0 to quit): 5
You entered: 5
Enter a number (0 to quit): 10
You entered: 10
Enter a number (0 to quit): 0
You entered: 0
Exited the loop.
Nested do-while Loop:
You can also nest a do-while loop inside another do-while loop. The inner loop will run completely for each iteration of the outer loop.
Example:
C
#include <stdio.h>
int main() {
for (int i = 1; i <= 3; i++) {
printf("Outer loop i = %d\n", i);
// Inner do-while loop
int j = 1;
do {
printf("Inner loop j = %d\n", j);
j++;
} while (j <= 3);
}
return 0;
}
Explanation:
- The outer for loop runs 3 times.
- The inner do-while loop runs 3 times for each iteration of the outer loop.
Output:
Outer loop i = 1
Inner loop j = 1
Inner loop j = 2
Inner loop j = 3
Outer loop i = 2
Inner loop j = 1
Inner loop j = 2
Inner loop j = 3
Outer loop i = 3
Inner loop j = 1
Inner loop j = 2
Inner loop j = 3
do-while Loop with break and continue :
- break: Terminates the loop immediately.
- continue: Skips the current iteration and moves to the next iteration.
Example with break and continue:
C
#include <stdio.h>
int main() {
int i = 0;
do {
if (i == 5) {
break; // Exit the loop when i equals 5
}
if (i % 2 == 0) {
i++; // Increment and skip even numbers
continue; // Skip even numbers
}
printf("i = %d\n", i); // Prints only odd numbers
i++;
} while (i < 10);
return 0;
}
Explanation:
- When i == 5, the break statement terminates the loop.
- When i is even, the continue statement skips that iteration.
Output:
i = 1
i = 3
i = 5
Summary of do-while Loop:
- The do-while loop guarantees that the loop body executes at least once, which makes it useful in scenarios where you need to perform an action before checking a condition (e.g., user input validation).
- It is a post-test loop, meaning the condition is checked after the loop body is executed.
Advantages:
- Ensures at least one execution of the loop body.
- Ideal for scenarios where the action needs to be performed first (e.g., user prompts, initialization).
Disadvantages:
- Can lead to unexpected behavior if not carefully managed, as the loop always executes at least once, even when the condition is false.
