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 例如
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
表达式用于在不满足特定条件时运行代码块。