java入门-分支与循环

分支结构

img

分支结构是解决程序选取分支走不同的路线,增加了程序开发的灵活性。java的分支主要由if和switch这两种结构实现。

if 语句

if是分支中最常用的语法,它从形式上有三种形态:

if 结构

结构

img

语法

if(表达式){

//代码块

}

程序案例
int x = 3;
System.out.println("if start");
if(x>3) {
	System.out.println("x>3");
}
System.out.println("first if end");
if(x==3) {
	System.out.println("x==3");
}
System.out.println("last if end");

运行结果

if start
first if end
x==3
last if end

if … else

选择分支,有两条分支。

结构

img

语法

if(表达式){

​ //代码块

}else{

//代码块

}

程序案例
int x = 3;
System.out.println("if start");
if (x >= 3) {
	System.out.println("x>=3");
} else {
	System.out.println("x<3");
}
System.out.println("if end");

运行结果

if start
x>=3
if end

多分支

有多个分支结果,扩展的if … else

结构

img

语法

if(表达式){

//代码块

}else if(表达式){

​ //代码块

}else{

//代码块

}

程序案例
int x = 3;
System.out.println("if start");
if (x > 3) {
	System.out.println("x >= 3");
} else if (x < 3) {
	System.out.println("x < 3");
} else {
	System.out.println("x == 3");
}
System.out.println("if end");
运行结果
if start
x>=3
if end

switch语句

如果多分支的条件是整数或者字符串,可以使用switch。switch语句更加结构化。

结构

img

语法

int key = 1; //定义整型key

switch (key) {

case 1:

​ //代码块

break;

case 2:

​ //代码块

break;

default:

break;

}

程序案例

Scanner sc = new Scanner(System.in);
    System.out.print("请输入学生成绩: ");
    int score = sc.nextInt();
    int key = score / 10;
    switch (key) {
    case 10:
    case 9:
        System.out.println("A");
        break;
    case 8:
        System.out.println("B");
        break;
    case 7:
    case 6:
        System.out.println("C");
        break;
    case 5:
    case 4:
    case 3:
    case 2:
    case 1:
    case 0:
        System.out.println("D");
        break;
    default:
        System.out.println("不合法");
        break;
    }
sc.close();

运行结构

请输入学生成绩: 78
C

switch的问题

case穿透

case中没有写break语句,继续向下运行不做判断。

case 10:    //case 10 穿透进入case9
case 9:
    System.out.println("A");
break;
switch的支持类型

注意: switch的类型支持除long之外的所有整型,String类型和枚举类型。

img


循环结构

循环结构是指在程序中需要反复执行某个功能而设置的一种程序结构。java中循环有while和for两种形式。

while循环

按照循环条件判断的先后分为while循环和do…while循环。

while循环

先判断条件,成立进入循环体,否则结束循环。

结构

img

语法

while(表达式){

//循环体

}

程序案例
int count = 0;
System.out.println("while start:");
while(count < 10) {
	  count++;
	  System.out.println("count=" + count);
}
System.out.println("while end:");

运行结果

while start:
count=1
count=2
count=3
count=4
count=5
count=6
count=7
count=8
count=9
count=10
while end:

do…while

先进入循环体,后判断条件,成功继续执行,失败结束循环。

结构

do{

//循环体

}while(表达式);

程序案例
int count = 0;
System.out.println("while start:");
do {
	count++;
	System.out.println("count=" + count);
} while (count < 10);
System.out.println("while end:");
运行结构
while start:
count=1
count=2
count=3
count=4
count=5
count=6
count=7
count=8
count=9
count=10
while end:

for循环

结构

img

语法

for(变量初始值; 条件; 更新变量值){

//循环体

}

程序案例

int s = 0;
for (int i = 0; i < 10; i++) {
	s += i;
}
System.out.println("s=" + s);

运行结果

s=45

几个关键字

break

作用: 跳出当前循环/跳出switch结构。

程序案例
 for (int i = 0; i < 10; i++) {
   if (i == 5) {
		 break;
}
    System.out.println(i);
 }
System.out.println("loop end");
运行结果
0
1
2
3
4
loop end

continue

作用: 跳过当前循环的变量,执行更新的变量,即跳过当此循环到下一次。

程序案例
for (int i = 0; i < 10; i++) {
	if (i == 5) {
		continue;
	}
System.out.println(i);
}
System.out.println("loop end");

运行结果

0
1
2
3
4
6
7
8
9
loop end

return

作用: return一方面用在循环语句中来结束循环,另一方面用来终止函数的执行或者退出类的方法,并把控制权返回该方法的调用者。

如果方法有返回类型,则return的返回该类型的值;如果没有返回值,则可以使用没有返回值的return的语句。

程序案例
for (int i = 0; i < 10; i++) {
	if (i == 5) {
		break;
	}
	System.out.println(i);
}
System.out.println("loop end");

运行结果

0
1
2
3
4

循环嵌套

在实际开发中会在循环中嵌套循环,即循环体依然是循环,熟练掌握嵌套循环可以提升编码能力,解决更复杂的问题。

案例1

案例1:使用*号打印一个三角形

img

for(int i =1;i<9;i++) {
	for(int j = 1; j<=i;j++) {
		System.out.print("*");
	}
	System.out.println();
}

运行结果

img

案例2

打印一个9x9乘法表

img

for (int i = 1; i <=9; i++) {
	for (int j = 1; j <= i; j++) {
		System.out.print(j + "x" + i + "=" + (j * i) + "\t");
	}
	System.out.println();
}

运行结果

img

  • 16
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值