VOOZH about

URL: https://www.geeksforgeeks.org/c/c-loops/

⇱ Loops in C - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Loops in C

Last Updated : 6 Dec, 2025

In C programming, there is often a need for repeating the same part of the code multiple times. For example, to print a text three times, we have to use printf() three times as shown in the code:


Output
Hello GfG
Hello GfG
Hello GfG

But if we say to write this 20 times, it will take some time to write statement. Now imagine writing it 100 or 1000 times. Then it becomes a really hectic task to write same statements again and again. To solve such kind of problems, we have loops in programming languages.

In the following code, we have used a loop to print text 3 times. We could have done it for 100 or even 1000 times in the same number of code lines.


Output
Hello GfG
Hello GfG
Hello GfG

Types of loops in C?

Loops in C programming are used to repeat a block of code until the specified condition is met. It allows programmers to execute a statement or group of statements multiple times without writing the code again and again.

There are 3 looping statements in C:

👁 1

Let's discuss all 3 types of loops in C one by one.

for Loop

for loop is an entry-controlled loop, which means that the condition is checked before the loop's body executes.

Syntax

The various parts of the for loop are:

  • Initialization: Initialize the variable to some initial value.
  • Test Condition: This specifies the test condition. If the condition evaluates to true, then body of the loop is executed. If evaluated false, loop is terminated.
  • Update Expression: After the execution loop’s body, this expression increments/decrements the loop variable by some value.
  • Body of Loop: Statements to repeat. Generally enclosed inside {} braces.

Example:


Output
1 2 3 4 5 

Flowchart of for Loop

👁 Forloop

while Loop

A while loop is also an entry-controlled loop in which the condition is checked before entering the body.

Syntax

Only the condition is the part of while loop syntax, we have to initialize and update loop variable manually.

Example:


Output
1 2 3 4 5 6 

Flowchart of while Loop

The below flowchart demonstrates execution flow of the while loop.

👁 While-loop

do-while Loop

The do-while loop is an exit-controlled loop, which means that the condition is checked after executing the loop body. Due to this, the loop body will execute at least once irrespective of the test condition.

Syntax

Like while loop, only the condition is the part of do while loop syntax, we have to do the initialization and updating of loop variable manually.

Example:


Output
0 1 2 3 4 5 6 7 8 9 10 

Flowchart of do-while Loop

The below flowchart demonstrates execution flow of the do while loop.

👁 Do-while-loop

Infinite Loop

An infinite loop is executed when the test expression never becomes false, and the body of the loop is executed repeatedly. A program is stuck in an Infinite loop when the condition is always true. Mostly this is an error that can be resolved by using Loop Control statements. 

Using for loop:


Output

This loop will run forever.
This loop will run forever.
This loop will run forever.
...

Using While loop:


Output

This loop will run forever.
This loop will run forever.
This loop will run forever.
...

Using the do-while loop:


Output

This loop will run forever.
This loop will run forever.
This loop will run forever.
...

Nested Loops

Nesting loops means placing one loop inside another. The inner loop runs fully for each iteration of the outer loop. This technique is helpful when you need to perform multiple iterations within each cycle of a larger loop, like when working with a two-dimensional array or performing tasks that require multiple levels of iteration.

Example:


Output
i = 0, j = 0
i = 0, j = 1
i = 1, j = 0
i = 1, j = 1
i = 2, j = 0
i = 2, j = 1

Loop Control Statements

Loop control statements in C programming are used to change execution from its normal sequence.

NameDescription
breakThe break statement is used to terminate the loop statement.
continueWhen encountered, the continue statement skips the remaining body and jumps to the next iteration of the loop.
gotogoto statement transfers the control to the labeled statement.

Example:


Output
0 1 2 
0 1 2 4 
0 1 2 
Jumped to the 'skip' label when i equals 3.
Comment
Article Tags:
Article Tags: