• Variables

    In programming, variables are used to store data values. These values can be of different data types, such as strings, integers, floating-point numbers, boolean, arrays, or objects. Variables are assigned names so that they can be referenced in the code. They can be any size and shape, and can hold anything from numbers to strings of text. You can use variables to keep track of things like user input, or the result of a calculation. The name of a variable can be anything you want, as long as it doesn’t start with a number and isn’t a reserved word in the language. Once a variable is given a value, that value can be changed throughout the program by reassigning the variable. Variables are one of the most important tools in programming, and understanding how to use them is essential to becoming a good programmer.

      • String Variables

      • A string variable is simply a variable that can hold multiple pieces of text (a word, phrase, or sentence). However, sometimes we need to store more than just text. We might want to store a person's name, which can be multiple words. In this case, we need to use a string variable.

      • Integer Variables

      • An integer variable is a data type in computer programming that stores numerical values. Integer variables can be used to represent whole numbers, such as 1, 2, 3, 4, 5, etc. They can also be used to represent negative numbers, such as -1, -2, -3, -4, -5, etc. Integer variables are commonly used in programs to store counts or loop indices.

      • Float Variables

      • Float variables, like integer variables, hold numerical values but are presented in decimal form. It is possible to represent decimal numbers such as 1.01, 2.20, 3.33, 4.44, 5.555, etc. in float variables. They can also be used to represent negative decimal numbers, such as -1.01, -2.20, -3.33, -4.44, -5.555, etc.

      • Boolean Variables

      • A boolean variable is a type of data that can only have one of two possible values, either true or false. In programming, boolean variables are often used to track whether a certain condition has been met, or to control the flow of a program. For example, a program might use a Boolean variable to keep track of whether a user is logged in or not.

      • import std.stdio : writeln;
        
        void main()
        {
        	bool isTruthy = true;
        	bool isFalsy = false;
        
        	writeln(isTruthy); // true
        	writeln(isFalsy); // false
        }
        
      • Array Variables

      • Array variables are used in many programming languages to store data sets of various sizes. An array is a type of data structure that stores a collection of elements. Each element in the array has a specific index, which can be used to access it.

      • import std.stdio : write;
        import std.string;
        import std.array;
        
        void main()
        {
        	string[] fruits = ["apple", "banana", "cherry"];
        
        	write(fruits[1]); // banana
        }
        
      • Object Variables

      • An object variable is a type of variable that is used to reference an object. The value of an object variable is an address that identifies the location of the object in memory. When an object is assigned to an object variable, a new reference to the object is created. An object is a type of data structure that holds a collection of properties and methods. For each property and method, a name is used to access them.

      • import std.stdio : write, writeln;
        import std.string;
        
        class Person {
        	string name;
        	int age;
        
        	this(string name, int age) {
        		this.name = name;
        		this.age = age;
        	}
        
        	void greet() {
        		write("Hello my name is ", this.name);
        	}
        }
        
        void main()
        {
        	Person human = new Person("John", 36);
        
        	writeln(human.name); // John
        	writeln(human.age); // 36
        	human.greet(); // Hello my name is John
        }