##### 标题：Rust if语句

`if`语句确定条件是否为真。如果条件为`true`，则执行`if`块，否则，控制会跳过`if`块。

• if块
• if-else块
• if else-if阶梯块
• 嵌套if语句

## if语句

`if`语句块的语法 -

``````if condition
{
//block statements;
}
``````

`if`语句的流程图 -

``````fn main(){

let a=1;
if a==1
{
println!("a is equal to 1");
}
}
``````

``````a is equal to 1
``````

## if-else语句

if-else语句的语法

``````if condition
{
//block statements
}else{
//block statements
}
``````

if-else的流程图

``````fn main()
{
let a=3;
let b=4;
if a>b
{
println!("a is greater than b");
}
else
{
println!("a is smaller than b");
}
}
``````

``````a is smaller than b
``````

## else-if语句

`else-if`语句的语法 -

``````if condition 1
{
//block statements
}else if condition 2
{
//block statements
}
.
.
else{
//block statements
}
``````

`else if`语句块的流程图 -

``````fn main(){

let num= -5;
if num>0
{
println!("number is greater than 0");
}
else if num<0
{
println!("number is less than 0 ");
}
else
{
println!("number is not equal to 0");
}
}
``````

``````number is less than 0
``````

## 嵌套if-else语句

`if-else`语句出现在另一个`if``else`块的主体内时，它称为嵌套`if-else`

``````if condition 1
{
// 嵌套if/else块
if condition 2
{
//block statements
}else
{
//block statements
}
}else{
//block statements
}
``````

``````fn main(){

let a=5;
let b=6;
if a!=b
{
if a>b
{
println!("a is greater than b");
}else{
println!("a is less than b");
}
}

else
{
println!("a is equal to b");
}
}
``````

``````a is less than b
``````