Rust If-else Statement


Rust Programming


Rust Programming


Rust If Statement


An if expression executes the code block only if the condition is true. The syntax of the if expression in Rust is:

if condition {
    // code block to execute
}{codeBox}


If the condition evaluates to

  • true - The code inside the if block is executed
  • false - The code inside of the if block is not executed

Example

fn main() {
    let number = 10;
   
    // condition to check if number is greater than zero
    if number > 0 {
        println!("{} is greater than 0", number);
    }
   
    println!("End of program")
}{codeBox}

Output

10 is greater than 0
End of program{codeBox}


In the example above, we created a variable number and checked whether its value is greater than 0. Notice the condition,

number > 0{codeBox}

Since the variable number is greater than 0, the condition evaluates to true. As a result, we see the code block inside of curly braces being executed.

println!("{} is greater than 0", number); {codeBox}

Suppose we change the number variable to a negative integer. Let's say -2.

let number = -2;{codeBox}

The output will be:

End of program{codeBox}

This is because the condition -2 > 0 evaluates to false and the body of if is skipped.


Rust if..else Expressions


The if expression can occasionally also include an optional else expression. The else expression is executed if the condition in if is false.

The syntax for the if..else expression in Rust is:

if condition {
    // executes when condition is true
} else {
    // executes when condition is false
}{codeBox}

1. If condition evaluates to true,


  • The code block inside if is executed
  • The code block inside else is skipped

2. If condition evaluates to false,


  • The code block inside the if block is skipped
  • The code block inside the else block is executed

Example: if..else expression

fn main() {
   let number = -2;
   
   // condition to check if number is greater than zero
   if number > 0 {
       println!("{} is greater than 0", number);
   } else {
       println!("{} is less than or equal to 0", number);
   }
}{codeBox}

Output

-2 is less than or equal to 0{codeBox}


Here, the variable number has a value of -2, so the condition number > 0 evaluates to false. Hence, the code block inside of the else statement is executed.

If we change the variable to a positive number. Let's say 10.

let number = 10;{codeBox}

The output of the program will be:

10 is greater than 0{codeBox}

Here, the condition number > 0 evaluates to true. Hence, the code block inside of the if statement is executed.


Rust if..else if Expressions


We can evaluate multiple conditions by combining if and else in an else if expression. An if..else if expression is particularly helpful if you need to make more than two choices. The syntax for if with else if expression looks like this:

if condition1 {
    // code block 1
} else if condition2 {
    // code block 2
} else {
    // code block 3
}{codeBox}


1. If condition1 evaluates to true,

  • Code block 1 is executed
  • Code block 2 and code block 3 is skipped

2. If condition2 evaluates to true,

  • Code block 2 is executed
  • Code block 1 and code block 3 is skipped

3. If both condition1 and condition2 evaluate to false,

  • Code block 3 is executed
  • Code block 1 and code block 2 is skipped

Example: if..else if..else Conditional

fn main() {
    let number = -2;
   
    if number > 0 {
        println!("{} is positive", number);
    } else if number < 0 {
        println!("{} is negative", number);
    } else {
        println!("{} is equal to 0", number);
    }
}{codeBox}

Output

-2 is negative{codeBox}

In this example, we check whether a number is positive, negative or zero. Because number = -2 is less than 0 which satisfies the condition: number < 0, the else if block is executed.


Nested if..else


You can use if..else expressions inside the body of other if..else expressions. It is known as nested if..else in Rust.

if outer_condition {
    // outer code block
    if inner_condition {
        // inner code block 1
    } else {
        // inner code block 2
    }
}

// outside if code block{codeBox}


Example: Nested if..else

fn main() {
    let number = -2;
    
    if number < 0 {
        // if outer condition evaluates to true evaluate the inner condition
        if number == -2 {
            println!("The current number is -2");
        } else {
            println!("The current number is {}", number);
        }
    }
}{codeBox}

Output

The current number is -2{codeBox}


In this example,

  • The outer_condition number < 0 evaluates to true as the number variable is assigned to a value of -2. Thus, the outer code block is evaluated.
  • The outer code block contains an inner_condition to check number == -2, which is again true. Thus, the inner code block of the inner if expression is executed.
  • The inner code block prints "The current number is -2" to the screen.
  • The inner else block is skipped because the inner_condition evaluated to true.


Credits & Copyrights©: Javatpoints & programiz

Post a Comment