Conditional Logic in Rust - If-Else-Else-if Statements in RUST

In system programming, you can use the if and if..else statements/evolution to run a block of code when a specific condition is met.

For example, we suggest a student can be assigned grades to a subject based on their in total score.

  • if the score is upon 80, then assign grade A
  • if the score is upon 70, then assign grade B
  • if the score is upon 60, then assign grade C

Boolean Expression In Rust

At First we study about if..else evolutions, let’s speedily understand boolean evolutions.

A boolean evolution is an evolution (produces a value) which returns other true or false (boolean) as the output. For example-

fn main() {
    // Define a variable 'x' and assign it the value 7
    let x = 7;

    // Create a boolean variable 'condition' by checking if 'x' is greater than 5
    let condition = x > 5;

    // Print the value of 'condition'
    println!("condition is {}", condition);
}

Output :

condition is true

Above Example, x > 5 is a boolean evolution that checks if the value of variable x is greater than 5. as like 7, the value of variable x is greater than 5, and the condition variable is assigned true.

Here condition is true is seen as output.

if Expression In Rust

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

if condition {
    // code block to execute
}

If the situation evaluates to

  • true - the example code inside the if block is realized
  • false - the example code inside of the if block is not realized

Working of if expression in Rust (Condition is true)

fn main() {
    let number = 42;

    let result_message = if number > 0 {
        "The number is positive"
    } else {
        "The number is non-positive"
    };

    println!("{}", result_message);
}

**Output : **

The number is positive

Working of if expression in Rust (Condition is false)

fn main() {
    let number = -5;

    if number > 0 {
        println!("The number is positive");
    } else {
        println!("The number is non-positive");
    }

    println!("This statement is always executed");
}

Output :

The number is non-positive
This statement is always executed

Example: if expression in Rust

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")
}

Output :

10 is greater than 0
End of program

In the example upon, we maked a variable number and prevented if its value is greater than 0. Notice the condition.

number > 0

While the variable number is greater than 0, the agreement evaluates to true. As a result, you see the code block inside of curly braces being evolved.

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

Suppose we alternative the number variable to a negative integer. Let’s we say -2.

let number = -2;

Now we see output will be:

End of program

This is the condition -2 > 0 appreciate to false and the body of if is scamper.

if..else Expressions In Rust

The if evolution can once and again also include an optional else evolution. The else evolution is realized if the condition in if is false .

The syntax for the if..else evolution in Rust is :-

if condition {
    // executes when condition is true
} else {
    // executes when condition is false
}
  1. If agreement evaluates to true,
  • the code block inside if is evolved
  • the code block inside else is scamper
  1. If agreement evaluates to false,
  • the code block inside the if block is scamper
  • the code block inside the else block is evolved

Working of if..else expression in Rust ( if condition is true)

fn main() {
    let number = 42;

    if number > 0 {
        println!("The number is positive");
    } else {
        println!("The number is non-positive");
    }
}

Working of if..else expression in Rust ( if condition is true)

fn main() {
    let number = 5;

    if number > 10 {
        println!("The number is greater than 10");
    } else {
        println!("The number is not greater than 10");
    }
}

Example: if..else expression In Rust

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);
   }
}

Output :

-2 is less than or equal to 0

Below, the variable number has a value of -2, so the agreement number > 0 evaluates to false. Here, the code block under of the else statement is evolved.

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

let number = 10;

Now we found The output of the program will be:

Above the example, the condition number > 0 evaluates to true. Here, the code block inside of the if statement is evolved.

if..else if Expressions In Rust

You can appreciate multiple conditions by mix if and else in an else if evolution. An if..else if evolution is particularly helpful if we need to create more than two choices. The syntax for if with else if evolution looks like below:

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

Below -

1. If condition1 values to true

  • code block 1 is evolved
  • code block 2 and code block 3 is scamper
fn main() {
    let number = 5;

    if number > 0 {
        println!("Number is positive");
    } else if number < 0 {
        println!("Number is negative");
    } else {
        println!("Number is zero");
    }
}

2. If condition2 values to true

  • code block 2 is evolved
  • code block 1 and code block 3 is scamper
fn main() {
    let number = -5;

    if number > 0 {
        println!("Number is positive");
    } else if number < 0 {
        println!("Number is negative");
    } else {
        println!("Number is zero");
    }
}

3. If both condition1 & condition2 value to false

  • code block 3 is evolved
  • code block 1 and code block 2 is scamper
fn main() {
    let number = 0;

    if number > 0 {
        println!("Number is positive");
    } else if number < 0 {
        println!("Number is negative");
    } else {
        println!("Number is zero");
    }
}

Example: if..else if..else Conditional In Rust

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);
    }
}

Output :

-2 is negative

In above example, you check if a number is positive, negative or zero. From number = -2 is less than 0 who content the condition: number < 0, the else if block is evolved.

Nested if..else In Rust

We can also use if..else evolution under the body of other if..else evolutions ,It is known as nested if..else in Rust Programming.

if outer_condition {
    // outer code block

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

// outside if code block

Example: Nested if..else In Rust

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);
        }
    }
}

Output :

The current number is -2

Please see above example :-

  • The outer_condition number < 0 appreciates to true as the number variable is attributed to a value of -2. this, the outer code block is appreciate.

  • The ostensible code block contains an inner_condition to check number == -2, who is again true. this, the inner code block of the inner if evolution is realized.

  • The inner code block impress “The current number is -2” to on the screen.

  • The inner else block is scamper because the inner_condition appreciated to true.

Previous
Rust Operators: A Comprehensive Guide to Efficient Code Handling
Next
A Comprehensive Guide to Rust for, while, and loop Constructs