移动端六大语言速记:第2部分 - 控制结构

移动端六大语言速记:第2部分 - 控制结构

本文继续对比Java、Kotlin、Flutter(Dart)、Python、ArkTS和Swift这六种移动端开发语言的控制结构,帮助开发者快速掌握各语言的语法差异。

2. 控制结构

2.1 条件语句

各语言条件语句的语法对比:

语言 if语句 三元运算符 switch/case 空值处理
Java if (condition) {...} else if (condition) {...} else {...} condition ? value1 : value2 switch(var){case val: ... break;} if(obj != null)
Kotlin if (condition) {...} else if (condition) {...} else {...} if (condition) value1 else value2 when(var){value -> ...} ?., ?:, !!
Dart if (condition) {...} else if (condition) {...} else {...} condition ? value1 : value2 switch(var){case val: ... break;} ?., ??, !.
Python if condition: ... elif condition: ... else: ... value1 if condition else value2 无原生支持,用字典或if-elif if obj is not None
ArkTS if (condition) {...} else if (condition) {...} else {...} condition ? value1 : value2 switch(var){case val: ...} ?., ??
Swift if condition {...} else if condition {...} else {...} condition ? value1 : value2 switch var {case val: ...} ?, ??, !
示例对比

Java:

// if-else语句
int score = 85;
String grade;

if (score >= 90) {
   
    grade = "A";
} else if (score >= 80) {
   
    grade = "B";
} else if (score >= 70) {
   
    grade = "C";
} else {
   
    grade = "D";
}

// 三元运算符
boolean isPassed = score >= 60 ? true : false;

// switch语句
int day = 3;
String dayName;

switch (day) {
   
    case 1:
        dayName = "Monday";
        break;
    case 2:
        dayName = "Tuesday";
        break;
    case 3:
        dayName = "Wednesday";
        break;
    // ... 其他情况
    default:
        dayName = "Invalid day";
}

// 空值处理
String text = null;
if (text != null) {
   
    System.out.println(text.length());
}

Kotlin:

// if-else语句
val score = 85
val grade = if (score >= 90) {
   
    "A"
} else if (score >= 80) {
   
    "B"
} else if (score >= 70) {
   
    "C"
} else {
   
    "D"
}

// Kotlin使用if-else作为表达式
val isPassed = if (score >= 60) true else false

// when语句 (类似switch)
val day = 3
val dayName = when (day) {
   
    1 -> "Monday"
    2 -> "Tuesday"
    3 -> "Wednesday"
    // ... 其他情况
    else -> "Invalid day"
}

// 空值处理
val text: String? = null
// 安全调用操作符
val length = text?.length  // 如果text为null,则length为null
// Elvis操作符
val nonNullLength = text?.length ?: 0  // 如果text为null,则nonNullLength为0
// 非空断言操作符
try {
   
    val forcedLength = text!!.length  // 如果text为null,抛出NullPointerException
} catch (e: Exception) {
   
    println("Caught exception: $e")
}

Dart:

// if-else语句
int score = 85;
String grade;

if (score >= 90) {
   
    grade = "A";
} else if (score >= 80) {
   
    grade = "B";
} else if (score >= 70) {
   
    grade = "C";
} else {
   
    grade = "D";
}

// 三元运算符
bool isPassed = score >= 60 ? true : false;

// switch语句
int day = 3;
String dayName;

switch (day) {
   
    case 1:
        dayName = "Monday";
        break;
    case 2:
        dayName = "Tuesday";
        break;
    case 3:
        dayName = "Wednesday";
        break;
    // ... 其他情况
    default:
        dayName = "Invalid day";
}

// 空值处理
String? text = null;
// 安全调用操作符
int? length = text?.length;  // 如果text为null,则length为null
// 空值合并操作符
int nonNullLength = text?.length ?? 0;  // 如果text为null,则nonNullLength为0

Python:

# if-else语句
score = 85

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

键盘小码哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值