7/2/21

Introduction of Loop in C Programming Language.

Has it ever happened to you that in C programming you have to print a code of block several times? And you have to write that code again and again. If you write the same thing several times. Then your code will get long, complex, and difficult to read. 

And didn’t want to write the same code again and again. So what is its solution? The Solution is Loop. Loop that statement until the condition is not satisfied. Let’s talk about the loop. What is a loop? How its work? etc. etc. 

Introduction of Loop

The Loop is used to execute a statement until the condition of the code is not satisfy. In simple words, a loop provides us to execute a group of statements or multiple instructions multiple times. For Example, if you have to print 1-10 numbers then instead of using printf statement 10 times, we can use printf inside the loop which runs up to 10 iterations.

Advantages of Loop

  1. The first and main thing of the loop is we didn’t write the same code again and again.
  2. Reduce the length of the code. 
  3. The Code is readable. And easy to understand.
  4. It provides the code reusability. 
  5. Loop takes less memory space comparing (write the code several times)

There are mainly 3 types of loops available in the C programming language. These are:-

  1. While Loop
  2. For Loop
  3. Do-While Loop

Let’s See a simple example of a loop. 

Write a program to print the First 50 natural numbers using the loop.

A natural number is a positive number (counting) so don’t be confused about it.


#include<stdio.h>

int main()
{
    int i;

    for (i=1; i<=50; i++)
    {
        printf("%d \n", i);
    }
    return 0;
}

Output:- 


Look at the length of the code. Using the loop the length of the code is reduced. If we have to print 50 numbers without using a loop then we have to write printf statement 50 times. And think if I said to you that, you have to print 50 lakh numbers. I bet, you are not going to do it at any cost. Writing printf statement 50 lakh times is not an easy job.

1 comment:

  1. Nice to read this article will be very helpful in the future, share more info with us. Good job!
    branding firm SF

    ReplyDelete

Like us on facebook

blogger templatesblogger widgets

Follow on Twitter

Linkedin

Categories

Mad About Computer. Powered by Blogger.