if else判断语句
int x = 5;
int y;
if (x>= 3) {
y = 2 * x + 1;
} else if (x >= ‐1 && x < 3) {
y = 2 * x;
} else {
y = 2 * x ‐ 1;
}
switch选择语句 :在switch语句中,如果case的后面不写break,将出现穿透现象,也就是不会在判断下一个case的值,直接向后运 行,直到遇到break,或者整体switch结束。
int weekday = 6;
//switch语句实现选择
switch(weekday) {
case 1:
System.out.println("星期一");
break;
case 6:
System.out.println("星期六");
break;
}
for循环语句
for(int min = 0, max = arr.length ‐ 1; min <= max; min++, max‐‐) {
int temp = arr[min];
arr[min] = arr[max];
arr[max] = temp;
}
while循环语句
int i = 1;
//循环条件<=10
while(i<=10){
System.out.println("HelloWorld");
//步进
i++;
}
do while循环语句:无条件执行一次方法体
int x=1;
do {
System.out.println("HelloWorld");
x++;
}while(x<=10);
for 和 while 的小区别: 控制条件语句所控制的那个变量,在for循环结束后,就不能再被访问到了,而while循环结束还可以继 续使用,如果你想继续使用,就用while,否则推荐使用for。原因是for循环结束,该变量就从内存中消 失,能够提高内存的使用效率。 在已知循环次数的时候使用推荐使用for,循环次数未知的时推荐使用while。
跳出语句break,continue
跳出语句
break
使用场景:终止switch或者循环
continue
使用场景:结束本次循环,继续下一次的循环