Variables and Arithmetic in Dart programming language - Road to Mobile Development

Glory OlaifaGlory Olaifa
4 min read

Dart is an object-oriented programming language created by Google. It's designed to be simple, fast, and flexible, with a focus on building applications for mobile, web, and desktop platforms.

Dart is a simple and flexible programming language that's easy to learn and use. Dart can help you create high-quality software quickly and efficiently. In this article, I'll walk you through the basics of dart and this is the basics of all other flutter courses.

Variables

In programming, a variable is a container that holds a value. The value can be a number, a string (text), or any other data type. To create a variable in Dart, you first need to specify the data type, such as int (integer), double (a floating-point number), or String (text). You then give the variable a name, and optionally, an initial value.

Here's an example:

int age = 22;
String name = "Glory";
double height = 2.2;

In this example, we've created age, name, and height variables. Age is an integer variable with an initial value of 25. The name is a string variable with an initial value of "John". Height is a double variable with an initial value of 1.75.

Don't bother yourself too much about what int, String, and Double mean. I'll be talking about data types in my next article.

You can also declare variables without assigning an initial value:

int age;
String name;
double height;

age = 25;
name = "Glory";
height = 2.2;

In this case, the variables are declared but do not have a value assigned to them yet.

Arithmetic in Dart

Dart supports the usual arithmetic operations, including addition (+), subtraction (-), multiplication (*), division (/), and the remainder (%). The % sign might look strange to you, but it is used to find the remainder of two values just like the remainder of 2 divided by 2 is 0. Then we can say 2%2 = 0.

Here are some examples:

Note: the value after the double slash (//) is the expected result.

int x = 10;
int y = 3;

int sum = x + y; // 13

int difference = x - y; // 7

int product = x * y; // 30

double quotient = x / y; // 3.3333333333333335

int remainder = x % y; // 1

In this example, we've created two integer variables, x and y. We then performed some arithmetic operations using these variables and assigned the results to new variables.

Note that the division operation returns a floating-point number (number with a decimal point), even if the operands are integers. If you want to perform integer division, you can use the ~/ operator:

int x = 10;
int y = 3;

int result = x ~/ y; // 3

In this example, the result is an integer (value without decimal point) variable that holds the result of the integer division operation.

You can also use parentheses to group operations and specify the order in which they should be performed:

int x = 10;
int y = 3;
int z = 2;

int result = (x + y) * z; // 26

In this example, we've used parentheses to group the addition and multiplication operations, so that the addition is performed before the multiplication.

Practice

You can use the dartpad to practice all we've discussed play around with it, and make sure you master variable declarations and use those variables to perform arithmetic operations.

Above is an example of the dartpad and how to use it. You'll notice something strange on lines 18-27 in the above screenshot. print() is used to display values or variables. It's just like you have a typed copy of a letter and you want to print it out, then you definitely need to use the printer. The print() is used to print out in our case here, all you need to do is to put in the value/variable you want to print.

Conclusion

Variables and arithmetic are essential concepts in programming, and Dart provides a simple and powerful way to work with them. By understanding these concepts, you'll be able to write programs that perform complex operations and manipulate data in useful ways. In my next article, we'll be discussing data types and you'll get to understand better what Int, String, double, and other data types mean.

16
Subscribe to my newsletter

Read articles from Glory Olaifa directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Glory Olaifa
Glory Olaifa

I’m a Mobile Engineer with 3 years of experience. I love to explore new technologies and try to choose the best for whatever I want to build. I enjoy building communities and contributing to the tech ecosystem to help people grow. I believe introducing fresh perspectives and techniques allows businesses to evolve and grow. My goal is to remain on the cutting edge of advancements.