
- C - Home
- C - Overview
- C - Features
- C - History
- C - Standards
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Basic Syntax
- C - User Input
- C - printf Function
- C - Format Specifiers
- Lexical Elements in C
- C - Tokens
- C - Keywords
- C - Identifiers
- Variables and Constants
- C - Variables
- C - Constants
- C - Const Qualifier
- C - Linkage
- Data Types and Type Conversions
- C - Data Types
- C - Literals
- C - Escape sequences
- C - Booleans
- C - Integer Promotions
- C - Character Arithmetic
- C - Type Conversion
- C - Type Casting
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Unary Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Miscellaneous Operators
- Decision Making & Control Statements
- C - Decision Making
- C - if statement
- C - if...else statement
- C - if...else if Ladder
- C - Nested if statements
- C - Switch statement
- C - Nested switch statements
- C - Switch Case Using Range
- Loops in C
- C - Loops
- C - For Loop
- C - While Loop
- C - Do...while Loop
- C - For Loop vs While Loop
- C - Nested Loop
- C - Infinite Loop
- C - Break Statement
- C - Continue Statement
- C - Goto Statement
- Functions in C
- C - Functions
- C - Function Prototype
- 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
- C - Predefined Identifier __func__
- 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
- C - Dynamic Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Character Arrays
- C - Special Characters
- Structures and Unions in C
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Dot (.) Operator
- C - Lookup Tables
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- 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 - Function Pointers
- C - Array of Function Pointers
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Pointer to an Array
- C - Pointers vs. Multi-dimensional Arrays
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Const Pointers & Pointer to Const
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Restrict Keyword
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Initialization of Pointer Arrays
- Storage Classes and Qualifiers
- C - Storage Classes
- Memory Management in C
- C - Memory Management
- C - Memory Address
- Preprocessors in C
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- File Handling in C
- C - File I/O (File Handling)
- C - Input & Output
- Constants and Literals in C
- C - Macros
- C - Header Files
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
- C Online Compiler
For Loop vs While Loop in C
Loops are one of the most important concepts in programming. They allow developers to execute a block of code multiple times without having to rewrite the same codes. Among the commonly used loops, the for loop and the while loop are the most widely used. Both help in iteration, but they differ in syntax, control, and their specific use-cases.
Read this chapter to learn how the for loop is different from the while loop. We will use real-world examples to show when you should choose a for loop over a while loop and vice versa.
What is "for" Loop?
The for loop is used when the number of iterations is already known in advance. It has three parts in its syntax: initialization, condition, and increment/decrement.
for(initialization; condition; update) { // Code to execute }
Example: Print Number from 1 to 5 using For Loop
In this example, we demonstrate the use of a for loop and how we can use it in C programming to print number from 1 to 5 −
#include <stdio.h> int main() { for(int i = 1; i <= 5; i++) { printf("%d\n", i); } return 0; }
What is "while" Loop?
A while loop is useful when the number of iteration is not known in advance. The loop continues until the given condition becomes false. Its syntax is as follows −
while(condition){ // code to execute }
Example: Print Number from 1 to 5 using While Loop
The following example demonstrates how to use a while loop in C programming to print the numbers from 1 to 5 −
#include <stdio.h> int main() { int i = 1; while(i <= 5) { printf("%d\n", i); i++; } return 0; }
When to Use a "for" Loop?
Programmers use for loops in tasks such as performing a fixed set of operations where the start and end conditions are clearly defined. Here, we have highlighted some scenarios where a for loop can be applied −
- Iterating over arrays, strings, or lists
- When the code executes a fix number of times
- In simple counting problems
In addition, we can use nested for loops in sorting algorithms.
Example: Sum of First 5 Numbers
In this example, we use a for loop to calculate the total of numbers up to 5. If the number exceeds 5, the code will stop and display the output.
#include <stdio.h> int main() { int sum = 0; for(int i = 1; i <= 5; i++) { sum += i; } printf("Sum = %d", sum); }
Output
The following is the sum of all the numbers up to 5 −
Sum = 15
When to Use a "while" Loop?
Programmers use while loops in situations where the execution depends on user input, external conditions, or events, and continues until a specific condition is met.
Here are some specific scenarios where you can use a while loop -
- When the termination condition depends on the user input
- When looping until a certain event occurs
- Reading files or streams until the end of function
- Waiting for specific state in programs
Example: Take Input Until User Enters 0
In this example, we use a while loop to display the user input until the user enters 0.
#include <stdio.h> int main() { int num; printf("Enter numbers (0 to stop):\n"); scanf("%d", &num); while (num != 0) { printf("You entered: %d\n", num); scanf("%d", &num); } return 0; }
Output
The program will terminate its execution when you enter "0". Here is the output of the code −
Enter numbers (0 to stop): 1 You entered: 1 2 You entered: 2 0 === Code Execution Successful ===
Difference between "for" Loop and "while" Loop
The following table compares and contrasts the important features of for and while loops −
Feature | for Loop | while Loop |
---|---|---|
Initialization | Declared within the loop structure and executed once at the beginning. | Declared outside the loop and must be done explicitly before the loop starts. |
Condition | Checked before each iteration. | Checked before each iteration. |
Update | Executed automatically after each iteration. | Must be executed inside the loop and handled explicitly. |
Use Cases | Useful when the number of iterations is known or when looping over a range. | Useful when the number of iterations is unknown or depends on conditions |
Initialization and Update Scope | Limited to the loop body. | Scope extends beyond the loop and must be handle explicitly. |