Programming Tutorials | Basics
When programming, a computer will not necessarily "remember" whatever it is that you are telling it. For example, you could run a line of code to work out the value of 2 + 3. This on its own would not actually do anything beneficial for your program. In order to make our program actually remember data, we have to use something called a "variable". A variable is defined with a specific name, and can be assigned or reassigned to a piece of data, which you can then reference throughout the program. Variables are one of the simplest but most useful parts of any program, and without using them, it would make many programs less efficient, impossible to use or just have incredibly messy code.
In order to define a variable, most programming languages use the same convention of the name followed by '=' and then whatever you wish to assign the variable to. For example, in a language such as Python:
variable = 5 + 3
This will define a variable under the name "variable" as the result of 5 + 3, so you would have "variable" as 8. In pseudocode (a general way of writing code to show the idea of what you want to do), we use "<-" to show the assignment of a variable instead of "=". Other programming languages also require variables to be defined differently, however they mostly use the same theory behind it, the same implementation of variables and the same basic idea / structure to it.
An integral thing to know in any programming language is the different data types that you can use. A data type is pretty much just the type of data that a certain value is. The majority of the main data types are the same across most languages, and so we will go over the basic and most common data types here, and then more complex ones (or the ones which vary more for different languages) in the tutorials where they are used.
Some of the main data types include:
- Integer - Commonly referred to as an int. Any whole number is an integer.
- String - A collection of characters. Strings are usually defined by using quotation marks ("string", `string` or 'string')
- Float - Floating point value. This is essentially just a decimal number.
- Char - A single character from a string.
Whilst it may seem like it is just very basic, knowing about different data types and when to use them is very important when programming, especially in languages such as Java, where you are required to define a variable as a specific data type as well as giving it a name and value. As previously mentioned, there are many more data types than this, however these are the most common ones to be aware of. We will not go into much detail about data types here, as more in-depth explanation would require going into specific languages, and so they will be done in the relevant tutorials as required. For now, the basics of knowing the main basic data types and what they are should be enough to get you started with programming.
When it comes to programming, there are various operations that we may wish to run on values. This includes multiplication, division, addition, subtraction or checking whether a value is equal to another value. For some of these operations, they may be different to what would be expected or how they are usually written in mathematics.
The "+" operator is used for addition, and will add the given values together, as would be expected, similarly to "-". The "*" operator is used to multiply the given values together. The "/" operator is used to divide values. The "//" operator is used for floor division, which will divide the values and return a whole number, rounding down. The "%" operator is known as "modulus" and will return the remainder when the first number is divided by the second. The "**" operator is used for exponents (powers), raising the first number to the power of the second.
These are the operators we can use for arithmetic; however, we can also use other operators for comparisons, which will return a boolean value:
The "==" operator will check if the first value is equal to the second. The "<=" operator will check if the first value is less than or equal to the second. The ">=" operator will check if the first value is greater than or equal to the second. The "<" operator will check if the first value is less than the second. The ">" operator will check if the first value is greater than the second.
We can also use logical operators, which tend to be used differently depending on the language:
"AND" (sometimes used as "&&" or "and") will return true if statements on both sides are true "OR" (sometimes used as "||" or "or") will return true if one of the statements is true "NOT" (sometimes used as "!" or "not") will return true if the statement is false (and vice versa).
There are various other types of operators than we can use in various languages, however these are the main basic ones to begin with, and further operators will be covered as required in further tutorials.
Functions, also known as subroutines, are an incredibly useful way to make our program neater, and to use features written elsewhere without rewriting them. A function is a piece of code which can be run through calling it by the name it was defined as followed by brackets "()" (in most languages). Within these brackets, we can also pass parameters into the function. Parameters are essentially pieces of information that we can put into the function to be used in whatever code it is running, however they are not always needed unless defined in the function. A pseudocode function definition could look like the following:
FUNCTION exampleFunction() returnValue <- 7 RETURN returnValue END FUNCTION
As you can see, this function will be defined as "exampleFunction", and it takes no parameters, as the brackets are empty. This function will assign the variable "returnValue" to 7 and then return that value. The "return value" of a function is what the function will give you if you try to use it like a variable. For example, if you had a function which will multiply two numbers, you could do the following:
FUNCTION multiply(num1, num2) RETURN num1 * num2 END FUNCTION OUTPUT(multiply(2, 3))
The result of this pseudocode would be output of the number 6. We give the function the numbers 2 and 3, and it will return the result of 2 * 3, the calculation done in the function. By calling the function within the OUTPUT, we are telling it that we want the return value of the function to be output, which is 6 in this case. As you can see, this function takes some parameters, "num1" and "num2". These must be defined within the function itself, and can be referenced like variables in the function, and must also be passed into the function when it is called, which can be done by putting them into the brackets after the name of the function. The parameters of a function, both when defining and calling the function, must be separated by commas, as you can see done in the function above.
Functions can be defined very differently depending on the language, however, similarly to variables, it is the same concept and the can be used in the same way.
IF statements can be used in order to allow us to handle different inputs or possible outcomes for our program, or even to prevent various errors from occurring. In any language, IF statements and other checks are incredibly useful for any program. Any professionally created program will most likely contain a multitude of IF statements to handle various aspects of the program.
IF statements will take a boolean value to test whether or not they should run. If this boolean value is true, then the code within the IF statement will run, otherwise, it will be skipped. Different languages define if statements differently, however in pseudocode, we can use:
value <- 7 IF value == 7 THEN OUTPUT "The value was equal to 7!" END IF OUTPUT "Continuing the program..."
This will always output "The value was equal to 7!" followed by "Continuing the program..." as we have pre-defined it to always return true, since value is equal to 7 (we put == in order to check if one value is equal to another).
We can add onto these IF statements by using ELSE statements, which will allow us to handle alternative possibilities to that of the main IF statement. For example, if we wanted to check for multiple values in the code above and change the way the program runs based on them:
value <- 9 IF value == 7 THEN OUTPUT("Value is equal to 7!") ELSE IF value == 9 THEN OUTPUT("Value is equal to 9!") ELSE OUTPUT("Value is not equal to 7 or 9!") ENDIF
With this code, we will receive the output "Value is equal to 9!" and we can see that it is allowing us to check for any possible value of the value variable, doing specific checks for 7 and 9, and then something else if it is not equal to either. We can use this in many programs, such as for handling user input and to ensure it is equal to certain values, or to check whether or not the user input is equal to something, such as the answer to a question, in order to run specific code.