Scott の 博客 Scott の 博客
首页
  • Data Structure and Algorithm
  • Java
  • 面试
  • Drafts
  • C++
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Scott

恋爱中
首页
  • Data Structure and Algorithm
  • Java
  • 面试
  • Drafts
  • C++
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Data Structure and Algorithm

  • Java

    • Java基础知识&面试题总结
    • Java
    • Control Flow
      • Comparison Operators
      • Logical Operators
      • If Statements
      • The Ternary Operator
      • Switch Statements
      • For Loops
      • While Loops
      • Do..While Loops
      • Break and Continue Statements
      • For-Each Loop
    • Clean Coding
    • Debugging and Deployment
    • Untitled
    • Refactor towards OOSD
    • Inheritance
    • Untitled
    • Exceptions
    • Generics
    • Collections
    • Lambda-Expression
    • Streams
    • Concurrency and Multi-threading
    • The Executive Framework
    • 4
    • 1
  • c++

  • 面试

  • Bilibili_Java

  • Python

  • All kinds of Drafts

  • High Integrity Information System

  • 左神算法课

  • 个人笔记
  • Java
Scott
2021-12-06
目录

Control Flow

  1. Comparison Operators
  2. Logical Operators
  3. If Statements
  4. The Ternary Operator
  5. Switch Statements
  6. For Loops
  7. While Loops
  8. Do..While Loops
  9. Break and Continue Statements
  10. For-Each Loop

# Comparison Operators

int x = 1; 
int y = 1;
boolean equal = (x == y); // true 
1
2
3

# Logical Operators

int temperature = 22;
boolean isWarm = temperature > 20 && temperature < 30; // true
boolean notWarm = temperature < 20 || temperature > 30; // true
boolean warm = !notWarm;
1
2
3
4

# If Statements

if (condition_1)
    // blablabla
else if (condition_2)
    // blablabla
else
    // blablabla
    
boolean hasHighIncome = income > 100_000 ? true : false; // ❌
boolean hasHighIncome = income > 100_000; // ✅


// FizzBuzz 
if (isFizz(number) && isBuzz(number))
    sout("FizzBuzz");
else if (isFizz(number)) // even though there is repetition, its more readable and understandable than nested structure
    sout("Fizz");
else if (isBuzz(number))
    sout("Buzz");
else 
    sout(number);

public int isFizz(int number) {
    return number % 5 == 0; 
}

public int isBuzz(int number) {
    return number % 3 == 0; 
}
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

# The Ternary Operator

String className = (income > 100_000) ? "high" : "low";
1

# Switch Statements

switch (role) {
    case "case_1":
        // blablabla
        break;
    case "case_2":
        // blablabla
        break; 
    ...
    default:
        // blablabla
}

// could be number
switch (role) {
    case 1:
        // blablabla
        break;
    case 2:
        // blablabla
        break; 
    ...
    default:
        // blablabla
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# For Loops

for (int i = 0; i < something; i++) {
    // blablabla
}
1
2
3

# While Loops

int i = 100;
while (i > 0) {
    // blablabla
    i --; 
}

// running indefinitely 
Scanner scanner = new Scanner(System.in);
String input = "";
while (!input.equals("quit")) {
    input = scanner.next().toLowerCase();
    sout(input);
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# Do..While Loops

Scanner scanner = new Scanner(System.in);
String input = "";

do {
    input = scanner.next().toLowerCase();
    sout(input);
} while (!input.equals("quit"))
1
2
3
4
5
6
7

# Break and Continue Statements

Scanner scanner = new Scanner(System.in);
String input = "";
while (true) { // condition can just be true
    input = scanner.next().toLowerCase();
    if (input.equals("quit"))
        break;
    if (input.equals("pass"))
        continue;
    sout(input);
}
1
2
3
4
5
6
7
8
9
10

# For-Each Loop

limitations:

  1. always forward going
  2. doesn't has access to index of items
String [] array = {"1", "2", "3"};

for (String number : array)
    sout(number);

1
2
3
4
5
上次更新: 2021/12/31, 15:34:56
Java
Clean Coding

← Java Clean Coding→

最近更新
01
day01-Java基础语法
08-31
02
1
08-29
03
路线
08-01
更多文章>
Theme by Vdoing | Copyright © 2019-2022 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×