Categories: C

Loops and its examples

Let us take a look at the various ways to use loops in c.

Infinite loop

An infinite loop in c does not end, in other words, its condition never becomes true and keeps on updating forever. It is also known as an endless loop or indefinite loop.

Example:
#include <stdio.h>

int main() {
   
    for(;;)
   {
       printf("\nCoderzpy!");
   }
    
    return 0;
}
Output:
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!
Coderzpy!

Here for(;;) is an infinite loop where there is no initialization, condition, or updating statement, therefore the loop becomes endless, and it continues forever.

Need for infinite loop:

  • All servers use an infinite loop to accept clients’ requests continuously.
  • All the operating systems run in an infinite loop, as it does not exist after performing some task. It comes out of an infinite loop only when the user manually shuts down the system.
  • All the games also run in an infinite loop.
The sum of first n natural numbers:

Here we are calculating the sum of n natural numbers using for loop.

#include <stdio.h>
int main()
{
    int num, count, sum = 0;

    printf("Enter a positive integer: ");
    scanf("%d", &num);

    // for loop terminates when num is less than count
    for(count = 1; count <= num; ++count)
    {
        sum += count;
    }

    printf("Sum = %d", sum);

    return 0;
}
Output:
Enter a positive integer: 10
Sum = 55
Check for Prime Number:

For checking prime numbers, calculate the number of factors of a number and check if it is greater than or equal to 2.

#include <stdio.h>

int main() {
   int n,i,count=0;
   scanf("\n%d",&n);
    for(i=1;i<=n;i++)
   { 
      if(n%i==0)
       count++;
   }
    if(count==2)
    {
        printf("%d is a prime number.",n);
    }
    else
    {
        printf("%d is not a prime number.",n);
    }
    return 0;
}
output:
11
11 is a prime number.

Note: also read about Decision-Making in C & Loops in C

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago

Binary Search Tree (BST)

A Binary Search Tree (BST) is a type of binary tree that satisfies the following…

1 year ago