English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Can someone show me an example and help me understand this a little better? Thanks :)

2006-11-16 01:55:59 · 7 answers · asked by Christina 2 in Computers & Internet Programming & Design

7 answers

Examples (in Visual Basic):

x = 1
Do While x < 10
MsgBox "Less Than 10!"
x = x + 1
Loop

For x = 1 to 9
MsgBox "Less Than 10!"
Next x

Both will give the same result, but a different way of doing it. You can get each to do different things or the same thing, depending on how you write them. Do While Loops do something WHILE something is true (x<10) and For Next Loops do something for a fixed amount of time.

2006-11-16 02:01:38 · answer #1 · answered by Yoi_55 7 · 0 0

Same action, implementation is different.
The major difference is a while loops condition my not always enter initially unless it's true and a counter is needed within the condition or within the main loop. Also, you are in charge of the termination of the loop. In a for it will terminate once your number has increment and satisfied your condition.(if your using a for() normally like:
for(int i = 0; i < some max; i++) otherwise special cases can arise)
While(condition)

A for loop already has an initialized value, a condition and an incrementor.

Ultimately it depends how creative or your logic structure is setup.
Both can be used the same way, but for loops are usually preferred for arrays. or when a ending condition is known.

2006-11-16 02:11:31 · answer #2 · answered by Arkane Steelblade 4 · 0 0

a while loop means to continue this loop as long as the "while" condition exists. This can be 0 to infinite number of times. A for loop is performed only for the number if iterations named in the for, as in do loop a for 4 times.

2006-11-16 01:58:49 · answer #3 · answered by c.s. 4 · 1 0

for sure this relies upon on the particular programming language, yet usually... a million) a lengthy time period loop makes its conditional try on the starting up of a block of code, so it can or gained't get complete. 2) An till loop (also accepted as repeat/till) makes its try on the great of a block of code, so the code will continually be complete once or extra.

2016-11-24 22:28:39 · answer #4 · answered by Anonymous · 0 0

You use a FOR loop when you have an exact number of times you want to repeat something. Such as adding up an array of numbers.

You use a WHILE loop when you want to repeat a set of instructions as long a certain condition is true.

2006-11-16 02:27:18 · answer #5 · answered by SHAWN G 3 · 0 0

While loop - executes something WHILE a condition is true

For loop - executes something FOR a specific duration

2006-11-16 01:58:05 · answer #6 · answered by blackratsnake 5 · 1 0

while (condition)
{
body of loop
}


for (initialization; condition; incrementation)
{
body of loop
}

2006-11-16 01:59:10 · answer #7 · answered by joker 2 · 1 0

fedest.com, questions and answers