Rust - Decision Making Flashcards
Give the possiblites of decision making
1 if statement
An if statement consists of a Boolean expression followed by one or more statements.
2 if…else statement
An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.
3 else…if and nested ifstatement
You can use one if or else if statement inside another if or else if statement(s).
4 match statement
A match statement allows a variable to be tested against a list of values.
Give a code example of if statment
let num = 5;
if num> 6 {
println!(“{} is greater than 6”, num);
}
Give an example of if..else and nested
let num = 12;
if num % 2==0 {
println!(“Even”);
} else {
println!(“Odd”);
}
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) ;
}
Describe 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.
Give an example of match statement
let state_code = “MH”;
let state = match state_code{
“MH” => {
}
}