Recap - If Else
Part of the Fundamentals section of Coddy's Rust journey — lesson 27 of 75.
Challenge
BeginnerYou are given a code which gets as input two numbers n1 and n2 and a single char string op.
Note: we will learn in next lessons how to get input from the user, currently just don't touch the three first lines.
The possible values for op are "+", "-", "/" and "*"
Your task is to set the variable result based on the conditions:
- if
opis"+", setresultwithn1 + n2. - if
opis"-", setresultwithn1 - n2. - if
opis"/", setresultwithn1 / n2. - if
opis"*", setresultwithn1 * n2.
Try it yourself
use std::io;
fn main() {
let mut n1_input = String::new();
let mut n2_input = String::new();
let mut op_input = String::new();
io::stdin().read_line(&mut n1_input).unwrap();
io::stdin().read_line(&mut n2_input).unwrap();
io::stdin().read_line(&mut op_input).unwrap();
let n1: f64 = n1_input.trim().parse().unwrap();
let n2: f64 = n2_input.trim().parse().unwrap();
let op = op_input.trim();
// Write your code below, use n1, n2 and op
let result: f64 = 0.0;
println!("{}", result);
}All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 33Variables Part 2
Type DeclarationNaming ConventionsType InferenceRecap - Initialize VariablesType Casting9Loops
For Over SeriesWhile LoopBreakContinueNested LoopLoop LabelsInfinite LoopRecap - Dynamic Input