Loops in C

Last Updated Nov 4, 2015, 07:00:14 PM





Loops are used to execute a statement several times without writing the redundant or duplicate code over time. Loops are very powerful in programming.

In computer programming, a loop is a block of code that is continually repeated until a particular condition is reached.

Imagine, if you want to print a statement like you name printf("Your Name"); 10 times, what do you? Would you go ahead and write printf code 10 times? No. Although, you can write it ten times, it is not a best practice. To solve similar type of problems, loops help us to write one single line of code to repeat a statement any no of times.

Loops in c programming makes a certian block of code to execute or repeat until the test condition becomes FALSE

Loops are very userful to perform repeatative tasks

For example, let us say, we want to print a statement 5 times. In the below example, we will print the same statement USING loops and also WITHOUT USING loops. You can understand the power of loops after.

Q) Print SnoopCode five times

Without Using Loops

Try It Now

If you notice the above example, to print a name "SnoopCode" we have to write the printf five times manually. This approach leads us to write redundant or duplicate code four times to achieve the same result. How can we solve this problem? by using loops in c programming. See the below example

Using Loops

Try It Now

In the above example, we have used for loop statement available in c programming to print the same statement five times. Meaning, we instructed the compiler to print SnoopCode five times with a loop condition. You got the difference right?.

C programming has different types of loop statements, the below table describes them all clearly

S.NO Loop Type Description Example
1 for loop Executes specific statement several times until the condition becomes FALSE Try It Now
2 while loop Repeats a statements while the given condition stays TRUE Try It Now
3 do... while loop Similar to while loop, but it checks the boolean condition at the end of the loop body. Try It Now
4 nested loops Used to execute nested loops one inside other. Try It Now