Menu
Coddy logo textTech

Creating an Operation

Part of the Fundamentals section of Coddy's Dart journey. Lesson 89 of 94.

challenge icon

Challenge

Easy

Our calculator needs to know which operation to perform. To represent this, we'll use a string variable that can hold one of the four basic arithmetic operators: +, -, *, or /.

Update your calculator program to:

  1. Declare a string variable named operation
  2. Initialize it with the multiplication operator: "*"
  3. Add a conditional statement that checks if the operation is valid (equal to '+', '-', '*', or '/')
  4. If the operation is not valid, set it to '+' and print exactly: "Invalid operation. Using addition (+) as default."
  5. Print "Operation: " followed by the operation value

Try it yourself

void main() {
  print("Welcome to the Simple Calculator!");
  
  // Declare and initialize number variables
  double firstNumber = 10.5;
  double secondNumber = 5.5;
  
  // Print the numbers
  print("First number: $firstNumber");
  print("Second number: $secondNumber");
  
  // Declare and validate the operation variable
}

All lessons in Fundamentals

Practice on your own: Online Dart compiler