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

If you don't know exactly how many times you'll want the loop to iterate, use a while or do-while loop. The main issue is deciding whether to test at the beginning or the end of the loop.

I thought the loop test was always in front of any other procedure or input in the loop?

2006-08-04 07:24:54 · 4 answers · asked by tom_allen95 2 in Computers & Internet Programming & Design

4 answers

If you want the conditional code to execute the first time no matter what then you should do the test at the end.

2006-08-04 07:29:48 · answer #1 · answered by realius 2 · 0 0

You can put a test condition at the bottom of the loop thusly:

bool exit = false;

while(!exit)
{
// do something here....

//put in a test condition here...
if(testcondition == true)
exit = true;
}

Notice that the loop processing takes place before you test for the exit condition.

}

2006-08-04 14:33:44 · answer #2 · answered by Anonymous · 0 0

a while loop checks the condition before going into a loop

i.e.
foo=true;
while (foo== true){
//do stuff that eventually changes foo
}

a do while loop runs through the loop once, then checks the condition.

foo=false;
do{
//do stuff, maybe change foo
}while(foo==true);

this second will always run the first time, then if foo is still false will stop.

2006-08-04 14:57:11 · answer #3 · answered by John J 6 · 0 0

mainly loops are of 2 types. in the case of 'for' loop, it checks the codition first, then executes the codes. in second type, the codes get executed first, after that it checks the condition.

2006-08-04 14:43:47 · answer #4 · answered by ebose01 1 · 0 0

fedest.com, questions and answers