Java流程控制语句
1.判断语句if
1.1 if 语句第一种格式: if
if(关系表达式){
语句体;
}
public static void main(String[] args){
System.out.println("开始");
// 定义两个变量
int a = 10;
int b = 20;
//变量使用if判断
if (a == b){
System.out.println("a等于b");
}
int c = 10;
if(a == c){
System.out.println("a等于c");
}
System.out.println("结束");
}
1.2 判断语句2–if…else
if 语句第二种格式: if…else
if(关系表达式) {
语句体1;
}else {
语句体2;
}
public static void main(String[] args){
// 判断给定的数据是奇数还是偶数
// 定义变量
int a = 1;
if(a % 2 == 0) {
System.out.println("a是偶数");
} else{
System.out.println("a是奇数");
}
System.out.println("结束");
}
1.3 判断语句3–if…else if…else
if 语句第三种格式: if…else if …else
if (判断条件1) {
执行语句1;
} else if (判断条件2) {
执行语句2;
}
...
}else if (判断条件n) {
执行语句n;
} else {
执行语句n+1;
}
public static void main(String[] args) {
// x和y的关系满足如下:
// x>=3 y = 2x + 1;
//‐1<=x<3 y = 2x;
// x<=‐1 y = 2x – 1;
// 根据给定的x的值,计算出y的值并输出。
// 定义变量
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;
}
System.out.println("y的值是:"+y);
}
1.4 if 语句和三元运算符的互换
public static void main(String[] args) {
int a = 10;
int b = 20;
//定义变量,保存a和b的较大值
int c;
if(a > b) {
c = a;
} else {
c = b;
}
//可以上述功能改写为三元运算符形式
c = a > b ? a:b;
}
2. 选择语句
2.1选择语句–switch
switch 语句格式:
switch(表达式) {
case 常量值1:
语句体1;
break;
case 常量值2:
语句体2;
break;
...
default:
语句体n+1;
break;
}
public static void main(String[] args) {
//定义变量,判断是星期几
int weekday = 6;
//switch语句实现选择
switch(weekday) {
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
default:
System.out.println("你输入的数字有误");
break;
}
}
switch 语句中,表达式的数据类型,可以是byte,short,int,char,enum(枚举),JDK7后可以接收字符串
2.2 case 的穿透性
在switch语句中,如果case的后面不写break,将出现穿透现象,也就是不会在判断下一个case的值,直接向后运行,直到遇到break,或者整体switch结束。
public static void main(String[] args) {
int i = 5;
switch (i){
case 0:
System.out.println("执行case0");
break;
case 5:
System.out.println("执行case5");
case 10:
System.out.println("执行case10");
default:
System.out.println("执行default");
}
}
上述程序中,执行case5后,由于没有break语句,程序会一直向后走,不会在判断case,也不会理会break,直接运行完整体switch。
3. 循环语句
3.1 循环语句1–for
for(初始化表达式1; 布尔表达式2; 步进表达式3){
循环体③
}
public static void main(String[] args) {
//1.定义一个初始化变量,记录累加求和,初始值为0
int sum = 0;
//2.利用for循环获取1‐100之间的数字
for (int i = 1; i <= 100; i++) {
//3.判断获取的数组是奇数还是偶数
if(i % 2==0){
//4.如果是偶数就累加求和
sum += i;
}
}
//5.循环结束之后,打印累加结果
System.out.println("sum:"+sum);
}
3.2 循环语句2–while
while 循环语句格式:
初始化表达式①
while(布尔表达式②){
循环体③
步进表达式④
}
public static void main(String[] args) {
//使用while循环实现
//定义一个变量,记录累加求和
int sum = 0;
//定义初始化表达式
int i = 1;
//使用while循环让初始化表达式的值变化
while(i<=100){
//累加求和
sum += i ;
//步进表达式改变变量的值
i++; }
//打印求和的变量
System.out.println("1‐100的和是:"+sum);
}
3.3 循环语句3–do…while
do…while 循环格式
do{
循环体1
步进表达式2
}while(布尔表达式3);
public static void main(String[] args) {
int x=1;
do {
System.out.println("HelloWorld");
x++;
}while(x<=10);
}
do…while循环的特点:无条件执行一次循环体,即使我们将循环条件直接写成false,也依然会循环一次。这样的循环具有一定的风险性,因此初学者不建议使用do…while循环。
public static void main(String[] args){
do{
System.out.println("无条件执行一次");
}while(false);
4. 跳出循环
4.1 break
使用场景:终止 switch或者循环
在选择结构 switch语句中
public static void main(String[] args) {
for (int i = 1; i<=10; i++) {
//需求:打印完两次HelloWorld之后结束循环
if(i == 3){
break;
}
System.out.println("HelloWorld"+i);
}
}
4.2 continue
使用场景:结束本次循环,继续下一次的循环
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
//需求:不打印第三次HelloWorld
if(i == 3){
continue;
}
System.out.println("HelloWorld"+i);
}
}