• 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.

      • 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.

      • 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.

      • class Person {
        	constructor(name, age) {
        		this.name = name;
        		this.age = age;
        	}
        
        	greet() {
        		console.log('Hello my name is ' + this.name);
        	}
        }
        
        var human = new Person('John', 36);
        
        console.log(human.name); // John
        console.log(human.age); // 36
        human.greet(); // Hello my name is John
      • class Person {
          String name;
          int age;
        
          Person(this.name, this.age);
        
          void greet() {
        	print('Hello my name is ' + this.name);
          }
        }
        
        void main() {
          var human = new Person('John', 36);
        
          print(human.name); // John
          print(human.age); // 36
          human.greet(); // Hello my name is John
        }
      • 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
        }
      • <?php
        class Person {
        	public $name, $age;
        
        	function __construct($name, $age) {
        		$this->name = $name;
        		$this->age = $age;
        	}
        
        	public function greet() {
        		print('Hello my name is ' . $this->name);
        	}
        }
        
        $human = new Person('John', 36);
        
        print($human->name); // John
        print($human->age); // 36
        $human->greet(); // Hello my name is John
      • class Person:
        	def __init__(self, name, age):
        		self.name = name
        		self.age = age
        
        	def greet(self):
        		print('Hello my name is ' + self.name)
        
        
        human = Person('John', 36)
        
        print(human.name)  # John
        print(human.age)  # 36
        human.greet()  # Hello my name is John
        
      • package main
        
        import "fmt"
        
        type Person struct {
        	name string
        	age  int
        }
        
        func (p Person) greet() {
        	fmt.Println("Hello my name is " + p.name)
        }
        
        func main() {
        	var human = Person{"John", 36}
        
        	fmt.Println(human.name) // John
        	fmt.Println(human.age)  // 36
        	human.greet()		   // Hello my name is John
        }