Understand the basics rules of programming | web stand

Programming basics

0

At the core of programming there are a few concepts that any developer should be familiar with, no matter the language the programmer works with, the framework he uses or the purpose for which he is coding for. Understanding these concepts is needed when someone is learning his first programming language.
Click on the phrase that you wish to explore about and the page will scroll down to it.

Variables & data types

Variables are where the information is stored, some languages demand that you first declare a variable before using it and some are not so strict. When declaring a variable the computer is interested to know what kind of information is stored inside the variable, this kind of information is called “data type”, here are the different kinds of data types:

Boolean type

This variable has only 2 options- true or falls. It can also represent the 0 and 1 in the simplest computer language.

Text type

Character (char)- is a single character variable, it can be a number such as: 4, 7, 8 and it can be also be a letter or symbol such as: %, T, p. These days, we use high level languages, so we don’t really use character variables, but we do put them together to create a string.

String- is a variable that contains a few characters strung together in any length, for example: “apple” is a string and “my name is Dorothy and I love to eat pizza” is a string also. A string can contain numbers as well but you can’t use it to do mathematical actions.
For example- if you try to add these two strings together:
a=“222”
b=“111”
The result will be:
a + b=“222111”
Because it’s acting like text.

Numeric type

Floating-point number (float) – is a variable in which you can assign a number with digits after the decimal point such as 108.756

Integer (int) – is a whole number with no digits after the decimal point such as 108.

The reason there are two data types in the numeric type is because the float takes more storage space so if it’s not a must, try to use integers instead of floats.

Composite type

Array– is a bunch of variables held together so later on there will be an access to them in different parts of the code. For example this is an array of strings {mom, dad, brother, sister}, they are indexed like that automatically inside the array {0, 1, 2, 3}, so mom is the number 0 string, dad is 1, brother is 2 and sister is three. Yes I know, it’s weird that the first one is 0 but you will get the hang of it pretty soon.
This array {3, 6, 10} is made out of integers,  when you index it later on and multiply the number 0 in the number 2 in this array you will get 3*10=30.

Equality, Assignment and conditions

The code from this point and on will be represented using JavaScript, after all, we are all here to learn web development are’nt we? it’s important to say that the syntax in all languages will be a bit different but the logic is the same in all of the languages.

Question: What’s the difference between:

  1. a=10
  2. a==10

Answer: the first one means that we would like to assign the value “10” to the variable “a”. The second one means that “a” equals “10” and mainly used when we want to test something.  If you lifted your eyebrow up and wondered what’s that mean, don’t you worry, I’ll explain.

If you wish to test the variable “a”, and check if it equals “10”, you can use the condition “IF” to do so.
If this is true, something will do something else. For example:

The user is given this equation: “5*2=   “, the blank space is the user input where he can answer the question, we declared that this input is variable “a”.
If the user puts the number “10” in the input the program will send him “way to go!” to greet him. The code can look like this:

var a;
If (a==10) {document.getElementById("demo").innerHTML = “way to go!”;}

We don’t use “a=10” when we do this kind of tests, we do it only when we want to declare a variable and assign a value to it.

Let’s look at our “IF” phrase again, if the user prints a different number and not “10” nothing will happen, in the real world we would like to inform the user that he made a mistake, so we use the “ELSE” phrase.

Like this:

