不做大哥好多年 不做大哥好多年
首页
  • MySQL
  • Redis
  • Elasticsearch
  • Kafka
  • Etcd
  • MongoDB
  • TiDB
  • RabbitMQ
  • 01.GO基础
  • 02.面向对象
  • 03.并发编程
  • 04.常用库
  • 05.数据库操作
  • 06.Beego框架
  • 07.Beego商城
  • 08.GIN框架
  • 09.GIN论坛
  • 10.微服务
  • 01.Python基础
  • 02.Python模块
  • 03.Django
  • 04.Flask
  • 05.SYL
  • 06.Celery
  • 10.微服务
  • 01.Java基础
  • 02.面向对象
  • 03.Java进阶
  • 04.Web基础
  • 05.Spring框架
  • 100.微服务
  • Docker
  • K8S
  • 容器原理
  • Istio
  • 数据结构
  • 算法基础
  • 算法题分类
  • 前置知识
  • PyTorch
  • 01.Python
  • 02.GO
  • 03.Java
  • 04.业务问题
  • 05.关键技术
  • 06.项目常识
  • 10.计算机基础
  • Linux基础
  • Linux高级
  • Nginx
  • KeepAlive
  • ansible
  • zabbix
  • Shell
  • Linux内核

逍遥子

不做大哥好多年
首页
  • MySQL
  • Redis
  • Elasticsearch
  • Kafka
  • Etcd
  • MongoDB
  • TiDB
  • RabbitMQ
  • 01.GO基础
  • 02.面向对象
  • 03.并发编程
  • 04.常用库
  • 05.数据库操作
  • 06.Beego框架
  • 07.Beego商城
  • 08.GIN框架
  • 09.GIN论坛
  • 10.微服务
  • 01.Python基础
  • 02.Python模块
  • 03.Django
  • 04.Flask
  • 05.SYL
  • 06.Celery
  • 10.微服务
  • 01.Java基础
  • 02.面向对象
  • 03.Java进阶
  • 04.Web基础
  • 05.Spring框架
  • 100.微服务
  • Docker
  • K8S
  • 容器原理
  • Istio
  • 数据结构
  • 算法基础
  • 算法题分类
  • 前置知识
  • PyTorch
  • 01.Python
  • 02.GO
  • 03.Java
  • 04.业务问题
  • 05.关键技术
  • 06.项目常识
  • 10.计算机基础
  • Linux基础
  • Linux高级
  • Nginx
  • KeepAlive
  • ansible
  • zabbix
  • Shell
  • Linux内核
  • Java基础

    • 01.Java基础
    • 02.数字类型
    • 03.字符串
    • 04.循环
      • 01.if else
        • 1、if else
        • 2、三元运算
      • 02.switch 语句
      • 03.while 循环
        • 1、简单循环
        • 2、do while
        • 3、break
        • 4、continue
      • 04.for循环
        • 1、for
        • 2、for-each
        • 3、break
        • 4、continue
    • 05.数组
    • 06.ArrayList
    • 07.LinkedList
    • 08.HashSet
    • 09.HashMap
    • 10.Iterator
    • 11.泛型
  • 面向对象

  • Java进阶

  • Web基础

  • Spring框架

  • 微服务

  • Java
  • Java基础
xiaonaiqiang
2021-11-16
目录

04.循环

# 01.if else

# 1、if else

package com.case_01;

public class Test{
    public static void main(String args[]){
        int x = 30;
        int y = 10;

        if( x == 30 ){
            if( y == 10 ){
                System.out.println("X = 30 and Y = 10");
            }
            if (y < 10) {
                System.out.println("c");
            } else if (y < 20) {
                System.out.println("java");
            } else {
                System.out.println("python");
            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# 2、三元运算

package com.case_01;

public class Test{
    public static void main(String args[]){
        int time = 20;
        String result = (time < 18) ? "学习python" : "学习java";
        System.out.println(result);   // 学习java
    }
}
1
2
3
4
5
6
7
8
9

# 02.switch 语句

package com.case_01;

public class Test{
    public static void main(String args[]){
        int day = 4;
        switch (day) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            case 4:
                System.out.println("Thursday");
                break;
            case 5:
                System.out.println("Friday");
                break;
            case 6:
                System.out.println("Saturday");
                break;
            case 7:
                System.out.println("Sunday");
                break;
            default:
                System.out.println("没有匹配");
        }
        // 输出 "Thursday" (day = 4)
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

# 03.while 循环

# 1、简单循环

int i = 0;
while (i < 5) {
    System.out.println(i);
    i++;
}
1
2
3
4
5

# 2、do while

  • do while循环是while循环的变体。
  • 在检查条件是否为true之前,此循环将执行一次代码块,然后只要条件为true,它将重复该循环。
do {
  // 要执行的代码块
}
while (condition);
1
2
3
4
public class Test{
    public static void main(String args[]){
        int i = 0;
        do {
            System.out.println(i);
            i++;
        }
        while (i < 5);
    }
}
1
2
3
4
5
6
7
8
9
10

# 3、break

public class Test{
    public static void main(String args[]){
        int i = 0;
        while(i <= 100) {
            i++;
            if (i > 30) {
                System.out.println(i + "已经超过了30,不能在循环了");
                break;
            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

# 4、continue

public class Test{
    public static void main(String args[]){
        int i = 0;
        while(i < 100){
            i++;
            if(i%10 == 0){
                System.out.println();
                continue;
            }
            System.out.print(i);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 04.for循环

# 1、for

  • 此示例将只输出0到10之间的偶数:
for (int i = 0; i <= 10; i = i + 2) {
  System.out.println(i);
}
1
2
3

# 2、for-each

  • 以下示例使用for-each循环输出angs数组中的所有元素
public class Test{
    public static void main(String args[]){
        String[] langs = {"c", "java", "python", "cjavapy"};
        for (String i : langs) {
            System.out.println(i);
        }
    }
}
1
2
3
4
5
6
7
8

# 3、break

for (int i = 0; i < 10; i++) {
  if (i == 4) {
    break;
  }
  System.out.println(i);
}
1
2
3
4
5
6

# 4、continue

for (int i = 0; i < 10; i++) {
  if (i == 4) {
    continue;
  }
  System.out.println(i);
}
1
2
3
4
5
6
上次更新: 2024/5/31 11:18:42
03.字符串
05.数组

← 03.字符串 05.数组→

最近更新
01
04.数组双指针排序_子数组
03-25
02
08.动态规划
03-25
03
06.回溯算法
03-25
更多文章>
Theme by Vdoing | Copyright © 2019-2025 逍遥子 技术博客 京ICP备2021005373号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式