while循环
while(循环条件1){
//循环操作1
while(循环条件2){
//循环操作2
}
}
do while 循环
do{
//循环操作1
do{
//循环操作2
}while(循环条件2);
}while(循环条件1);
for 循环
for(循环条件1){
//循环操作1
for(循环条件2){
//循环操作2
}
}
多重循环的使用
各循环可互相嵌套
一般不超过三层
外层循环变量变化一次,内层循环变量要变化一遍
使用双重循环输出九九乘法表
//wuhle 循环
public static void main(String[] args) {
int a = 1;
while (a <= 9) {
int b = 1;
while(b <= a) {
System.out.print(b+"*"+a+"="+b*a+" ");
b++;
}
a++;
System.out.println(" ");
}
}
//for 循环
for(int a = 1;a <= 9;a++) {
for(int b = 1;b <= a;b++) {
System.out.print(b+"*"+a+"="+b*a+" ");
}
System.out.println();
}
break语句语法
改变程序控制流
用于do-while、while、for循环中,可跳出循环而执行循环后面的语句
什么是continue语句
只能用在循环里
作用:跳过循环体中剩余的语句而执行下一次循环
为什么需要程序调试
编写程序过程中有时出现错误,但不好发现和定位错误,有没有好的方法呢?
通过代码阅读或者加输出语句查找程序错误
当程序结构越来越复杂时,需要专门的技术来发现和定位错误,就是“程序调试”
练习题:打印数列
使用循环输出 100、95、90、85.......5(使用三种循环输出)
public static void main(String[] args) {
//for 循环
for (int i = 100; i >= 5;i -=5) {
System.out.println(i);
}
//while 循环
int a = 100;
while(a >= 5) {
System.out.println(a);
a -=5;
}
//do while 循环
int b = 100;
do {
System.out.println(b);
b -=5;
}while(b >= 5);
}