break command (C and C++)
The break command allows you to terminate and
exit a loop (that is, do, for, and while)
or switch command from any point other than the logical
end. You can place a break command only in the body
of a looping command or in the body of a switch command.
The break keyword must be lowercase and cannot be
abbreviated.
In a looping statement, the break command ends
the loop and moves control to the next command outside the loop.
Within nested statements, the break command ends
only the smallest enclosing do, for, switch,
or while commands.
In a switch body, the break command
ends the execution of the switch body and gives control
to the next command outside the switch body.
Usage notes
- You cannot use the
breakcommand while you replay recorded statements by using thePLAYBACKcommands.
Examples
- The following example shows a
breakcommand in the action part of aforcommand. If thei-th element of the arraystringis equal to'\0', thebreakcommand causes theforcommand to end.for (i = 0; i < 5; i++) { if (string[i] == '\0') break; length++; } - The following
switchcommand contains severalcaseclauses and onedefaultclause. Each clause contains a function call and abreakcommand. Thebreakcommands prevent control from passing down through subsequent commands in theswitchbody.char key; key = '-'; AT LINE 15 switch (key) { case '+': add(); break; case '-': subtract(); break; default: printf("Invalid key\n"); break; }
