for循环
格式
代码语言:javascript复制for(初始化值1;循环终止条件2;循环体执行后的语句4){
循环体3;
}
代码语言:javascript复制for (int i = 0; i <= 10; i ) {
System.out.println(i);
}
流程
初始化值1
->循环终止条件2
「true」->循环体3
->循环体执行后的语句4
->循环终止条件2
「true」->循环体3
->循环体执行后的语句4
->…循环终止条件2
「false」-> 循环结束
增强for循环
for(内部元素类型 变量名;集合){}
普通方式实现
代码语言:javascript复制int[] arr = {100, 24, 90, 78};
for (int i = 0; i < arr.length; i ) {
System.out.println(arr[i]);
}
增强方式实现
- 优点:代码少
- 缺点:不能直接获取索引
for (int i : arr) {
System.out.println(i);
}
求和
代码语言:javascript复制package com.zhongxin.loop;
public class ForDemo2 {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i <= 10; i ) {
sum = i;
}
System.out.println(sum);
}
}
while循环
代码语言:javascript复制package com.zhongxin.loop;
public class WhileDemo {
public static void main(String[] args) {
int i = 1; // 初始化语句1
while (i <= 10) { // 循环执行条件2
System.out.println(i); // 循环体3
i ; // 循环体执行完毕之后的语句4
}
}
}
流程:1234->2(true)->34->2(true)->......->2(false)->循环结束
Do…While
至少会执行一次循环体,其他和while
一样
package com.zhongxin.loop;
public class DoWhileDemo {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i ;
} while (i <= 10);
}
}
break
代码语言:javascript复制跳出整个循环
for (int i = 1; i <= 10; i ) {
if (i == 3) {
break;
}
System.out.println(i);
}
// 1
// 2
continue
代码语言:javascript复制结束本次循环,继续下次循环
package com.zhongxin.loop;
public class BreakContinueDemo {
public static void main(String[] args) {
for (int i = 1; i <= 10; i ) {
if (i == 3) {
continue;
}
System.out.println(i);
}
}
}
// 1
// 2
// 4
// 5
// 6
// 7
// 8
// 9
// 10
条件控制语句
- switch
- if … else
- if else if … else
- if … if … if
if
代码语言:javascript复制package com.zhongxin.ifdemo;
public class IFDemo {
public static void main(String[] args) {
int age = 17;
if (age >= 18) {
System.out.println("恭喜你成年了");
}
if (age >= 18) {
System.out.println("可以访问本网站");
}else{
System.out.println("FBI WARNING!");
}
int score = 60;
if (score == 100) {
System.out.println("满分");
}else if (score>=90){
System.out.println("优秀");
}else if (score>=60){
System.out.println("及格");
}else {
System.out.println("不及格");
}
}
}
switch
优点
效率比if快
结束的条件
- switch
- 右大括号
变量类型
- <= int
- String
- 枚举
package com.zhongxin.ifdemo;
public class SwitchDemo {
public static void main(String[] args) {
int i = 1;
switch (i) {
case 1:
System.out.println("1");
break;
case 2:
System.out.println("2");
break;
default:
System.out.println("未知");
break;
}
}
}
不使用break 形成穿透
package com.zhongxin.ifdemo;
public class SwitchDemo2 {
public static void main(String[] args) {
int mouth = 12;
switch (mouth) {
case 3:
case 4:
case 5:
System.out.println("春");
break;
case 6:
case 7:
case 8:
System.out.println("夏");
break;
case 9:
case 10:
case 11:
System.out.println("秋");
break;
case 12:
case 1:
case 2:
System.out.println("冬");
break;
default:
System.out.println("未知");
break;
}
}
}
练习
打印
代码语言:javascript复制*
**
***
****
*****
代码语言:javascript复制package com.zhongxin.ifdemo;
public class Homework {
public static void main(String[] args) {
String start = "*";
for (int i = 1; i < 6; i ) {
for (int j = 0; j < i; j ) {
System.out.print(start);
}
System.out.println("");
}
}
}
打印
代码语言:javascript复制 *
* *
* * *
* * * *
* * * * *
代码语言:javascript复制package com.zhongxin.ifdemo;
public class Homework1 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i ) {
for (int j = 0; j < 5 - i; j ) {
System.out.print(" ");
}
for (int x = 1; x <= i * 2 - 1; x = 2) {
System.out.print("* ");
}
System.out.println();
}
}
}