Open In App

Rust – If-else Statement

Last Updated : 09 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Branching with if-else in rust also is similar to other languages. Just the difference is the way of writing(syntax). Here the condition doesn’t need to be surrounded by parenthesis. Decision-making structure is an important part of life. Similarly, in programming also there are situations where you have to decide(make decisions) that for the specific conditions which code should be executed. For this, we use the if-else. 

Decision-making statements in programming languages decide the direction of the flow of program execution.

Mainly there are 4 types that are used more often are explained in this article.

They are:

  1. if statement
  2. if-else statement
  3. else-if ladder statement
  4. Nested if-else statement

If Statement

Here only 1 condition is there and if that condition is true then a block of code inside the if statement will be executed.

Syntax: 

if boolean_expression {
// statement(s) will execute if the boolean expression is true
}

Example: To check if the number is negative or positive.

Rust
fn main(){
   let num = -5;
   if num < 0 {
      println!("number is negative") ;
   }
}

Output:

number is negative

If-Else Statement

Here also there is only 1 condition but the difference here is that there is another block of code that is executed when the if the condition is false and suppose that the condition is true then the block of code inside if is executed and else part is ignored completely.

Syntax: 

if boolean_expression {
// if the boolean expression is true statement(s) will execute
} else {
//if the boolean expression is false statement(s) will execute
}

Example: To check which number is bigger

Rust
fn main(){
  let num = 3;
  let num1= -3;
   if num > num1 {
      println!("1st number is bigger") ;
   }
  else {
    println!("2nd number is bigger") ;
  }
}

Output:

1st number is bigger

Else-If Ladder Statement

In this, there is more than 1 condition. In fact, there can be many and after that, there is else now suppose any of the conditions does not satisfy then all are ignored and block of code inside else part will be executed. But suppose if some condition is satisfied then the block of code in that part will be executed and rest will be ignored.

Syntax:

if boolean_expression1 {
//statements if the expression1 evaluates to true
} else if boolean_expression2 {
//statements if the expression2 evaluates to true
} else {
//statements if both expression1 and expression2 result to false
}

Example: To check if the number is negative, positive or zero

Rust
fn main() {
   let num = 0 ;
   if num > 0 {
      println!("num is positive");
   } else if num < 0 {
      println!("num is negative");
   } else {
      println!("num is neither positive nor negative") ;
   }
}

Output:

num is neither positive nor negative

Nested if-else statement

If we want to check multiple conditions we can use if statements within if statements we call it as nested statement. A nested if is an if statement that is the target of another if or else.

Syntax:

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

Example: This example check if the number is greater than other number also checks it is even or odd using nested if-else.

Rust
fn main() {
    let x = 10;
    let y = 5;

    if x > y {
        println!("x is greater than y");

        if x % 2 == 0 {
            println!("x is even");
        } else {
            println!("x is odd");
        }
    } else {
        println!("x is not greater than y");
    }
}

Output
x is greater than y
x is even


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads