- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- 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 - 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 - Loops
Loops are a programming construct that denote a block of one or more statements that are repeatedly executed a specified number of times, or till a certain condition is reached.
Repetitive tasks are common in programming, and loops are essential to save time and minimize errors. In C programming, the keywords while, do–while and for are provided to implement loops.
Looping constructs are an important part of any processing logic, as they help in performing the same process again and again. A C programmer should be well acquainted with implementing and controlling the looping construct.
Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times.
Flowchart of C Loop Statement
Given below is the general flowchart of a loop statement which is applicable to any programming language −
The statements in a C program are always executed in a top-to-bottom manner. If we ask the compiler to go back to any of the earlier steps, it constitutes a loop.
Example: Loops in C
To understand the need of loops in a program, consider the following snippet −
#include <stdio.h> int main (){ // local variable definition int a = 1; printf("a: %d\n", a); a++; printf("a: %d\n", a); a++; printf("a: %d\n", a); a++; printf("a: %d\n", a); a++; printf("a: %d\n", a); return 0; }
Output
On running this code, you will get the following output −
a: 1 a: 2 a: 3 a: 4 a: 5
The program prints the value of "a", and increments its value. These two steps are repeated a number of times. If you need to print the value of "a" from 1 to 100, it is not desirable to manually repeat these steps in the code. Instead, we can ask the compiler to repeatedly execute these two steps of printing and incrementing till it reaches 100.
Example: Using While Loop in C
You can use for, while or do-while constructs to repeat a loop. The following program shows how you can print 100 values of "a" using the "while" loop in C −
#include <stdio.h> int main () { // local variable definition int a = 1; while (a <= 100){ printf("a: %d\n", a); a++; } return 0; }
Output
Run this code and check the output −
a: 1 a: 2 a: 3 a: 4 ..... ..... a: 98 a: 99 a: 100
If a step redirects the program flow to any of the earlier steps, based on any condition, the loop is a conditional loop. The repetitions will stop as soon as the controlling condition turns false. If the redirection is done without any condition, it is an infinite loop, as the code block repeats forever.
Parts of C Loops
To constitute a loop, the following elements are necessary −
- Looping statement (while, do–while or for)
- Looping block
- Looping condition
Loops are generally of two types −
Counted Loops in C
If the loop is designed to repeat for a certain number of times, it is a counted loop. In C, the for loop is an example of counted loop.
Conditional Loops in C
If the loop is designed to repeat till a condition is true, it is a conditional loop. The while and do–while constructs help you to form conditional loops.
Looping Statements in C
C programming provides the following types of loops to handle looping requirements −
Sr.No. | Loop Type & Description |
---|---|
1 |
Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. |
2 |
Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. |
3 |
It is more like a while statement, except that it tests the condition at the end of the loop body. |
4 |
You can use one or more loops inside any other while, for or do-while loop. |
Each of the above loop types have to be employed depending upon which one is right for the given situation. We shall learn about these loop types in detail in the subsequent chapters.
Loop Control Statements in C
Loop control statements change the execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
C supports the following control statements −
Sr.No. | Control Statement & Description |
---|---|
1 |
Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch. |
2 |
Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. |
3 |
Transfers the control to the labeled statement. |
The break and continue statements have contrasting purposes. The goto statement acts as a jump statement if it causes the program to go to a later statement. If the goto statement redirects the program to an earlier statement, then it forms a loop.
The Infinite Loop in C
A loop becomes an infinite loop if a condition never becomes false. An infinite loop is a loop that repeats indefinitely because it has no terminating condition, or the termination condition is never met or the loop is instructed to start over from the beginning.
Although it is possible for a programmer to intentionally use an infinite loop, they are often mistakes made by new programmers.
Example: Infinite Loop in C
The for loop is traditionally used for creating an infinite loop. Since none of the three expressions that form the "for" loop are required, you can make an endless loop by leaving the conditional expression empty.
#include <stdio.h> int main (){ for( ; ; ){ printf("This loop will run forever. \n"); } return 0; }
Output
By running this code, you will get an endless loop that will keep printing the same line forever.
This loop will run forever. This loop will run forever. ........ ........ This loop will run forever.
When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but C programmers more commonly use the for(;;) construct to signify an infinite loop.
Note − You can terminate an infinite loop by pressing the "Ctrl + C" keys.