Essentials of Counter-Controlled Repetition

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisShare on RedditPin on PinterestPrint this pageEmail this to someone

What is “Counter-Controlled Repetition” ?

Counter-Controlled Repetition which seems bit ruff at the first sight is nothing but a simple king of flow controlling method, available in almost every programming language. So lets break this word into simple units to understand what it is.

Counter is a variable which is used to count up or down to some value. As an example think of a simple cash counter you see at a regular bank. It keep on counting number of notes, until the notes are over.The machine increases a electronic counter to keep track of total number of notes. In the same way we use counters in various parts of programming process.

So what is a repetition. As the word means it is an repetition of some computer instructions. So when it comes to programming languages, it refers to the repeated execution of some lines of code over and over again.

So “Counter-Controlled Repetition” refers to the repetition of same instructions depending on a counter variable, that controls the number of times instructions should be repeatedly executed.

Essentials of Counter-Controlled Repetition ?

Counter variable – As we discussed earlier, it is necessary to have a control variable, to control number of executions and flow of the repetition.

Initial value of counter variable – The counter variable could start counting from zero or else it is possible to start counting from a desired value. As a example imagine you need to count from 100 to 0 backwards. It is possible do this in two main ways, as sown below.

No 1 – Starting with initial value of 0 and counting to 100 while printing 100 – counterVar

Dim counterVar As Integer = 0
While counterVar <= 100
Console.Out.WriteLine(100 – counterVar)
counterVar += 1
End While
No 2 – Starting with initial value of 100 and counting to 0 while printing counterVar
Dim counterVar As Integer = 100
While counterVar >= 0
Console.Out.WriteLine(counterVar)
counterVar -= 1
End While

 

As you can see the second way is much more simple, efficient and will increase the performance of the applications. So it is clear that initial value of the counter variable is an important fact.

Loop-continuation condition – There should be a limit for the repetition of instruction. Otherwise an infinite loop will lead to a program crash or malfunction. This condition which checks limit is called loop-continuation condition. The limit could be defined in may ways.

As an example in above loops “While counterVar >= 0” means that repetition of instructions should only happpen if value of counterVar is greater than or equal to zero.

Increment or decrement operation – Finally there should be a statement which is going to increase or decrease value of counter. Again, loop will end up in a infinite execution, if this instruction was missed.

As an example ” counterVar -= 1″ in above example instructs the decrement of counterVar variable by one, in each repetition.

Examples

Example are always better than words. So I’ll list some examples in VB.Net for various kind of Counter-Controlled Repetitions. Each examples shows different ways of counting from 0 to 100.

While loop :

‘counter variable definition and initialization
Dim counterVar As Integer = 0
‘Loop-continuation condition
While counterVar <= 100
‘Instruction to be repeated
Console.Out.WriteLine(counterVar)
‘Increment of counter
counterVar += 1

End While

For Loop :

‘counter variable definition
Dim counterVar As Integer
‘counter variable initialization and Loop-continuation condition
For counterVar = 0 To 100
‘Instruction to be repeated
Console.Out.WriteLine(counterVar)

‘Increment of counter
Next counterVar

Do Until Loop :

‘counter variable definition and initialization
Dim counterVar As Integer = 0
‘Loop-continuation condition
Do Until counterVar = 101 ‘(just for example. counterVar > 100 is the best practice)
‘Instruction to be repeated
Console.Out.WriteLine(counterVar)
‘Increment of counter
counterVar += 1

Loop

Protected by Copyscape Duplicate Content Software

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisShare on RedditPin on PinterestPrint this pageEmail this to someone

7 comments on “Essentials of Counter-Controlled Repetition

  1. Devaka Cooray September 18, 2010 10:58 AM

    Hmm, the topic sounds familiar 😀
    Man, why not posting that HTML == HTTP discussion? 😛

  2. Devaka Cooray September 18, 2010 11:13 AM

    that was the coolest discussion we ever had 🙂

  3. Ayoma Gayan Wijethunga September 18, 2010 11:13 AM

    Yeh.. Do you have a full copy of that Devaka? If so I can publish a complete copy including your posts.

  4. Devaka Cooray September 18, 2010 11:16 AM

    he he, let’s not open that to the public 😉

  5. Ayoma Gayan Wijethunga September 18, 2010 11:16 AM

    Good for me 😉 Anyway there were too dangerous things including XSS and Cache Poisoning. Then, let’s leave it behind..

  6. Devaka Cooray September 18, 2010 11:24 AM

    Ah yeah, I remember, you were about going to jail in it too 🙂

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">