# The Basics

## Constants and Variables

Constants and variables associate a name (such as `maximumNumberOfLoginAttempts` or `welcomeMessage`) with a value of a particular type (such as the number `10` or the string `"Hello"`). The value of a *constant* can’t be changed once it’s set, whereas a *variable* can be set to a different value in the future.

### Declaring Constants and Variables

Constants and variables must be declared before they’re used. You declare constants with the `Const` keyword and variables with the `Var` keyword. Here’s an example of how constants and variables can be used to track the number of login attempts a user has made:

```xojo
Const maximumNumberOfLoginAttempts = 10
Var currentLoginAttempt As Integer = 0
```

This code can be read as:

"Declare a new constant called `maximumNumberOfLoginAttempts`, and give it a value of `10`. Then, declare a new variable called `currentLoginAttempt`, and give it an initial value of `0`."

In this example, the maximum number of allowed login attempts is declared as a constant, because the maximum value never changes. The current login attempt counter is declared as a variable, because this value must be incremented after each failed login attempt.

You can declare multiple variables on a single line, separated by commas:

```xojo
Var x As Double = 0.0, y As Double = 0.0, z As Double = 0.0
```

or shorter:

```xojo
Var x, y, z As Double = 0.0
```

{% hint style="info" %}
If a stored value in your code won’t change, always declare it as a constant with the `Const` keyword. Use variables only for storing values that need to be able to change.
{% endhint %}

### Type Annotations

You can provide a *type annotation* when you declare a constant, to be clear about the kind of values the constant can store. Write a type annotation by placing a colon after the constant name, followed by a space, followed by the name of the type to use.

This example provides a type annotation for a variable called `welcomeMessage`, to indicate that the variable can store `String` values:

```xojo
Const welcomeMessage As String
```

The `As` in the declaration means "…of type…," so the code above can be read as:

"Declare a constant called `welcomeMessage` that’s of type `String`."

The phrase "of type `String`" means "can store any `String` value." Think of it as meaning "the type of thing" (or "the kind of thing") that can be stored.

The `welcomeMessage` constant can now be set to any string value without error:

```xojo
Const welcomeMessage As String = "Hello"
```

You can define multiple related variables of the same type on a single line, separated by commas, with a single type annotation after the final variable name:

```xojo
Var red, green, blue As Double
```

### Naming Constants and Variables

Constant and variable names can contain almost any character, including Unicode characters:

```xojo
Const π = 3.14159
Const 你好 = "你好世界"
Const 🐶🐮 = "dogcow"
```

Constant and variable names can’t contain whitespace characters. Nor can they begin with a number, although numbers may be included elsewhere within the name.

Once you’ve declared a constant or variable of a certain type, you can’t declare it again with the same name, or change it to store values of a different type. Nor can you change a constant into a variable or a variable into a constant.

You can change the value of an existing variable to another value of a compatible type. In this example, the value of `friendlyWelcome` is changed from `"Hello!"` to `"Bonjour!"`:

```xojo
Var friendlyWelcome As String = "Hello!"
friendlyWelcome = "Bonjour!"
// friendlyWelcome is now "Bonjour!"
```

Unlike a variable, the value of a constant can’t be changed after it’s set. Attempting to do so is reported as an error when your code is compiled:

```xojo
Const languageName = "Xojo"
languageName = "Xojo++"
// This is a compile-time error: This is a constant; its value can't be changed
```

### Comments

Use comments to include nonexecutable text in your code, as a note or reminder to yourself. Comments are ignored by the Xojo compiler when your code is compiled.

Comments in Xojo are very similar to comments in C. Single-line comments begin with two forward-slashes (`//`), `'` or `REM`:

```xojo
// This is a comment.
' This is a comment.
REM This is a comment.
```

## Integers

*Integers* are whole numbers with no fractional component, such as `42` and `-23`. Integers are either *signed* (positive, zero, or negative) or *unsigned* (positive or zero).

Xojo provides signed and unsigned integers in 8, 16, 32, and 64 bit forms. These integers follow a naming convention similar to C, in that an 8-bit unsigned integer is of type `UInt8`, and a 32-bit signed integer is of type `Int32`.

### Integer Bounds

#### Integer

In most cases, you don’t need to pick a specific size of integer to use in your code. Xojo provides an additional integer type, `Integer`, which has the same size as the current platform’s native word size:

* On a 32-bit platform, `Integer` is the same size as `Int32`.
* On a 64-bit platform, `Integer` is the same size as `Int64`.

Unless you need to work with a specific size of integer, always use `Integer` for integer values in your code. This aids code consistency and interoperability. Even on 32-bit platforms, `Integer` can store any value between `-2,147,483,648` and `2,147,483,647`, and is large enough for many integer ranges.

#### UInteger

Xojo also provides an unsigned integer type, `UInteger`, which has the same size as the current platform’s native word size:

* On a 32-bit platform, `UInteger` is the same size as `UInt32`.
* On a 64-bit platform, `UInteger` is the same size as `UInt64`.

