- 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 while Loop
![]() Share with a Friend |
C Programming - C while Loop
while Loop in C
The while loop is used in C programming when you want to repeat a block of code an unknown number of times, but only as long as a certain condition remains true. It is a pre-test loop, meaning the condition is evaluated before each iteration of the loop.
Syntax:
C
while (condition) {
// Code to be executed
}
- condition: The condition is checked before each iteration. If it evaluates to true (non-zero), the loop body executes. If it's false (zero), the loop terminates.
- Code inside the loop: This block will keep executing as long as the condition is true.
How while Loop Works:
- Initialization: Before the loop starts, a variable (or variables) are initialized.
- Condition Evaluation: The condition is checked at the beginning of each iteration.
- Loop Body Execution: If the condition is true, the loop body executes.
- Update Step: After each iteration, variables are updated (this can be done inside the loop).
- Termination: When the condition becomes false, the loop terminates and the control is passed to the next statement after the loop.
Example:
C
#include <stdio.h>
int main() {
int i = 0;
// Loop that prints numbers from 0 to 4
while (i < 5) {
printf("i = %d\n", i);
i++; // Increment i
}
return 0;
}
Explanation:
- i is initialized to 0.
- The loop runs while i < 5 (the condition is checked at the beginning).
- Inside the loop, i is printed and then incremented by 1.
- Once i reaches 5, the condition becomes false, and the loop terminates.
Output:
i = 0
i = 1
i = 2
i = 3
i = 4
Key Points about while Loop:
- Pre-Test Loop: The condition is checked before the loop body executes, meaning that if the condition is false initially, the body may not execute at all.
- Infinite Loop: If the condition is always true, the while loop will run indefinitely. This can be avoided by ensuring the condition eventually becomes false.
Example of an infinite loop:
C
while (1) {
// This will run forever unless there's a break or return statement
printf("This is an infinite loop.\n");
}
- Condition is Evaluated at the Start: If the condition is false at the start, the loop will not run at all.
- Loop Body Execution: The statements inside the loop are executed as long as the condition is true.
Example with User Input:
Here’s a more interactive example where the while loop continues until the user enters a specific value.
C
#include <stdio.h>
int main() {
int number;
// Ask the user for input
printf("Enter a number (0 to quit): ");
scanf("%d", &number);
// Loop runs until the user enters 0
while (number != 0) {
printf("You entered: %d\n", number);
printf("Enter another number (0 to quit): ");
scanf("%d", &number); // User enters new number
}
printf("Exited the loop.\n");
return 0;
}
Explanation:
- The program asks the user to enter a number.
- If the user enters 0, the loop terminates.
- Otherwise, it prints the entered number and prompts the user to enter another number.
Sample Output:
Enter a number (0 to quit): 5
You entered: 5
Enter another number (0 to quit): 10
You entered: 10
Enter another number (0 to quit): 0
Exited the loop.
Advantages of while Loop:
- Flexible: It is ideal when the number of iterations is not known in advance.
- Simple Condition: It executes based on a simple condition.
Disadvantages of while Loop:
- Potential for Infinite Loops: If the condition is not correctly managed or updated, the loop may never terminate.
- Condition Checking Overhead: The condition is checked each time, which may not be as efficient as a for loop if the number of iterations is fixed.
Conclusion:
The while loop is a fundamental looping construct in C that is useful for repeating tasks while a condition holds true. It's particularly useful when you don't know in advance how many iterations the loop will need. However, care should be taken to ensure the condition eventually becomes false to avoid infinite loops.
