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

how do you make a loop that stops outputting when just one item has been output?

2007-05-11 08:40:59 · 2 answers · asked by dylan k 1 in Computers & Internet Programming & Design

2 answers

The break; command will exit the loop block. (also works on for loops, of course)

while(condition)
{
//do something
if(breakingcondition)
{
break;
}
}

2007-05-11 08:43:40 · answer #1 · answered by Rex M 6 · 0 0

There are different types of loop
For loop
for(var i=0;i<1;i++0) {
//Loop code here. This will only be done once
}

While loop
var =0;
while(i<1) {
//loop code here. This will only be done once
i++
}

Until loop
var i=0;
do {
//loop code here. This will only be done once
i++
} while(i<1);

Generally the for loop is used for arrays because it gives you an automatic element count.

The first while loop is used because it tests first.

The second while loop is used because generally it will be performed at least once.

2007-05-11 08:57:14 · answer #2 · answered by AnalProgrammer 7 · 0 1

fedest.com, questions and answers