fuddlepixelFuddlepixel coz tech is fun();

Looping Repeating Concept

Let us learn the concept of looping and how to implement loops in C language, the syntax of for, while and do while loops.

By Shubham Ramdeo
23 July 2015 · 6 mins read
# c  newbie 

Looping Repeating Concept

Don’t want to write similar instructions again and again ? Use looping to repeat it !

Welcome Back to The Newbie Programmer Series. With the previous part (Click here), we have completed variables and formatted input output. All the things you should know before going forward have been completed. If any extra knowledge is needed, We will do it along. In this part, we are going to learn about looping. What is Loop and How to use this in C Language. So If you are new to this series, please go to the index (click here) and read out all the previous parts so that you can easily get what is happening below.

This is part is gonna be theoretical, we would do applications in the next part.

Looping

Loop actually means a ring. But what so special in that ? Women loves rings. There are many reasons for that. But why do programmers ? Well, Women may love rings as it’s too costly, but for programmer, they love it as it reduces a lot of work costs ! If you start moving on a ring, you will get back to the initial point after a round. This is loop. You walk in a circular park ? You get back to the same point after a round. The number of rounds you run, is the number of loops.

This is the concept we use in programming. For example, if you want to print “Hello, World !” on the screen 100 times, are you gonna write the Hello World program 100 times ? What if I want it 10000 times ? I believe no body is so hardworking to write same line 10000 times. It’s a hard work. And as I always say, Who does the hard work ? Computer ! Instead of hardworking and writing “hello world” 100 times, we will use loops, our smartness, to print Hello World 100 or even 10000 times !

looping

That’s it. Now let us learn how can we use loops in programming. But there is one more important concept you should know before jumping into coding part…

Counting Number Loops

You can see as above that looping can never end. A person can go on forever taking rounds and rounds because there is no end point in a loop or ring. So how can we stop loops ? Well think in a simple way, what our running man would do ? Simple. He will count the number of rounds. Similarly here we gonna count the number of loops during programming. And to store these numbers, We will use what ? Variables ! A variable will store number of loops and when the limit exceeds, it will stop.

Mechanism

To count these number of terms, the variable is simply of an Integer type. Because the number of turns are what ? 1 turn, 2 turns, 3… 100 turns are simply Natural numbers. So we use integer values only. And the Mechanism is very simple. After every loop, we are gonna increase the value of our counter by one. For example, let a variable count. Let we want to print “Hello World” 100 times. When “Hello World” printed first time, count = 1, “Hello world” printed second time, count = 2…. and so on… when “Hello world” printed 100th time, the program will find the limit of repetition. And the program will stop. If there was No limit, the program would continue writing “Hello World” Forever !

Programming the Loops

We have discussed a lot of theory above So lets come to the coding part.

We can use loops in C in many ways. Directly on Indirectly. (Its my classification, not authentic ! )

In direct looping, we are gonna use the ways which are provided by the C language directly for loops. C provides mainly two solutions for loops. The For loop and the while loop.

Both of them have 4 simple parameters. ( I am writing in a simple way, not the authentic definition )

  1. Initial point which is where our counting starts
  2. Repeat until which is the condition up to which the loop will go.
  3. Instructions to repeat which will be repeated again and again till a condition is satisfied.
  4. Counter, which is the real counter, it increases or decreases the number of counts after each repetition.

“While” loop

Lets learn the syntax first,

initial points while ( repeat until ) { instructions to repeat counter }

Lets make the “hello world” repeater with while loop. Let it repeat for 10 times

#include <stdio.h>
main()
{
   int count = 0;
   while ( count < 10 )
   {
      printf("Hello, World ! n");
      count = count + 1;
   }
}

NOTE: instead of writing count = count + 1; you can write count++

So the same program can be written as:

#include <stdio.h> 
main() 
{ 
   int count = 0;
   while ( count < 10 ) 
   { 
      printf("Hello, World !"); 
      count++; 
   }
}

“Do_While” loop

Little different syntax,

initial point do { instructions to repeat } while (repeat until);

Lets make the “hello world” repeater with do_while loop. Let it repeat for 10 times

#include <stdio.h>
main()
{
   int count = 0;
   do
   {
      printf("Hello, World ! n");
      count++;
   }while(count < 10);
}

“For” loop

Its my favorite, its like :

for ( initial point ; repeat until ; counter ) { instructions to repeat }

The Hello World repeater in the for loop version is gonna like:

#include <stdio.h>
main()
{
   int count;
   for ( count = 0; count < 10; count ++ )
   {
      printf("Hello, World !"); 
   }
}

Both of the above will do the same, will print hello world 10 times.

Working

At first, count is 0, then hello world is printed, number of count is incremented to +1. Then again hello world is printed, then count goes 2… this process continues until count is less than 10. When count becomes 10, this loop process stops. The real working and their difference are in the next post.

Do a little experiment it.

And That’s it for today. I will stop now because this post, as written above, is theoretical and We will do a lot of beautiful applications of loops but from the next part. So stay connected.

Its raining and I wrote it in this part in a single try so please let me know if there is any correction needed above !

Please share this post as much as you can so that we can get connected to more geeks who wants to learn programming but have no clue. Thanks for reading :)


Share this :



Update me weekly ✉

Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.

I'm in ♥


Or follow us
   


About author

Shubham Ramdeo

Shubham Ramdeo

Shubham Ramdeo is the founder of Fuddlepixel. He is a geek, a writer, involved with many technical and humanity projects.

Meet me!



Recent





Related

c 

newbie 

Top