The key to learn all Programming Languages
This tutorial is going to be different than most programming tutorials. We are not going to learn any specific programming language when we finish; but we will learn the base of all programming. It’s going to be a lot easier to learn any programming language after we finish with this tutorial, because instead of learning a programming language we are going to learn the base of programming.
Between the years 1969 and 1973 the “C” programming language was created. A few years later about 1979 “C++” language was created. Some people should be guessing why I am recalling that? That’s because those languages contain the base for all or most current modern programming languages. Nowadays we can program with languages such as “java,” “C#,” “Python,” “PHP,” “Ruby,” and many other languages. All of these languages have something in common and we are going to learn it now.
Libraries
Programming we can create something from scratch, perform tasks in an easier way and many other applications. Most programming languages during these days need to “import,” “use,” “include libraries which are already created to perform different types of tasks. Most of the times we import libraries on the top of our code (not necessary to be on the top all the time but that’s where we use to put them) the example below shows how few languages include libraries.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
//C++ Libraries
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
//C# Libraries
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.security.*;
import java.lang.management.*;
import java.lang.instrument.*;
//Java Libraries
Comments
By now some people should be guessing is commenting part of programming. Well commenting is not programming but it is A very useful way to describe our code. It is very useful to describe every part of our program (not every line). To comment most programming language use two slashes for a single line of comment (//) and for multiple line commenting use slash and asterisk (/* */) to open and close comments as shown in the example below.
// This is a single line comment in most programming languages
/*
This is a multiple line comment for
most programming languages.
Comments are not going to be compiled;
but they describe our code
*/
Data Types
Data types are also fundamental when programming. There are numbers, characters, strings, Boolean values, constants, variables, and may other data types depending on the language. We try to explain them here.
Numbers
As we all know there are many types of numbers. In programming it is the same thing. We can use Integers, decimal numbers with floating points, Doubles as shown below. Some other languages use only the key word “number” for all of them.
int integerOne = 10;
float floatingPoint = 3.1415;
double decimalNumber = 35.54987;
Operations with numbers
In my opinion operations are similar in most programming languages. The basic operations as addition, subtraction, multiplication, division, square root, power, etc. Use the same operators. All of them are similar. The example below shows how we create two Integers and perform all the operations for these two integers. Symbols are the same (+, – , *, /, sqrt(), ^) there are many exceptions in some languages; but similar in most of them. It also shows the increment and decrement operator which are very useful in most programming languages
int numberOne = 15;
int numberTwo = 10;
int sum = numberOne + numbeTwo; //Result = 25
int sub = numberOne - numbeTwo; // Reault = 5
int mult = numberOne * numbeTwo; //Result = 50
int numbDiv = numberOne / numbeTwo; // Result = (Double = 1.5 Integer = 1)
double squareRoot = sqrt(numberOne + numbeTwo); // Operator inside another operator
//addition inside square root result = sqrt(25) = 5
int numberPower = numberOne ^ numbeTwo; // result = 100,000
int mudulusOp= numberOne % numbeTwo;
// The Modulus Operator divides numbers and returns their reminder Result = 5
int i = 1;
i++
//This is in most programming languages increment operator
//This is going to increase "i" by one in this case i = 2.
int i = 5;
i--
//This is decrement operator. decreases variable "i" by one.
//This case i = 2.
/*
These are the most used operators in math and programming. They may vary its syntax depending on the programming language; but most languages implement them as shown above
*/
Characters
We also have characters in most programming languages. Characters hold a single letter number etc. Numbers as characters can perform character operations but not number operations. Some compilers and programming languages transform them to numbers to perform any number operations; but by definition and programming characters are characters and numbers are numbers. Most languages enclose characters in single quotation marks as the example below.
char vowel = 'a';
char number1 = '1';
char symbol = '@';
//Thre characters defined above. many programming languages use the keyword "char"
Strings
A String in programming is a set of characters together. Strings can be letters, numbers, symbols, etc. The same rule with character applies to strings with numbers. Strings with numbers can perform operations as strings not as numbers. Most languages enclose strings in double quotation marks (“) as shown in the example below.
string String1 = "I am learning Programming";
string numberString = "1234567"; // this is a string not a number
Operation with Strings
We are able to perform string operations. The most useful operation is string concatenation. That is when we have two strings and want to join them in one. Also some programming languages define functions as toUpper() or toLower() which transform string characters (alphabetical) to upper case or lower case respectively. When we see functions below we are going to learn functions deeper. Those were only to show how strings work.
string str1 = "Hello ";
string str2 = "World!";
string str3 = str1 + str2; // outputs Hello World!
string str4 = str1.toUpper(); // outputs HELLO in some programming languages
string str5 = "123456";
string str6 = str5.toNumber(); // converts string to number in some languages
Constants and Variables
In many programming languages today we are going to find values that currently do not have an assigned data type. Most modern languages have that feature. Also modified versions of older languages can have it. Three keywords for these values in most programming languages are “const,” “let,” and “var.” where the value of const never changes, let is a constant but can change using some programming, and var is a defined variable.
let number = 1;
const character = 'C';
var decimal = 3.1415;
Boolean Values
Boolean values are also crucial in modern programming languages. All Boolean values have only one value. The value should be “true” or “false. To apply Boolean values we need a condition. We can now see the example of how we declare a Boolean value. Later we are going to see how to apply them in programming.
bool xyz = false;
bool abc = true
Boolean Operators
As we have Boolean values we have three main Boolean operators in programming. They are the OR Operator (||), the AND Operator (&&) and the NOT Operator (!) See the example below.
bool orOp = A || B; // A or B
bool andOp = C && D; // C and D
bool notOp = !E; not E
// Three main boolean operators They are also logic operators
Conditional Operators
Conditional operators are similar to Boolean operators. Their operation is similar. The most used conditional operators are: Less than (<), Greater than (>), Equal to (==), not equal to (! =), greater or equal (>=), less or equal (<=). We saw the equal sign before (=). In programming only one equal sign is the assignment operator.
A > B; // A greater than B
C < D; // C is less than D
A1 == A2; // A1 is equal to A2
B2 != B3; // B2 is not equal to B3
C4 >= C5; // C4 is greater or equal to C5
D6 <= D7; // D6 is less or equal to D7
const E = 35;
// Only one equal sign in programming is to assign values
// The value assigned to E is 35
Programming Statements
By now we have seen some operations with some data types. We are going to continue working with these data types used in some programming statements. Let’s continue learning.
If Statements
If Statements are very useful in programming. As you can see below, the “if” statement is a conditional statement. Most of the time is used with Boolean operators or conditionals operators.
int grade;
if(grade >= 80){
System.out.print "Your grade is B";
}
Else
Sometimes after the “if” statement we start an else statement. If we can’t accomplish the condition with the “if” statement, then we have another option. As we show in the example below. Sometimes we can create a chain of “if else” statements to accomplish the task we desire.
int grade;
if(grade >= 90){
System.out.println "Your grade is A";
}
else if(grade >= 80){
System.out.println "Your grade is B";
}
else if (grade >= 70){
System.out.println "Your grade is C";
}
else if (grade >= 60){
System.out.println "Your grade is D";
}
else if (grade < 60){
System.out.println "Your grade is F you Failed";
}
Switch Statements
Although we do not frequently use switch statements, sometimes they are very useful. A switch statement is similar to a chain of “if else” statements. We can have any condition or variable to “switch” as we show below. The switch statement chooses the right case and then breaks. We can also add a default case. If the other cases cannot accomplish the condition default case is going to. execute. We can see below how it works.
Bool thereIsGrade;
int grade;
if(thereIsGrade){
switch(grade){
case 60:
System.out.print("Grade is D");
break;
case 70:
System.out.print("Grade is C");
break;
case 80:
System.out.print("Grade is B");
break;
case 90:
System.out.print("Grade is A");
break;
case 50:
System.out.print("Grade is F You are failed");
break;
default:
System.out.print(" Invalid Grade");
break;
}
}else{
System.out.print("There are no grades to display");
}
While Loops
While loops are also very useful in programming. When we need to accomplish a task many times based on the condition. If the condition is not met then the loop ends. The example below shows how while loops work.
int number;
while(number > 10){
System.out.print("Your number is good enough");
}
Do While
“Do while” loops are also very useful in programming. They are similar to “while” loops. The only difference is that they are going to perform a task at least once. If the condition is not met in the “while” loop the loop is not going to start. Example below also shows how “do while” loops work.
int number;
do {
System.out.print(" Your number is good enough ");
} while(number >10);
Arrays
An Array in programming is the most basic set of data. There are arrays of all dimensions. One dimension array and multi-dimensional arrays. In most programming languages define arrays using the square bracket ([ ]). If the array has more than one dimension we use the number of square brackets needed. Also we can assign the array size with a number. The first element for all arrays in all programming languages is 0. We can also assign values to arrays. The examples below shows how we implement arrays in programming.
int myArray[5]; // This is an array of integers and size 5
int myArray2[]; // This is another array of integers with undefined size
string carBrands =["Honda","Toyota", "Ford", "Kia", "Chevrolet"];
//Defined array of strings
int twoDimensions[][]; // This is an example of multi dimensional array
// array index starts at 0
int numbers = [1, 2, 3, 4, 5, 6]; // the value of numbers[0] is 1
//The "numbers" array size is 6 where its last element is numbers[5] = 6
Loops used in Arrays
Besides “while” and “do while loops” We have many other loops used in array implementation. These loops are the “for” loop and the “foreach” loop. We use the“for” loop for iteration and it allows the code to execute repeatedly. We also use the “foreach” loop sometimes as the “for” loop though we only use them to traverse items in a collection. Examples of array loops are shown below.
int oddNumbers = [1,3,5,7,9]
for(int i= 0; i <= oddNumbers.length; i++)
console.WriteLine(i);
// This is an example of a for statement. this is going to output the elements of the array.
// As you may see the "for" loops initializes a variable, sets a condition, and then executes an operation.
// For loops are not only used with arrays; but they are very useful in array implementation.
int pairNumbers = [2,4,6,8];
foreach(int pairNumber in pairNumbers)
{
Console.WriteLine(pairNumber);
}
//Foreach loops are most used in arrays
//As you may see we did not declare the variable "pairNumber" outside the loop.
// We can name elements inside the loop with the name we desire the loop only needs the array name.
Functions
Functions are very useful in programming. A function is a section of code that is able to perform a task and we can call it in any other part of the program. Functions are the introduction for reusable code. There are many ways to create a function depending on the programming language. In many programming languages we can use the keyword function and its name. Then between parentheses we can assign parameters; if we do not wish to set any parameters we can leave the parentheses empty. After the function name and the parameters, we place our executable code between curly braces. Some programming languages do not require the word function. They only require the name of the function parameters and code. In programming languages as “Python” and “Ruby” we define functions using the “def” keyword. Example below shows how functions are created in some programming languages.
Some languages define two types of functions. These two types are “void ” and “return” types. Both types of function execute code; the difference is that the return type of function returns a value as the example below is showing. Also in many programming languages the most important part of the language is the “main” function. Some programming languages don’t require a main function.
function addition(A, B){
return A+B;
}
// The function above has two parameters A and B and returns their sum
function Subtraction(){
var A;
var B
if(A> B){
return A-B;
}else{
console.log("This subtraction cannot be done");
}
}
//The function above does not have any parameters; but declares two variables and subtract them
/**********************************************************************/
using System; //Using the system library
namespace Tester1 // Namespaces in C# are the name of your program
{
class Program
{
static void Main(string[] args) // Main Function C# programming language
{
Console.WriteLine("Hello World!");
}
}
}
// C# Main Function
Exception Handling
In my opinion exception handling is one of the most tedious parts of programming for many programmers. Although it’s also part of programming. Sometimes we try to avoid exception handling; but we cannot do it. The most known exception is present in a calculator function. That the division by zero. As the example below is showing. A lot of programming languages use “try” “catch” blocks as we can see below. Sometimes we use exception throwing with the “throw” keyword as we can also see below.
double A;
double B;
void Div(){
try{
A/B;
}
if(B=0){
catch(divisionByZeroException e){
console.WriteLine(" Division by zero", e);
}
}
}
Object Oriented Programming
Object oriented programming is very important nowadays. We can create our own objects or our own data types and then implement them in our programs. Programming we can set “structs,” “interfaces,” and “classes.” They are similar but each one of them is different. You can see some examples below about how to handle them in most programming languages.
Structs
A struct is a data type which can contain a group of variables and functions to implement under a name. We can access structs by declaring its name in any part of the program. If we need to access a function within a struct, we type the struct name followed by a dot (.) and then the function name. The example below shows how we define and implement structs.
struct Car {
string: Make;
int Year;
string Model;
void beep(){
console.WriteLine(" Beep Beep");
}
} // Struct created
Car myCar = new Car("Toyota",2015,"Camry") //Struct Implementation
console.WriteLine
myCar.beep(); // outputs Beep Beep
Interfaces
An Interface is similar to a struct. In some programming languages such as “java” and “C#” an interface is an abstract type and a class needs to implement them. They are declared using the interface keyword as shown in the example below, and then in curly braces we create our executable code.
interface Address{
int streetNumber;
string streetName;
string City;
string State;
int zipCode;
}// Creation of an interface
Classes
Classes are the base of object oriented programming. We can create classes as we create structs and interfaces using the “class” keyword. The main difference between a struct and a class is that a class supports “inheritance” which is also of great value in modern programming. We did not mention before that we can have public or private member variables and functions in classes and structs. We use them more often in classes. If we do not define a member in a struct is set to public, in a class is set to private. The difference between private and public members of the class is that private members can be accessed only by the class members while public can be accessed by outside members. There are also protected members in classes which we cannot modify outside any part of the class. Example below shows how classes are defined.
class vehicle {
public string Make;
public int Year;
public void beep(){
console.WriteLine(" Beep Beep");
}
}
vehicle myVehicle = new vehicle("Honda", 2010);
myVehicle.beep(); // Outputs Beep Beep
Class Constructors
Although sometimes it’s not necessary in some languages we can create a constructor for a specific class. The constructor must have the same name of the class, and it specifies the members of the class. The class invokes the constructor to perform its tasks.
class Car{
public string Mk;
public int Yr;
//Constructor
public Car(string make, int year){
make = Mk;
year = Yr;
}
}
Inheritance
While creating new classes maybe we want it to have some other features from an existing class. If that is the case, we can create a class derived from another class. That class is going to have access to members of the base class. In some programming languages inherited classes do not have access to private members of the base class; but that is depending on the language.
class Vehicle{
public string make;
public int year;
} // Base class Vehicle
class Van extends Vehicle{
int NumberOfPassengers;
}// Class inherits all public members of base class. In this case it inherits make and year
Generic Collections in programming
We had a brief explanation about arrays above. In programming arrays are not the only sets of data. In some programming languages we implement something called “generics.” Generics can be derived from any data type. The most useful generic data types used in many programming languages are “linked lists,” “Queues,” and “Stacks”
Linked Lists
A “Linked List” in programming is a set of data where the elements are made of two items. These two items are the “data” and “reference.” The order of the elements is linear not given by their placement in memory. Each element points to the next (they are a sequence). A linked list uses a “First in First out” (FIFO) method in order to add or remove elements
List = new LinkedList<T>
//T is the data type
StringList = new LinkedList<T>
Stack Data Structure
A stack in programming is a collection where we can add or remove elements. Operation for stacks in most programming languages are the “push” and “pop” which add and remove elements respectively. In a stack, elements use the method “Last in First Out. (LIFO)”
Queue
Queues in programming are similar to Linked List and Stacks. They use the FIFO method in order to add or remove elements. In some languages its elements can only be inserted at the back and removed from the front
Peek Pop and Push
There are three main operations for generic elements in programming. These three methods are peek, pop, and push. As we state above, the push method adds elements to the generic collection. The Pop method removes elements. The Peek Method passes on the next element but does not remove it. In queues some programming languages can implement the functions “enqueue” or “dequeue” to perform the mentioned tasks.
Multithreading
In programming “Multithreading” is an execution model that allows to perform multiple tasks under the same process. Multithreading or Multitasking is very useful for programmers. When we start programming maybe we think it’s not necessary; but once we acquire some experience we learn that it is very useful.
Conclusion
As we stated above this is the key for all programming languages. This tutorial does not show any specific programming language; but shows a great foundation for all of them. Once we learn the concepts explained here. We would be able to learn any programming language quicker. We also skipped many concepts such as “enums,” “encapsulation,” “Polymorphism,” and many other Object oriented programming concepts, because they are most learned once we dive deeper in any specific programming language. I hope you enjoy learning programming.