Home >Java >javaTutorial >What does continue mean in Java
continue in Java means skipping the remaining statements in the loop body and forcibly executing the next loop, that is, terminating the current iteration of the loop and entering the next loop. It is similar to the break statement, but it can only appear in the loop body
#continue
The continue statement skips the loop body The remaining statements in the loop are forced to execute the next loop. Its function is to end this loop, that is, to skip the unexecuted statements below in the loop body, and then make a judgment whether to execute the loop next time.
The continue statement is similar to the break statement, but it can only appear within the loop body. The difference between it and the break statement is that continue does not interrupt the loop statement, but terminates the current iteration of the loop and enters the next iteration. Simply put, continue ignores the current loop of the loop statement.
Note: The continue statement can only be used in the loop body of a while statement, a for statement, or a foreach statement. Using it anywhere else will cause a syntax error.
Example
Enter the student scores of the Java course in a loop, and count the number of students with scores greater than 80 points (including equal to or equal to). At this time, you need to define the variable count to record the number of students with scores greater than 80 (including equal to). Once in each loop, you need to determine whether the entered student scores are greater than or equal to 80. If so, execute count 1, otherwise execute continue and skip this time. Cycle and continue the next cycle. The code is as follows:
public class Test26 { public static void main(String[] args) { int score=0; //记录课程的分数 int count=0; //记录成绩大于等于80分的人数 Scanner input=new Scanner(System.in); for(int i=0;i<10;i++) { System.out.println("请输入第"+(i+1)+"位学生的Java成绩:"); score=input.nextInt(); //获取用户录入的学生成绩 if(score<80) //判断用户录入的学生成绩是否小于80 { continue; //如果小于80,跳过本次循环,继续下次循环 } count++; //如果用户录入的分数大于等于80,则人数加1 } System.out.println("Java 成绩在 80 分以上的学生人数为:"+count); } }
In this program, the variable count represents the number of students with a score of 80 or above. The for loop starts from 0 and loops 10 times. It can be understood that there are only 10 students in the class and the scores of 10 students need to be entered.
Each cycle needs to enter the student's score. At the same time, it needs to be judged whether the student's score entered by the user is less than 80 points. If it is less than 80 points, jump out of this cycle, that is, "count" "Not executed, the number of people with more than 80 points will not be accumulated, and then the next cycle will be executed. Only when the conditional expression of "score<80" does not hold, "count" is executed.
The running results are as follows:
请输入第1位学生的Java成绩: 80 请输入第2位学生的Java成绩: 20 请输入第3位学生的Java成绩: 40 请输入第4位学生的Java成绩: 90 请输入第5位学生的Java成绩: 78 请输入第6位学生的Java成绩: 74 请输入第7位学生的Java成绩: 48 请输入第8位学生的Java成绩: 78 请输入第9位学生的Java成绩: 58 请输入第10位学生的Java成绩: 45 Java 成绩在 80 分以上的学生人数为:2
The above is the detailed content of What does continue mean in Java. For more information, please follow other related articles on the PHP Chinese website!