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

If for example i have a condition inside the for-loop, when the condition satisfy even in the middle of the loop, the loop will stop, what statement or syntax should i use?

2006-10-03 20:57:53 · 5 answers · asked by raven_002 1 in Computers & Internet Programming & Design

5 answers

The statement to use is "break". In C, it would go something like this:

while( there_are_lines_left() ) {
s = read_line();
if ( time_to_quit_early(s) )
break;
process_line(s);
}


In that vein, if you want to stop processing a loop early and go to the next iteration, use "continue".

while ( there_are_lines_left() ) {
s = read_line();
if ( line_should_be_ignored(s) )
continue;
process_line(s);
}

2006-10-03 21:32:55 · answer #1 · answered by G. Whilikers 7 · 0 1

another option to break is to set the condition to where it will end. example:
for (a = 0; a < 100; a++)
{
if (a == 50)
a = 100; //this will end the loop after 50 iterations
}

2006-10-04 02:12:02 · answer #2 · answered by justme 7 · 0 0

If you are writing in Visual Basic, the statements "Exit For", "Exit Do" etcetera will exit from a for-loop, do-loop etcetera.

2006-10-03 21:45:21 · answer #3 · answered by bh8153 7 · 0 0

in many languages, like c#,c++,"break;" will get you out of the loop:


for (int i=0;i {
if( condition )
break;
}

2006-10-03 21:26:49 · answer #4 · answered by Stanley 3 · 0 0

sounds like: if then else
so depends what language which version etc.

2006-10-03 21:03:07 · answer #5 · answered by scrambulls 5 · 0 2

fedest.com, questions and answers