coding

8 basics you need to learn at the start of your programming journey

There are some basics that are required to do any type of programming and these basics carry over to any or most programming languages, you’ll ever encounter. So, let’s start our list of “things you need to know at start your programming journey”.

1. Values

Value is a representation of an entity that a computer program manipulates. In other words, it is a fundamental thing; for eg. a number, letter etc that a computer program manipulates.

For eg.

3

3.501

"I love JavaScript"

false

true

2. Operations

An operation consists of two things

  • Operator
  • Operands

An operator is something that operates on the operands.

I guess the examples will make it clear.

5 + 2 // plus sign is an operator; 5 and 2 are operands 

5 - 4 

4 > 3 

a++ // also known as an increment operator, increases the value of a by 1 

true && false // similar to Boolean 1*0 which results to 0; if one operand is false in & operator operation then the resulting value will be "false"

true || false // OR operator; similar to Boolean 1+0 which results to 1; if one operand is true in OR operator operation then the resulting value will be "true"

!true // also known as "not" or "negation" operator. Here is negates the "true" operand and results into the result "false" after the operation. This is also an unary operator as only one operand is innovated. Above examples were mostly of binary operators. 

If you want to read more on operators, this tutorials-point page is a good resource.

3. Data Types

A data type is simply a property of data that lets the computer (compiler) know how the programmer intends to use that data. We talked about operations and values above. Different types of operators and values are required according to data type for the right operations. One example of this can be 5 and 6 can be added, multiplied, subtracted because the data type of these two values 5 and 6 is a number or an integer depending upon the programming language.

They can be further divided into

  • primitive data types
  • non- primitive data types

The above figure illustrates various data types in Java. All the programming languages have similar set of primitive data types with some differences here and there.

4. Variables

Variables are often described as containers to hold values. That definition is kind of wrong. There is problem in that definition. I’d suggest you to think of a variable more like a label for your memory address where the data lies. In other words, variable is a name that acts as a reference to the data of where the data resides in the memory. So, memory address/es is more like the container to hold values, a variable merely is a label or reference to that.

How variables are defined is depended upon the typing system the programming language uses.

In statically typed languages, the description that what type of data is the variable going to hold is provided in the declaration

int i; // uninitialized integer type 
int i = 0; //initialized integer type 
float b; // variable floating point number 
char d; //variable to hold a character. 

In dynamically typed languages, the data types are rather resolved during the run time so a single keyword is used to declare all kinds of variables.

var a = 0; 
var b = "String"; 
var c = false; 

// single var declaration is bring used to declare variables for different types of data in Javascript which is a dynamically typed language. 

5. Statements vs Expression

If we compare programming to English language

Expression = a phrase

Statement = a sentence

int age = 1; // age = 1 is an assignment expression; not much distinction between statement and expression here. 

age =  1 + (age*2); // consists of various different expressions as explained below

The second example has following expressions:

  • 2 -> literal expression
  • age -> needs to get value age variable is referencing to; also an expression.
  • (age*2)
  • 1 -> literal expression
  • 1 + (age*2) ;
  • age = 1 + (age*2);

So, the statement “age = 1 + (age*2);” consists of 6 expressions.

We learnt about the operators above, expressions in the statement get evaluated on the basis of something called operator precedence. When it comes to mathematics, it is similar to BODMAS rule that you learnt in school. Although, you may google the specific operator precedence for your language. Here is a great explanation for C lang on the topic.

6. If & else (Conditionals)

In programming, we often want to make decisions based on conditionals. Think of it like what if user has added items in shopping cart in our eCommerce website and wants to check out or hasn’t added any items? We want to show two different things to the users based on these two different conditions.

let age = 25;
if(age>=18){
console.log("you are old enough to view the website"); // console log prints to console
} else if(age>=15){
console.log("you are old enough to view the website with some content blocked");
} else {
console.log("sorry, you are not allowed to proceed");
}

So, the decision flow is like .. if condition is met do something, if not but some other condition is met then do something else. If none of of the conditions are made do something default. Now, you can only use ‘if’ part if that’s all you need. Some somethings you’ll need both if and else and for others you may require a lot of ‘else ifs’ in between.

Another way to do it, if you might end up with too many ‘else ifs’ is to use ‘switch case’ statements.

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  case z:
    // code block
    break;
  default:
    // code block
}

7. Loops

Often times in programming, we need to repeat something over and over and over again. Let’s say you had to print number ranging from 1 to 90,000. If you do that with print statement, you’d have write 90,000 print statements, so it makes sense to loop things.

Above image shows that, a loop loops until the certain condition is true and the program exits the loop once it is false. If the condition is always true then it’ll create an infinite loop that’ll end up hanging your machine.

for(let i = 0; i
let i = 0;
while(i
let i = 0;
do {
console.log(i+"views");
i++;
} while (i

For loop consists of three parts

  • Initialization: i = 0; // so the counting of our loop starts from 0
  • increment-er/decrement-er i++ // increments value of i by 1 on each loop
  • condition; i<=300 // means 'i' is less than or equal to 300; so our loop runs until the value of 'i' is 300

While loop is similar, we do

while (some condition is met) {
do something
}

We usually use 'for loop' when we know how many times we have to loop through. In cases, where do not know how many times to loop through, we use while loops.

while(number!=0){
//execute code
} 

// code runs until number is zero

The difference between 'do while' and 'while' is that since ' do' parts comes before conditional in 'do while' loop. The code block runs at least one time whereas, in while loop, it may not even run. Sometimes that may be exactly the behavior we are looking for and sometimes not at all.

Note: Some programming languages also include for-each loops and others which is out of the scope of this article.

8. Functions

A function is an organization of a block of code that does a single action every time you call it so you can call it over and over again to do the same thing. Let's say, you wanted to display "welcome to my website" every time a user clicks the 'welcome' button. You'd put the code in a function, and every time the user clicks it, the function is called.

Print() is a function. It's a function that takes a string parameter and prints in to the screen/terminal.
for eg. Print("print this to terminal") outputs "print this to terminal" on screen. Some functions do take parameters and some do not. But all functions are executed when called.

function add (let a, let b){
  return a+b; 
}  // function definition 

add(5,6);  //function executes; results to return value of 11

//functions can also have no arguments 


function print(){
console.log("I love printing to screen");
}

print(); // function executes. prints "I love printing to screen" on console. 

Note functions in static languages such as C/C++ are more nuanced than this. They also include of something called return type in which you have to mention what type of data is being returned through your function. You may refer to this article for that.

I'd like to end this article here. You may go to my blog's programming section to read other articles I've written on programming.

Leave a Comment

Your email address will not be published. Required fields are marked *