学习Rust的第5天:控制流

Control flow, as the name suggests controls the flow of the program, based on a condition.
控制流,顾名思义,根据条件控制程序的流。

If expression If表达式

An if expression is used when you want to execute a block of code if a condition is met.
当您希望在满足条件的情况下执行代码块时,将使用 if 表达式。

Example 例如

ba 复制代码
fn main(){
  let age: u32 = 17;
  if age > 18{
    println!("You are an adult");
  }
}

This program will check if the age is greater than 18 or not. if yes it will output "You are an adult".
该程序将检查年龄是否大于18岁。 if 是的,它会输出"你是一个成年人"。

Now what if I want to get an output when the condition is not met?
现在,如果我想在条件不满足时获得输出,该怎么办?

Else expression Else表达式

An else expression is used to run a block of code when a certain condition is not met.
else 表达式用于在不满足特定条件时运行代码块。

ba 复制代码
fn main(){
  let age: u32 = 17
  if age>18{
    println!("You are an adult");
  }else{
    println!("You are not an adult");
  }
}

This program will check if the age is greater than 18 or not. if yes it will output "You are an adult" else it will output "You are not an adult".
该程序将检查年龄是否大于18岁。 if 是的,它将输出"你是一个成年人",否则它将输出"你不是一个成年人"。

Else If Expression Else If表达式

An else if expression can be used to check for multiple conditions. for example :
else if 表达式可用于检查多个条件。例如:

ba 复制代码
fn main(){
  let number = 92;
  if number % 9 == 0{
    println!("number is divisible by 9");
  } else if number % 5 == 0{
    println!("number is divisible by 5");
  }else if number % 3 == 0{
    println!("number is divisible by 3");
  }else{
    println!("number is not divisible by 9, 5, 3");
  }
}

Loops 环

Loops are used to go over through a block of code till explicitly specified to stop or if a certain condition is met.
循环用于遍历代码块,直到明确指定停止或满足特定条件。

loop keyword loop 关键字

The loop keyword tells rust to run a block of code till told to stop using the break keyword
loop关键字告诉rust运行一段代码,直到停止使用 break 关键字

ba 复制代码
fn main() {
    let mut i: u32 = 0;
    let mut j: i32 = 10;
ax 复制代码
  // labelled infinite loop with break statements
    'counting_down: loop {
        if j >= 0 {
            println!("{}", j);
            j -= 1;
        } else {
            println!("counting down loop complete");
            break 'counting_down;
        }
    }
}

Explanation: 说明:

  • The main function is the entry point of the Rust program.
    main 函数是Rust程序的入口点。
  • j of type i32 (signed 32-bit integer) initialized with the value 10.
    类型 i32 (有符号32位整数)的 j ,初始化为值10。
  • The code enters a labeled infinite loop marked with the label 'counting_down.
    代码进入一个标记为 'counting_down 的带标签的无限循环。
  • Inside the loop, there's a conditional statement checking if j is greater than or equal to 0.
    在循环内部,有一个条件语句检查 j 是否大于或等于0。
  • If true, it prints the current value of j using println! and decrements j by 1.
    如果为true,则使用 println! 打印 j 的当前值,并将 j 递减1。
  • If false (when j is less than 0), it prints a message and breaks out of the loop labeled 'counting_down.
    如果为false(当 j 小于0时),它将打印一条消息并跳出标记为 'counting_down 的循环。
  • The loop continues indefinitely until the break 'counting_down; statement is executed.
    循环将无限期地继续,直到执行 break 'counting_down; 语句。
  • The label 'counting_down is used to specify which loop to break out of, especially when dealing with nested loops.
    标签 'counting_down 用于指定要中断哪个循环,特别是在处理嵌套循环时。

While loops While循环

A while loop repeatedly executes a block of code as long as a specified condition is true.
只要指定的条件为真, while 循环就会重复执行代码块。

Example: 范例:

ba 复制代码
fn main(){
  let mut num: u8 = 4;
  while num!=0 {
    println!("{}",num);
    num-=1;
  }
}

Explanation: 说明:

  • A mutable variable num is declared and initialized with the value 4. It has the type u8 (unsigned 8-bit integer).
    声明了一个可变变量 num ,并使用值4进行初始化。它的类型为 u8 (无符号8位整数)。
  • The code enters a while loop with the condition num != 0.
    代码进入一个带有条件 num != 0while 循环。
  • Inside the loop, it prints the current value of num using println!.
    在循环内部,它使用 println! 打印 num 的当前值。
  • It then decrements the value of num by 1 with the num -= 1; statement.
    然后使用 num -= 1; 语句将 num 的值减1。
  • The loop continues as long as the condition num != 0 is true.
    只要条件 num != 0 为真,循环就会继续。
  • The program prints the values of num in descending order from its initial value (4) until it becomes 0.
    程序按从初始值(4)到0的降序打印 num 的值。
  • Once num becomes 0, the loop exits, and the program continues to any subsequent code outside the loop.
    一旦 num 变为0,循环退出,程序继续执行循环外的任何后续代码。

For Loops for循环

A for loop iterates over a range, collection, or iterator, executing a block of code for each iteration.
for 循环遍历范围、集合或迭代器,每次迭代执行一个代码块。

Examples: 示例如下:

ba 复制代码
fn main(){
  //for loops in arrays
  let array: [u8; 10] = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
  println!("For loop to access array");
    for item in array {
        println!("{}", item);
    }
ax 复制代码
  //for loops in ranges
  println!("For loops in range ");
    for number in 0..=5 {
        println!("{number}");
    }
  println!("For loops in range (reversed)");
    for number in (0..=5).rev() {
        println!("{number}");
    }
}
相关推荐
爱上语文2 分钟前
Java LeetCode每日一题
java·开发语言·leetcode
bug菌25 分钟前
Java GUI编程进阶:多线程与并发处理的实战指南
java·后端·java ee
Манго нектар29 分钟前
JavaScript for循环语句
开发语言·前端·javascript
蒲公英100136 分钟前
vue3学习:axios输入城市名称查询该城市天气
前端·vue.js·学习
程序猿小D38 分钟前
第二百六十九节 JPA教程 - JPA查询OrderBy两个属性示例
java·开发语言·数据库·windows·jpa
训山42 分钟前
【11】纯血鸿蒙HarmonyOS NEXT星河版开发0基础学习笔记-模块化语法与自定义组件
笔记·学习·华为·harmonyos·鸿蒙系统
阿华的代码王国1 小时前
【JavaEE】——文件IO的应用
开发语言·python
satan–01 小时前
R语言的下载、安装及环境配置(Rstudio&VSCode)
开发语言·windows·vscode·r语言
①个程序员1 小时前
thinkphp 学习记录
学习
电饭叔1 小时前
《python语言程序设计》2018版第8章19题几何Rectangle2D类(下)-头疼的几何和数学
开发语言·python