Recap - If Else
Part of the Fundamentals section of Coddy's C++ journey — lesson 31 of 74.
Challenge
BeginnerYou are given a code which gets as input two numbers n1 and n2 of type double 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 of type double based on the conditions:
- if
opis'+', setresultwithn1 + n2. - if
opis'-', setresultwithn1 - n2. - if
opis'/', setresultwithn1 / n2. - if
opis'*', setresultwithn1 * n2.
Try it yourself
#include <iostream>
int main() {
double n1, n2;
char op;
std::cin >> n1 >> n2 >> op;
// Write your code below
double result = 0;
std::cout << result << std::endl;
return 0;
}All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison3Variables Part 2
Type DeclarationNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementConditional OperatorRecap - If ElseNested If - Else