{% hint style="info" %}
Use `UInteger` only when you specifically need an unsigned integer type with the same size as the platform’s native word size. If this isn’t the case, `Integer` is preferred, even when the values to be stored are known to be nonnegative. A consistent use of `Integer` for integer values aids code interoperability, avoids the need to convert between different number types, and matches integer type inference, as described in [Type Safety and Type Inference](#type-safety-and-type-inference).
{% endhint %}

## Floating-Point Numbers

*Floating-point numbers* are numbers with a fractional component, such as `3.14159`, `0.1`, and `-273.15`.

Floating-point types can represent a much wider range of values than integer types, and can store numbers that are much larger or smaller than can be stored in an `Integer`. Xojo provides two signed floating-point number types:

* `Double`&#x20;
* `Single`

{% hint style="info" %}
The appropriate floating-point type to use depends on the nature and range of values you need to work with in your code. In situations where either type would be appropriate, `Double` is preferred.
{% endhint %}

## Type Safety and Type Inference

Xojo is a *type-safe* language. A type safe language encourages you to be clear about the types of values your code can work with. If part of your code requires a `String`, you can’t pass it an `Integer` by mistake.

Because Xojo is type safe, it performs *type checks* when compiling your code and flags any mismatched types as errors. This enables you to catch and fix errors as early as possible in the development process.

Type-checking helps you avoid errors when you’re working with different types of values. However, this doesn’t mean that you have to specify the type of every constant that you declare. If you don’t specify the type of value you need, Xojo uses *type inference* to work out the appropriate type. Type inference enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide.

Type inference is particularly useful when you declare a constant with an initial value. This is often done by assigning a *literal value* (or *literal*) to the constant at the point that you declare it. (A literal value is a value that appears directly in your source code, such as `42` and `3.14159` in the examples below.)

For example, if you assign a literal value of `42` to a new constant without saying what type it is, Xojo infers that you want the constant to be an `Integer`, because you have initialized it with a number that looks like an integer:

```xojo
Const meaningOfLife = 42
// meaningOfLife is inferred to be of type Integer
```

Likewise, if you don’t specify a type for a floating-point literal, Xojo infers that you want to create a `Double`:

```xojo
Const pi = 3.14159
// pi is inferred to be of type Double
```

Xojo always chooses `Double` (rather than `Single`) when inferring the type of floating-point numbers.

If you combine integer and floating-point literals in an expression, a type of `Double` will be inferred from the context:

```xojo
Const anotherPi = 3 + 0.14159
// anotherPi is also inferred to be of type Double
```

The literal value of `3` has no explicit type in and of itself, and so an appropriate output type of `Double` is inferred from the presence of a floating-point literal as part of the addition.

## Numeric Literals

Integer literals can be written as:

* A *decimal* number, with no prefix
* A *binary* number, with a `&b` prefix
* An *octal* number, with a `&o` prefix
* A *hexadecimal* number, with a `&h` prefix

All of these integer literals have a decimal value of `17`:

```xojo
Const decimalInteger = 17
Const binaryInteger = &b10001       // 17 in binary notation
Const octalInteger = &o21           // 17 in octal notation
Const hexadecimalInteger = &h11     // 17 in hexadecimal notation
```

## Numeric Type Conversion

Use the `Integer` type for all general-purpose integer constants and variables in your code, even if they’re known to be nonnegative. Using the default integer type in everyday situations means that integer constants and variables are immediately interoperable in your code and will match the inferred type for integer literal values.

Use other integer types only when they’re specifically needed for the task at hand, because of explicitly sized data from an external source, or for performance, memory usage, or other necessary optimization. Using explicitly sized types in these situations helps to catch any accidental value overflows and implicitly documents the nature of the data being used.

### Integer Conversion

The range of numbers that can be stored in an integer constant or variable is different for each numeric type. An `Int8` constant or variable can store numbers between `-128` and `127`, whereas a `UInt8` constant or variable can store numbers between `0` and `255`. A number that won’t fit into a constant or variable of a sized integer type is reported as an error when your code is compiled:

```xojo
Var cannotBeNegative As UInt8 = -1
// UInt8 can't store negative numbers, and so this will appar as 255
Var tooBig As Int8 = 127 + 1
// Int8 can't store a number larger than its maximum value,
// and so this will appear as -128
```

## Booleans

Xojo has a basic *Boolean* type, called `Boolean`. Boolean values are referred to as *logical*, because they can only ever be true or false. Xojo provides two Boolean constant values, T`rue` and F`alse`:

```xojo
Const orangesAreOrange = True
Const turnipsAreDelicious = False
```

The types of `orangesAreOrange` and `turnipsAreDelicious` have been inferred as `Boolean`from the fact that they were initialized with Boolean literal values. As with `Integer` and `Double`above, you don’t need to declare constants or variables as `Boolean` if you set them to `True` or `False` as soon as you create them. Type inference helps make Xojo code more concise and readable when it initializes constants or variables with other values whose type is already known.

Boolean values are particularly useful when you work with conditional statements such as the `If` statement:

```xojo
If turnipsAreDelicious Then
  System.DebugLog("Mmm, tasty turnips!")
Else
  System.DebugLog("Eww, turnips are horrible.")
End If
// Prints "Eww, turnips are horrible."
```

Conditional statements such as the `if` statement are covered in more detail in [Control Flow](https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html).

Xojo’s type safety prevents non-Boolean values from being substituted for `Boolean`. The following example reports a compile-time error:

```xojo
Const i = 1
If i Then
  // this example will not compile, and will report an error
End If
```

However, the alternative example below is valid:

```xojo
Const i = 1
If i = 1 Then
  // this example will compile successfully
End If
```

The result of the `i = 1` comparison is of type `Boolean`, and so this second example passes the type-check. Comparisons like `i = 1` are discussed in Basic Operators.

As with other examples of type safety in Xojo, this approach avoids accidental errors and ensures that the intention of a particular section of code is always clear.