var a;
If (a==10) {document.getElementById("demo").innerHTML = “way to go!”;}
else {document.getElementById("demo").innerHTML = "Try again”;}

Loops

FOR loop– a loop is a bunch of code to be executed again and again. The “FOR” loop is being executed a limited number of times, we determine this limitation and tell it when to stop. For example:

We create a “FOR” loop that prints the numbers 1-5, the code is printing just one number at a time, but because it’s a loop it happens again and again:

Var i;
For (i=0, i<6, i++) {document.getElementById("demo").innerHTML = i;}

This code means that we first declare a variable called “i”, then we start a loop with the phrase “FOR” and it will start at i=0, then it will check to see if the condition is true (the condition is “i<6”), if it’s true, “i” will be increased by “+1” (in programming, the phrase “++” means exactly like “+1”) and it will print the result, now “i” had been changed and it’s bigger by “+1”, then the code repeat itself all over again until “i” is no longer smaller then 6 and this is when the loop will stop. The result in our case will be “12345”.

WHILE loop– this loop is very similar to “FOR” loop, it looks a bit different but it does the same thing. We use a while loop when we don’t know when it will end. In our example above the “FOR” loop will happen 5 times exactly. We use a “WHILE” loop when we don’t know the number of times the loop will keep on happening. For example when we wait until something will be valid, this will take an unknown number of loops to check until it will be true and it’s better to use a WHILE loop for that.

Functions

A function is a bunch of code like variables, loops, conditions and more held together in one place, when the function will be called later on in the program the code inside it will be executed. All you got to do is to name the function and call it later on by its name.
For example, here is a function named: “clockAngle()”, it uses JavaScript to check the current time and calculate the angle between the clock’s hands, it contains IF statments, variables and it does some simple calculations:

\\ angle between clock's hands

function clockAngle() {
var d= new Date();
var x= d.getHours();
var y= d.getMinutes();

if(x>12) {x-=12};

var z;
z = 30*x - 5.5*y;
if (z<0) {document.getElementById("demo").innerHTML = z * -1 + " degrees";}
else {document.getElementById("demo").innerHTML = z + " degrees";}
}

If you wish to invoke it (make it start running) you can create a button in HTML and add an “On Click” event, like this:

<body>
<button onclick="clockAngle()">Click me</button>
<p id="demo"></p>
<script>
function clockAngle() {
var d= new Date();
var x= d.getHours();
var y= d.getMinutes();

if(x>12) {x-=12};

var z;
z = 30*x - 5.5*y;
if (z<0) {document.getElementById("demo").innerHTML = z * -1 + " degrees";}
else {document.getElementById("demo").innerHTML = z + " degrees";}
}

</script>
</body>

When a user will click the button, the function will calculate the current angle between the clock’s hands and will display it inside the <p> element.

Difference between programming languages

If all the programming languages have the same basic idea, and they all use the same ideas, what’s the difference between them?

Syntax– you can do the same thing in different languages, it looks similar but it’s a bit different. This is why learning the concepts is the most important thing when someone is just starting to learn how to program, and then, learning another language will be much easier because he just need to learn the new syntex (and the functions of course).

Here is how to assign the value “36” to the variable (“int”, in this case) “b” in different programming languages:

JAVASCRIPT

Var b=36;

C\C++

Int b=36;

PASCAL

Var b: Integer=36;

 

Strong and weak typed– as you can see, in JAVASCRIPT, you don’t have to mention that the variable is an integer. Because there is a number assign to the variable and it’s not inside quotation marks, it’s been interpreted automatically as an integer. When you don’t have to be specific about everything like the variables, or when you try to add an integer to a string and something is being outputted and there is no error, it means that this language is a “weak typed language”. A “strong typed language” such as C, won’t let you do these kind of actions and you have to be specific about everything.

Functions– it’s possible to define a new function and decide exactly what it would do no matter the language you are using, the difference is the built in functions because each language was crafted to do certain tasks.

For example, PHP has a lot of built in functions that are doing tasks that are very useful when handling the back end of websites, JAVASCRIPT on the other hand, has its own built in functions that are more suitable for the front end of websites, nevertheless, it is possible with both languages to create the other one’s functions from scratch so it will be possible for both of them do the same actions. Each built in function can include many lines of code so it’s better to use the language which has more functions that suits your needs.

Featured image credit: “Book”, by Federico Feroldi, under license CC BY-SA 2.0

Share.

Leave A Reply

3 × 3 =

Top