##### 标题：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语句出现在另一个ifelse块的主体内时，它称为嵌套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