Break statements are typically used in loop statements and switch statements. When break is used in switch statements, the program can jump out of a switch and execute a switch statement, and if there is no break statement, the execution starts from the place where the condition is met (that is, the case that matches the expression in the switch (expression) bracket). Until the switch structure is finished. When the break statement is used in an do-while, for, and while Loop statement, the program terminates the loop. Instead of executing the statements that follow the loop, the break statement is always associated with the IF statement. That is, it jumps out of the loop when the condition is met. Example: Main () {int I=0;char c;while (1)///Set loop */{c= ' \ n ';/* variable assigns initial value */while (c!=13&&c!=27)//keyboard receives character until press ENTER or ESC key */{c=getch ();p rintf ("%c\n", c);} if (c==27) break; /* Determine if the ESC key exits the Loop */i++;p rintf ("the No. Is%d\n ", i);} printf ("the End");}
Note : 1. The break statement has no effect on if-else conditional statements. 2. In a multilayer loop, a break statement jumps only one layer outward.
Use of break in C