visit
But unlike in mathematics, variables could store not just numbers. They could be characters, series of characters (or called strings in programming), booleans (just only true or false), and any other thing that could be stored for further operations.
Variables could also store references to a specific thing or entity in programming. Such references mean that variables hold the entity's information crucial for accessing them. Values are also called literal or just simply data in some contexts.
Simple variable declarations in Python
myFullName = "John Rommel Octaviano"
myMiddleInitial = 'P';
myAge = 20;
isHuman = true;
Accessing or reading the values that a variable stored could be used for different operations such as multiplication and division if the said value is a number. The same goes for strings.
A source code for solving the roots of a quadratic equation that shows the importance of variables in storing values in such small use cases
double a;
double b;
double c;
double determinant;
double root1;
double root2;
a = 2.3;
b = 4;
c = 5.6;
determinant = b * b - 4 * a * c;
if (determinant > 0) {
root1 = (-b + Math.sqrt(determinant)) / (2 * a);
root2 = (-b - Math.sqrt(determinant)) / (2 * a);
} else if (determinant == 0) {
root1 = root2 = -b / (2 * a);
} else {
double real = -b / (2 * a);
double imaginary = Math.sqrt(-determinant) / (2 * a);
}
Identifiers are just another term for the name of something in our code. In programming, most concepts like variables, functions, objects, arrays, and others are needed to become known by putting names.
Simple statements using C# that declare variables with data types
string myFullName = "John Rommel Octaviano";
char myMiddleInitial = 'P';
int myAge = 20;
bool isHuman = true;
Simple statements using Python that declare variables without data types
myFullName = "John Rommel Octaviano"
myMiddleInitial = 'P'
myAge = 28
isHuman = true
Just think of data types like just a marking that states that a particular variable could only accept a value depending on the data type that is stated. Not following these markings by means of not storing a number in a String could cause errors. A protocol must be followed.
int - for integers of the whole number
float - for numbers that have a floating point value
double - for numbers that have floating point value in which the range or limit of this data type that a variable can accept is much higher than the float
long - for integers of the whole number in which the range or limit of this data type that a variable can accept is much higher than int
char - for a single character
String - for the sequence of characters
boolean - for values true or false
Simple statements using Java that declare variables with data types
String myFullName = "John Rommel Octaviano";
char myMiddleInitial = 'P';
int myAge = 20;
float myBatteryLevel = 90;
double myTemperature = 35.7;
long myCellCount = 30,000,000,000,000;
boolean isHuman = true;
and many more. These are the common and general types that most programming languages present particularly the strongly-ones. There are also data types for specific entities like arrays (basic storage for values), and many more.
In fact, we could also create our own types (this is called Generics in programming, an advanced concept).
Simple types are also called primitives which used primitive data types of a specific language. Basically, they directly stored the actual value. In which, a simple or primitive variable literally represents the actual literal itself.
Meanwhile, composite ones are basically just the complete opposite. Those are the variables that used created types by the programmer itself. In most languages, we can create our own types. They are also called reference types.
Reference variables commonly do not directly store the actual value itself but the reference or just basically a piece of information of the actual value's location in the memory.
Addition operator (+)
add number types. It could also be used to concatenate (or just basically just combine) non-string types to string types in most languages.
Subtraction operator (Subtract) (-)
subtract number types. It could also use to indicate negative numerical.
Multiplication operator (*)
multiply number types.
Division operator (/)
divide number types.
Modulo operator (%)
it computes the remainder of the number types.
Simple statements using JavaScript that perform different basic computations and store their values on different variables
let numOne = 23;
let numTwo = 24;
let sum;
let difference;
let product;
let quotient;
let remainder;
sum = numOne + numTwo; // 47
difference = numOne - numTwo; // -1
product = numOne * numTwo; // 552
quotient = numOne / numTwo; // 0.95833333333
remainder = numOne % numTwo; // 0
AND (&&)
returns true if both the conditions on the left and right are true
OR (||)
returns true if at least one condition on either left or right is true
NOT (!)
flips the boolean value of a condition or a value
Simple statements using Java that perform different basic operations by using logical operators between 2 boolean values and printing their results
boolean booleanOne = true;
boolean booleanTwo = false;
System.out.println(booleanOne && booleanTwo); // false
System.out.println(booleanOne || booleanTwo); // true
System.out.println(!booleanOne); // false
System.out.println(!booleanTwo); // true
Equals to (==)
returns true if both condition or value are equal
Not equals to (!=)
returns true if both condition or value are not equal
Greater than (>)
returns true if the value on the left is bigger than the right ** Less than (<)** returns true if the value on the left is smaller than the rightGreater than or equal to (>=)
returns true if the value on the left is bigger than or equal to the rightLess than or equal to (<=)
returns true if the value on the left is smaller than or equal to the rightSimple statements using Python that perform different basic comparisons of numbers by using relational operators between integer numbers and printing their results
numOne = 56
numTwo = 33
print(numOne > numTwo) # True
print(numOne != numTwo) # True
print(numOne > numTwo) # True
print(numOne < numTwo) # False
print(numOne >= numTwo) # True
print(numOne <= numTwo) # False
Assignment (=)
assigns the value of the right variable to the left variableAdd and assign (+=)
increase by adding the value of the left variable from the right variableSubtract and assign (-=)
decrease by adding the value of the left variable from the right variableMultiply and assign (*=)
increase by multiplying the value of the left variable by the right variableDivide and assign (/=)
decrease by dividing the value of the left variable from the right variableSimple statements using C++ that perform different basic comparisons of a number by using assignment operators and printing its final result
int numOne = 28;
numOne += 5; // 33
numOne -= 4; // 29
numOne *= 7; // 203
numOne /= 2; // 101
cout << numOne; // 101
Post-increment (x++)
increase the value of the variable by 1 after using the valuePre-increment (++x)
increase the value of the variable by 1 before using the valuePost-decrement (x--)
decrease the value of the variable by 1 after using the valuePre-increment (--x)
decrease the value of the variable by 1 before using the valueSimple statements using Java that perform different basic modifications of a number by using increment/decrement operators and printing its final result
int numOne = 28;
numOne++; // 28
++numOne; // 30
numOne--; // 30
--numOne; // 28
System.out.println(numOne); // 28
And this is where control structures in programming came to work. They are crucial and not just optional tools to consider. Just like operators, there are structures that are not present in one language and that is part of learning some adjustments when jumping from one language to another.
Serves as a gate on what code will be run. Since it's like a gate, the only way to open them is to have a true key, literally. Or a condition evaluates to TRUE.
Single Alternative (if)
Statements will be done or executed when a condition is met, evaluated, or equal to TRUE. Otherwise, meaning if the condition will become equal to FALSE, then nothing happens.
An if statement checks if the value of a variable is equal to something through a conditional in JavaScript, in which if the checking is true, it will print something, otherwise, nothing happens
let foo = "foo";
if(foo === "foo") {
console.log("foo!"); // Prints this if true
}
Double Alternatives (if-else)
Statements will be done or executed when a condition is met, evaluated, or equal to TRUE. Otherwise, meaning if the condition will become equal to FALSE, then the alternative statements will be executed.
An if statement checks if the value of a variable is equal to something through a conditional in JavaScript, in which if the checking is true, it will print something, otherwise, it prints something else
let foo = "foo";
if(foo === "foo") {
console.log("foo!"); // Prints this if true
} else {
console.log("bar!"); // Prints this if false instead
}
Multiple Alternatives (if-elseif-else)
Statements will be done or executed when a condition is met, evaluated, or equal to TRUE. But, if the condition returns FALSE, succeeding conditions will be checked until a condition is evaluated to TRUE, and statements inside of that will be executed. Finally, if all those succeeding conditions are all evaluated to FALSE, meaning none of them happens, then the statements in the final block will be executed.
An if statement checks if the value of a variable is equal to something through a conditional in JavaScript, in which if the checking on the first condition is true, it will print something, otherwise, it will check the next conditions and print something else if true but if all conditions are false, print the last one
if(foo === "bar") {
console.log("bar!"); // Prints this if true
} else if(foo === "foobar") {
console.log("foobar!"); // Prints this if false instead
} else {
console.log("foo!"); // Prints this if everything else is false
}
Serves as a wheel that continues to spin or run our code repeatedly. Since it's like a wheel, the only way to stop them is to have a false handbrake, literally. Or until a condition returns FALSE.
Repeat-While (while)
Before statements will be run, a condition is first evaluated and if the result is equal to TRUE, code will be run. After the execution, it will back at checking the condition again and it will run again and again until the condition becomes FALSE.
A basic while loop in Java that prints all numbers within the starting and ending range
int counter = 1;
while (counter <= 10) {
System.out.println(counter);
counter++;
}
Do-Repeat-While (do-while)
Statements will be run first before checking the condition. After checking and the result was TRUE, it will run again the code and back at checking again. It will run again and again until the condition becomes FALSE. Unlike while loops, do-whiles are expected to run at least once.A basic do-while loop in Java that prints all numbers within the starting and ending range
do {
System.out.println(counter);
counter++;
} while(counter <= 10);
Repeat-For (for)
This structure also works under the hood like while loop but they have their own major difference. In for loop, iteration or number of times the code will run is known before the execution while the iteration is not known in a while loop.
A basic for loop in Java that prints all numbers within the starting and ending range
for(int counter = 1; counter <= 10; counter++) {
System.out.println(counter);
}