• Skip to main content

Itchy Fish

Computer Programming Tips : Choosing What Loop to Use

by itchyfish

Loops are very useful in computer programming. As its namesake, loops are meant to repeat a statement, command, or a set of statements and commands until a condition has been met or for a specific number of times. In my experience, among the most useful and most common loops are the For loop, While Wend, and Do Until loops. Each programming language may have their unique set of loops but for the sake of example for this article, I’ll be using statements from Visual Basic.

Let’s start with the For loop. Let me give an example so I could explain it to you.

For x = 1 to 10
Print “Hello”
Next x

In this example, x will acquire values from 1 to 10 meaning, it will repeat the statement Print “Hello” 10 times. It will output 10 Hellos on your screen. Next x serves as a container which encloses the statements that the For loop will repeat. So as you can see, it will simply count from 1 to 10 and when finished, it will move on to the statement after Next. While Wend and Do Until are somewhat similar. Let me give you an example for each.

x = 1
While x = 1
If condition = met then x = 2
Wend

x = 1
Do
If condition = met then x = 2
Until x 1

It looks confusing if you compare the two but you’ll get it as you go along. While Wend loops will repeat the statements until the condition (x = 1) becomes false. From the example, the While Wend loop will loop over and over while x is equal to 1. If a condition is met (depending on the logic of the program you’re trying to develop) within the loop and you change the value of the variable, the loop will end.

On the other hand, in the Do Until example, the loop will repeat until x becomes any other number aside from 1. So if the value of x does not change, it will continue to repeat. If the condition (x 1 , x is not equal to 1) becomes true, the Do Until loop ends.

So depending on the situation and the logic of your program, choose properly between the 3 loops. For loops will repeat a set number of times, While Wend loops will repeat itself while the condition is true and Do Until loops will repeat itself until the condition becomes true.

These are just the basic applications of these loops. There are much more advanced uses for these loops. However, in order to utilize the loops more effectively, you must understand the basics first. Knowing how to use them can save you time and will make your program easier to code and understand. You’ll probably have your own unique way of using these loops as you learn and continue to practice programming.

Related

  • Computer Programming Tips : Fixing Infinite Do and While Loops
  • Training for Computer Programming at Home
  • Deepwater Horizon Oil Leak Continues with Slick Moving into Loop Current
  • Romantic Dining in Chicago's Loop
  • Gulf Oil Spill: NOAA's Analysis of Loop Current Oil Transport to Florida and Florida Keys
  • Tips for Choosing a Computer Power Supply Unit
Previous Post: « THE TOWER
Next Post: Kids Rooms: How to Get Your Kids to Clean Their Rooms on Their Own! »

© 2021 Itchy Fish · Contact · Privacy