public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 121;
if(a>18){
System.out.println("aaa");
}else if (a>30){
System.out.println("bbb");
}else if (a>40){
System.out.println("ccc");
}else{
System.out.println("ddd");
}
}
结果只会输出aaa
满足一个条件执行以后就跳出循环.
switch 语句
public static void main(String[] args) {
// TODO Auto-generated method stub
char c ='a';
switch(c){ //括号里边是条件表达式
case 'a': //记得后边是冒号
System.out.println("aa");
break; // 如果没有break 就会一直执行 连bb cc 都会输出
case 'b':
System.out.println("bb");
case 'c':
System.out.println("cc");
}
}
注意 括号里边的 条件表达式 数据类型 必须和 case 里边的常量表达式类型相同
举个栗子 int a = 100 case a>=90&&a<100; 编译出错 case 后边是个 boolean 值
可以用的数据类型 byte short int char enum 等 比如 case 13.5 编译报错 因为是float
循环语句
for没啥说的
while
int i=0;
while(i<10){
System.out....;
i++;
}
do while 循环
int i=0;
do{
system.....;
i++}
while(i<10);
do 后边是{ } 不是 括号