package com.lqh.chapter01; public class _11stop { public static void main(String[] args) { //1.break int sum = 0; for(int i = 1; i <= 5000; i++) { sum += i; if(i >= 100) { break; } } System.out.println("1+2+3+...+100="+sum); sum = 0; //2.continue for(int j = 0; j <= 100; j++) { if(j % 2 == 0) { continue; } sum += j; } System.out.println("1+3+5+7+...+99="+sum); //label break sum = 0; outer: for(int k = 0; k <= 10000; k++) { inner: while(true) { sum+=k; if(k == 100) { break outer; } else { break inner; } } } System.out.println("1+2+3+...+100="+sum); } }
输出结果为:
1+2+3+...+100=5050
1+3+5+7+...+99=2500
1+2+3+...+100=5050