for循环语句
- 让程序在满足某条件时,重复执行某个代码块。for是java中的关键字。
- 初始语句在for循环开始前执行一次,以后不再执行;循环体条件表达式在每次循环体执行前会执行,如为true,则执行循环体,否则循环结束;循环体后语句会在每次循环执行后被执行;
- for(初始语句;循环体条件表达式;循环体后语句)
{
for 循环体
}
Break语句
break语句可以结束循环
Continue语句
跳过不符合条件的循环
- continue语句可以结束当前循环的执行,开始下一次循环体的执行
案例参考如下:
public class CalcDivFor {
public static void main(String[] args) {
int divided = 1;
int divisor = 5;
int found = 0;
int toBeFound = 10;
for (int i = 0; i < 100 && found < 10; i++) {
if (divided < divisor) {
System.out.println(divided + "小于" + divisor + ",当前循环退出");
divided++;
continue;
}
if (divided % divisor == 0) {
System.out.println(divided + "可以整除" + divisor + ",商为" + (divided / divisor));
found++;
}
if (found >= toBeFound) {
System.out.println("已经找到" + toBeFound + "个数,循环退出");
break;
}
divided++;
}
}
}