- 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 Loops
![]() Share with a Friend |
C Programming - C Loops
Loops in C Programming
In C programming, loops are used to execute a block of code repeatedly based on a given condition. There are three main types of loops in C:
- for loop
- while loop
- do-while loop
Each type of loop is used depending on the condition and when you want to check the condition (before or after executing the loop body).
- for Loop
The for loop is typically used when you know in advance how many times the loop needs to run. It has three parts:
- Initialization: The counter variable is initialized.
- Condition: The loop runs as long as this condition is true.
- Update: The counter is updated after each iteration.
Syntax:
C
for (initialization; condition; update) {
// Code to be executed
}
Example:
C
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
printf("i = %d\n", i);
}
return 0;
}
Explanation:
- The loop starts with i = 0.
- The condition i < 5 is checked. If true, the code inside the loop executes.
- After each iteration, i is incremented (i++).
- The loop runs until i reaches 5.
Output:
i = 0
i = 1
i = 2
i = 3
i = 4
- while Loop
The while loop is used when the number of iterations is not known in advance and depends on a condition. The condition is evaluated before executing the loop body.
Syntax:
C
while (condition) {
// Code to be executed
}
Example:
C
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
printf("i = %d\n", i);
i++;
}
return 0;
}
Explanation:
- The condition i < 5 is checked before each iteration.
- As long as the condition is true, the code inside the loop runs.
- i is incremented each time until it reaches 5, at which point the loop terminates.
Output:
i = 0
i = 1
i = 2
i = 3
i = 4
- do-while Loop
The do-while loop is similar to the while loop, but with one key difference: the condition is checked after executing the loop body. This ensures that the loop body is always executed at least once, even if the condition is false initially.
Syntax:
C
do {
// Code to be executed
} while (condition);
Example:
C
#include <stdio.h>
int main() {
int i = 0;
do {
printf("i = %d\n", i);
i++;
} while (i < 5);
return 0;
}
Explanation:
- The code inside the loop is executed first before checking the condition.
- The condition i < 5 is checked after the loop body.
- The loop runs until the condition becomes false.
Output:
i = 0
i = 1
i = 2
i = 3
i = 4
Comparison of Loops
| Feature | for Loop | while Loop | do-while Loop |
|---|---|---|---|
|
Condition Check |
Before loop execution |
Before loop execution |
After loop execution |
|
Use Case |
Known number of iterations |
Unknown number of iterations (pre-condition) |
Unknown number of iterations (post-condition) |
|
Guarantee of Execution |
May not execute at all if the condition is false initially |
May not execute if the condition is false initially |
Will always execute at least once |
|
Example Use Case |
Iterating over a range of numbers |
Iterating until a condition becomes true |
Repeating a menu or prompt until the user provides valid input |
break and continue Statements in Loops
In C, break and continue are used to control the flow of execution inside loops:
- break: Exits the loop immediately, regardless of the condition.
- continue: Skips the current iteration and moves to the next iteration.
Example with break and continue:
C
#include <stdio.h>
int main() {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Exit the loop when i equals 5
}
if (i % 2 == 0) {
continue; // Skip the iteration if i is even
}
printf("i = %d\n", i); // Print only odd numbers less than 5
}
return 0;
}
Explanation:
- The loop will break when i equals 5.
- Even numbers are skipped due to the continue statement, so only odd numbers before 5 are printed.
Output:
i = 1
i = 3
Summary of C Loops:
- for loop: Best when you know the number of iterations beforehand.
- while loop: Best when the loop condition should be evaluated before each iteration and the number of iterations is unknown.
- do-while loop: Best when the loop body should be executed at least once before checking the condition.
- break: Exits the loop immediately.
- continue: Skips the current iteration and moves to the next one.
Loops are essential in C for repeating tasks and iterating over data, and they form the foundation of many algorithms and control structures.
