Next: for Statement, Previous: do-while Statement, Up: Loop Statements [Contents][Index]
break
StatementThe break
statement looks like ‘break;’. Its effect is to
exit immediately from the innermost loop construct or switch
statement (see switch Statement).
For example, this loop advances p
until the next null
character or newline.
while (*p)
{
/* End loop if we have reached a newline. */
if (*p == '\n')
break;
p++
}
When there are nested loops, the break
statement exits from the
innermost loop containing it.
struct list_if_tuples
{
struct list_if_tuples next;
int length;
data *contents;
};
void
process_all_elements (struct list_if_tuples *list)
{
while (list)
{
/* Process all the elements in this node’s vector,
stopping when we reach one that is null. */
for (i = 0; i < list->length; i++
{
/* Null element terminates this node’s vector. */
if (list->contents[i] == NULL)
/* Exit the for
loop. */
break;
/* Operate on the next element. */
process_element (list->contents[i]);
}
list = list->next;
}
}
The only way in C to exit from an outer loop is with
goto
(see goto Statement).