MQL4|Integer Type (int)

Purpose

The integer type (int) is used to represent integer values. Integers are positive or negative numbers that do not include decimal points. In a program, they are used for various purposes such as loop counters, indexes, flags, and calculations.

Size

The size of an integer type is 4 bytes, which means it occupies 32 bits of memory space.

Value Range

The value range for integer types is from -2147483648 to 2147483647. This wide range allows efficient handling of many calculations and operations.

Example of Usage

Below is an example of using the integer type. In this example, an integer type is used as a loop counter to output messages based on specific conditions.

void OnTick() {
int count = 0;
double sum = 0.0;
int periods = 10;

// Sum the prices of the last 10 bars
for (int i = 0; i < periods; i++) {
sum += Close[i];
count++;
}

// Calculate the average price
double average = sum / count;

Print("The average price of the last " + count + " bars is " + average + ".");
}

In this code, the integer type is used as follows:

  1. Declare and initialize int count and int periods.
  2. Use a for loop to sum the prices of the last 10 bars.
  3. Increment count with each iteration of the loop.
  4. Calculate the average price by dividing the sum by the number of bars.
  5. Output the result with the Print function.

In this way, the integer type is used in various parts of the program to perform calculations and loop processing efficiently. Understanding and appropriately using the integer type can improve the performance and readability of the program.