开发工具与关键技术: Eclipse 语句
撰写时间:2020年 12 月 24日
一、switch语句特点
1、switch语句选择的类型只有四种:byte,short,int,char。
2、case之间与default没有顺序。先执行第一个case,没有匹配的case执行default。
3、结束switch语句的两种情况:遇到break,执行switch语句结束。
4、如果匹配的case或者default没有对应的break,那么程序会继续向下执行,运行可以执行的语句,直到遇到break或者switch结尾结束。
二、if和switch语句很像,具体什么场景下,应用哪个语句呢?
如果判断的具体数值不多,而是符合byte short int char这四种类型。
虽然两个语句都可以使用,建议使用switch语句,因为效率稍高。
其他情况:对区间判断,对结果为boolean类型判断,使用if,if的使用范围更广。
while:先判断条件,只有条件满足才执行循环体。
do while:先执行循环体,再判断条件,条件满足,再继续执行循环体。
简单一句话:do while:无论条件是否满足,循环体至少执行一次。
- class OtherDemo
- {
- public static void main(String[] args){
- w:for(int x=0;x<3;x++)
- {
- for(int y=0;y<4;y++)
- {
- System.out.printIn(“x=”+x);
- break w;
- }
- }
- //continue:只能作用于循环结构,继续循环。特点:结束本次循环,继续下一次循环。
- for(int x=1;x<=10;x++)
- {
- if(x%2==1)
- continue;
- System.out.printIn(“x=”+x);
- }
- w:for(int x=0;x<3;x++)
- {
- for(int y=0;y<4;y++)
- {
- System.out.printIn(“x=”+x);
- continue w;
- }
- }
- }
- class ForForTest2
- {
- public static void main(String[] args) {
- for (int x = 0; x < 5; x++)
- {
- for(int y=x+1;y<5;y++){
- Sysyem.out.print(" ");
- }
- for(int z=0;z<=x;z++){
- System.out.print("* );
- }
- System.out.printIn();
- }
- }
- }