Home >Java >Javagetting Started >Usage analysis of break and continue
Introduction to the usage of break and continue:
(recommended learning: java entry program)
break
1. Break is used in a switch statement to terminate the switch statement;
2. When break is used in a loop, it jumps out of the loop;
(1) break is used In the switch statement, terminate the switch statement
Example:
int a=4; switch(a){ case 1: System.out.println("星期一"); case 2: System.out.println("星期二"); case 3: System.out.println("星期三"); case 4: System.out.println("星期四"); break; case 5: System.out.println("星期五"); case 6: System.out.println("星期六"); case 7: System.out.println("星期日"); default: System.out.println("你输入的有误"); }
Execution result:
(2) break When used in loops, jump out of the loop
Example:
int sum=0; for(int i=1;i<=100;i++){ if(i>10) break; sum+=i; }
Execution result:
(Video tutorial recommended : java video tutorial)
continue
continue is used in a loop to jump out of this loop and continue to execute the next loop;
Example:
int i; for(i=1;i<10;i++){ if(i==5) continue; System.out.print(i+" "); }
Execution result:
You can see that when i=5, the continue key is encountered word, jump out of this loop, and continue to execute the loop after i=6.
The above is the detailed content of Usage analysis of break and continue. For more information, please follow other related articles on the PHP Chinese website!