Rust – Decision Making

  • Post author:
  • Post category:Rust
  • Post comments:1 Comment
Rust - Decision Making

Rust Decision-making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

Shown below is the general form of a typical decision-making structure found in most of the programming languages āˆ’

Rust - Decision Making

Statement & Description Of Rust Decision Making

r.NoStatement & Description
1if statement AnĀ ifĀ statement consists of a Boolean expression followed by one or more statements.
2if…else statement AnĀ ifĀ statement can be followed by an optionalĀ elseĀ statement, which executes when the Boolean expression is false.
3else…if and nested if statement you can use oneĀ ifĀ orĀ else ifĀ statement inside anotherĀ ifĀ orĀ else ifĀ statement(s).
4match statementĀ allows a variable to be tested against a list of values.

If Statement

The ifā€¦else construct evaluates a condition before a block of code is executed.

Syntax

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

If the Boolean expression evaluates to true, then the block of code inside the if statement will be executed. If the Boolean expression evaluates to false, then the first set of code after the end of the if statement (after the closing curly brace) will be executed.

fn main(){
   let num:i32 = 5;
   if num > 0 {
      println!("number is positive") ;
   }
}

The above example will printĀ the number as positiveĀ as the condition specified by the if block is true.

if else statement

An if can be followed by an optional else block. The else block will execute if the Boolean expression tested by the if statement evaluates to false.

Syntax

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

FlowChart

Rust - Decision Making

The if block guards the conditional expression. The block associated with the if statement is executed if the Boolean expression evaluates to true.

The if block may be followed by an optional else statement. The instruction block associated with the else block is executed if the expression evaluates to false.

Illustration – Simple ifā€¦else

fn main() {
   let num = 12;
   if num % 2==0 {
      println!("Even");
   } else {
      println!("Odd");
   }
}

The above example prints whether the value in a variable is even or odd. The if block checks the divisibility of the value by 2 to determine the same. Here is the output of the above code āˆ’

Even

Nested If

TheĀ elseā€¦ifĀ the ladder is useful to test multiple conditions. The syntax is as shown below āˆ’

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
}

When using ifā€¦elseā€¦if and else statements, there are a few points to keep in mind.

  • An if can have zero or one else’s and it must come after any else..if.
  • An if can have zero to many else..if and they must come before the else.
  • Once an else..if succeeds, none of the remaining else..if or else will be tested.

Example: elseā€¦if ladder

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

The snippet displays whether the value is positive, negative, or zero.

Output

2 is positive

Match Statement

The match statement checks if a current value is matching from a list of values, this is very much similar to the switch statement in C language. In the first place, notice that the expression following the match keyword does not have to be enclosed in parentheses.

The syntax is as shown below.

let expressionResult = match variable_expression {
   constant_expr1 => {
      //statements;
   },
   constant_expr2 => {
      //statements;
   },
   _ => {
      //default
   }
};

In the example given below,Ā state_codeĀ is matched with a list of valuesĀ MH, KL, KA, GAĀ āˆ’ if any match is found, a string value is returned to the variableĀ state. If no match is found, the default case _ matches and valueĀ UnkownĀ is returned.

fn main(){
   let state_code = "MH";
   let state = match state_code {
      "MH" => {println!("Found match for MH"); "Maharashtra"},
      "KL" => "Kerala",
      "KA" => "Karnadaka",
      "GA" => "Goa",
      _ => "Unknown"
   };
   println!("State name is {}",state);
}

Output

Found match for MH
State name is Maharashtra

Next Topic – Click Here

This Post Has One Comment

Leave a Reply