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
Advantages of Loop
- The first and main thing of the loop is we didn’t write the same code again and again.
- Reduce the length of the code.
- The Code is readable. And easy to understand.
- It provides the code reusability.
- 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:-
- While Loop
- For Loop
- Do-While Loop
Let’s See a simple example of a loop.
A natural number is a positive number (counting) so don’t be confused about it.
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.
Nice to read this article will be very helpful in the future, share more info with us. Good job!
ReplyDeletebranding firm SF