while循环(最基本的循环)
do...while循环(不管布尔值是否正确,dowhile至少循环一次)
for循环
package struct; public class WhileDemo01 { public static void main(String[] args) { //计算1+2+3+.....+100 int i = 0;//i初始值 int sum = 0; while (i <= 100) { sum=sum+i; i++; } System.out.println(sum); } }
---------------------------------------------------------------------------------------------------------------------------------
Dowhile语法是先执行后判断,while则先判断后执行,这也导致了前者至少被执行一次
package struct; public class DowhileDemo01 { public static void main(String[] args) { int i=0; int sum=0; do { sum=sum+i; i++; }while(i<=100); System.out.println(sum); } }