Lesson 5
Dart Essentials: Arithmetic and Logical Operations
Lesson Overview

Are you ready to start, coder? Today, we're exploring the fundamentals of Dart, focusing on arithmetic and logical operations on basic types. These concepts are essential for data processing and decision-making in the development of your applications.

Arithmetic Operations Uncovered

From our previous discussions, Dart's basic types are int for integer numbers, double for floating point numbers, bool for true/false values, and String for text. Both int and double have constraints on their numerical range. We will explore these constraints further when we describe overflow later in this session.

Arithmetic operations, such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (remainder of a division, symbolized by %), can be performed on numerical types. For instance:

Dart
1int a = 10; 2int b = 2; 3int c = 3; 4 5print(a + b); // Outputs: 12 6print(a - b); // Outputs: 8 7print(a * b); // Outputs: 20 8print(a / b); // Outputs: 5.0 9print(a % b); // Outputs: 0 10print(a % c); // Outputs: 1 11print(b % c); // Outputs: 2 12print(c % a); // Outputs: 3

Dart supports the alteration of order using parentheses and provides the modulus (%) operation, which is useful for determining if numbers are even or odd.

Logical Operations Explained

Logical operators — && (AND), || (OR), ! (NOT) — function as decision-makers in Dart and evaluate to bool values — true or false. Here's how you can use them with two bool values:

Dart
1print(true && true); // true 2print(true && false); // false 3print(false && true); // false 4print(false && false); // false 5 6print(true || true); // true 7print(true || false); // true 8print(false || true); // true 9print(false || false); // false 10 11print(!true); // false 12print(!false); // true

In this context, && produces true if both boolean values are true. || outputs true if either value is true, and ! flips the boolean value.

The primary application of logical operations is with variables. Here's a simple illustration of how you can use them for decision-making:

Dart
1int speed = 60; 2int minSpeed = 30; 3int maxSpeed = 70; 4// Verify if the speed is within the expected range. 5print(speed > minSpeed && speed < maxSpeed); // Prints: true
Lesson Summary

Congratulations! Today, we've delved into arithmetic operations and made complicated decisions with logical operators. Practice exercises are up next to reinforce these essential topics on our journey into Dart. Get ready, fellow programmers; it's time to code!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.