break
语句可以跳出当前的循环,包括 while
循环和 for
循环
示例:
public class testt { public static void main(String[] args) { int i = 0; while (i < 10) { if (i < 5) { System.out.print(i+" "); i++; } else { break; } } } } /*输出0 1 2 3 4 */
示例:
public class testt { public static void main(String[] args) { List<Integer> l = Arrays.asList(1, 2, 3, 4, 5, 6); for (int i = 0; i < l.size(); i++) { if (i < 5) { System.out.print(l.get(i)+" "); } else { break; } } } } /*输出1 2 3 4 5 */