好久没有写了,原因是最近在加班中,每天十二点睡六点起真是巨痛苦....
本次的主题是while和do...while,因为是拾遗,所以关键字暂时就到这里,像if判断,switch多重判断,finally、final和finalize的区别,这些一些稍微基础些,或synchronized
等一些跟后面联系紧密的,会在后面提一下。回归主题吧,while的作用与for是一样的用于循环,不同的是for也许是能够更加直观地看到循环的次数吧,其实在上一篇for循环使用迭代器时也没直观地看到循环次数,所以我觉得这两个循环差异应该不是很大。说道do...while和while还是有一个区别的,就是第一循环时后者在执行时先走判断,不成立则不执行,而前者肯定是要执行一次的,只讲区别了,至于语法,还是贴代码吧:
public class WhileDemo {
public static void test(){
int i=0;
int j=0;
int count=0;
int count01=0;
while(i<3){
count++;
i++;
}
do {
count01++;
j++;
} while (j<3);
System.out.println(count);
System.out.println("==============");
System.out.println(count01);
}
public static void test01(){
int i=0;
int j=0;
int count=0;
int count01=0;
while(i==3){
count++;
i++;
}
do {
count01++;
j++;
} while (j==3);
System.out.println(count);
System.out.println("==============");
System.out.println(count01);
}
public static void main(String[] args) {
WhileDemo.test();
WhileDemo.test01();
}
}
运行结果为:
3
==============
3
0
==============
1
ok,结果摆在那个,我就不赘述了