1.while语句
用while来实现下在控制台输出1到10
public class test13 {
public static void main(String[] args) {
int i=1;
while(i<=10) {
System.out.print(i+" ");
i++;
}
}
}
2.do…while
public class test14 {
public static void main(String[] args) {
int i=1;
do {
System.out.print(i+" ");
i++;
}while(i<=10);
}
}
while和do…while的区别
从前面两个实例我们很容易看出,while是先判断后执行 do…while是先执行后判断,do…while是肯定会至少执行一次,while的话,不一定会执行;
3.for循环
public class test15 {
public static void main(String[] args) {
for (int i = 1; i <=10; i++) {
System.out.print(i+" ");
}
}
}
4.for循环嵌套
打印9*9乘法口诀
public class test16 {
public static void main(String[] args) {
for (int i = 1; i <=9; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(i+"*"+j+"="+(i*j)+" ");
}
System.out.println();
}
}
}
注:break结束当前循环,continue结束当次循环