Skip to main content

How to Code? Learn These 5 Basic Concepts of Programming


How to code: 5 basic concepts of programming
unsplash-logoLewis Ngugi
If you are intrigued by the title and you are reading this, it means that you are interested to learn how to code. You might have heard people saying that programming is a God gifted skill and not everybody is blessed with it or programming is not for people like you or me or whatever. Believe me, that's bullshit. Computer programming is the most interesting and the easiest skill to acquire in computer sciences. At least, I think so. It is said that one can do almost everything with programming and its true. If you can do everything with something, it then definitely worths a lot of your time.
A question arises here that how much time one should dedicate daily to learn how to code fast and effect? I would say 8 hrs if you are on your own. To reduce the time and effort you need to learn programming, you should benefit from those who have experience and are good at it.
As far as I think, you can boost up your learning process if you learn the basics well. Different authors have a different opinion about what are the basic concepts of computer programming. They are also right. Everyone has own perspective. Here I have listed 5 of the basic concepts of computer programming. I am sure it will save you pretty much time and effort.
I am not saying that you will become an expert in programming after reading this post. I am just helping you to get started, to give you a push. You will get an idea of what are the basics of programming and how these basic concepts work. In order, to get a full understanding of how to code I would recommend you to read 3-5 books of programming fundamentals.

Programming Fundamentals

1. Variables, Constants, and Arrays

Anything that you write in your code is stored in computer memory slots. Each slot has a unique address. To read from and write to a memory location, its address is used. But it is hard to work with numbers like 0x19a823f2... this is how memory addresses look like. Naturally, humans are good at working with names than numbers. That's why a genius (idk who but God bliss him/her) thought that why not to use real world names to deal with computer memory. Variables, constants and Arrays and other similar things are named memory locations. Each offers a different approach to handle memory read/write operations.
You need to remember only 3 things about variables and related terms.
  1. It is a memory location.
  2. It has a name.
  3. It has some data.
Remember, a variable is always a variable. Irrespective of the language that you are using, every variable will have the above-mentioned properties. Once you declare a variable with a name suppose x, a constant or an array the operating system selects one or more memory slots (depends on the type of variable) and attach your given name to these slots. Anywhere you use that x in your code, the mighty Operating System resolves it into the memory address it is attached to. Hope it makes sense.

Where to use?

Use them wherever you have to read/write some data to/from memory again and again in your code.

Constants are the same as variables but you cannot change its value once it is initialized.

Where to use?

Use constants when you have to write data only once and read many times.

An array is just a sequence of memory addresses. It has a name, data type and data. The only difference is that it is just a sequence of memory slots. To use a specific slot in this sequence, indexes are used.
It has also been observed that human remember the things which they see more than the things that they hear. For your convince, a video is included below which further explains the concept.

Where to use?

Use arrays whenever you have a sequence of the same kind of data.

2. Conditional Statements

Conditional statements (the decision is taken based on some condition(s)), also called control statements (control the flow of execution of your code) or if statements (have if keyword in syntax). We will use the term if statement onward.
If statements come to the game when there are multiple choices available and one is to be selected based on a given condition. When there are multiple paths of execution if statements are used to direct the execution flow to one of the paths. It decides whether to execute a block of code or not.
The easiest way to know where to use if statements are to look at your problem statement and identify if word. Any condition following the if word will be your if condition. Real world conditions are depicted in code by using variables and expressions. e.g if a person is at least 18 years old, show him the content (some age-restricted content).
There are different varieties of conditional statements suitable for different scenarios.
  • if statement:

Single if with one conditional expression and a single block of code. Decides whether to execute a piece of code or not.

Example: if something happens to do something.
  • if-else statement:

single if with one else. Has two blocks of code to choose from for execution based on only one conditional expression.


Example: if something happens then do something otherwise do something else.
  • if-else-if statement:

It is the same as if-else but with multiple conditions. Each condition following the first condition is written with the keyword else if. It has more than 2 blocks of code to choose from for execution.

Example: if something happens then do something if something else happens, do something else and keep it going.

Yes, I know, the examples are extremely dumb. Just tried to be more generic. If you have better examples, write in comments. I will include it.if-else-if statement : imgif and if-else statements : img

3. Loops

A loop is something which starts from one point, goes around and ends back on starting point. If there is a block of code which you see is consecutively used several times, add it to a loop. Note the word consecutively here. We will refer to it in the next section.
When a block of code is written inside a loop body, it executes again and again. The execution control starts from the first statement of the loop body and progress until it reaches the end. In the end, it returns back to the start of the body. But wait, when will stop executing? Never?
A loop is similar to if statement with an extra feature. The body of a loop is executed again and again based on a conditional expression. If the condition is true, the body is executed, otherwise not. The control returns back to check the condition every time it finishes executing the block of code until the conditional expression evaluates to false. That's why a dynamic conditional statement (not officially called dynamic) is required in loops.
There are 4 widely used variations of loops in popular programming languages like JavaScript, Python, C++, Java. All the variations do the same thing, execute code repeatedly. However, each variation comes with a slight modification suitable for different scenarios.
  1. For Loop

    For loop is mostly used as a counter. Like if you want to execute a block of code several times, for loop is the best choice.

    Keyword: for

    Components: 

    1. initialization
    2. conditional expression
    3. condition update statement
    4. body
      the conditional expression is mandatory and the other two are optional.
  2. While Loop

    If you want to run a block of code repeatedly while a condition remains true, go for while loop. You make for loop work like a while loop by ignoring its 1st and 3rd component.

    Keyword: while

    Components:

    1. conditional expression
    2. body
    Both of the components are mandatory.

  3. Do-While Loop

    The do-while loop comes in handy if you need a block of code at least once whether the condition is true or false. After the first iteration, it works like a while loop. However, in while loop, the condition is evaluated first before its body is executed and in do-while, the condition is evaluated at the end of iteration. Unlike while and for loops, two keywords are used in do-while.

    keywords: do, while

    Components:

    1. body
    2. condition
      both components are required.
  4. For-Each Loop

    For-each loop is more suitable for situations when you have a list of elements and you want to execute some code for each element of the list. For example, you have a list of some sort of geometrical shapes and want to change the color of each object, use for-each instead of for and while.

    keywords: for-each has a syntax similar to for loop in almost all languages. Some languages use each keyword in combination with for and some use : operator.

    Components:

    1. condition
    2. body
    both of the components are mandatory.
types of loops in programming

4. Functions / Methods:

an activity that is natural to or the purpose of a person or thing.
Here, we are not talking about the function of a person or things but a function of a block of code. The reason to include the above definition is to present an analogy between functions in the real world and functions in programming.
You know what is the function of a printer, don't you? It prints electronic information on a paper. Printing is a sequence of related events. A printer pulls a paper from its tray, prints the queued information on it and pushes it smoothly. Similar to this, a function in programming is a block of code which performs a sequence of related operations. That's it.

Components:

  1. Function Name
  2. Parameters
  3. Return type
  4. Body
  5. Function Call
All the 5 components are mandatory except parameters. A function must have a name which is used to call it, a body which is executed on each call, and a return type which is the type of data a function produces as a result. Parameters are optional. The same naming conventions are followed which are used for variables. 
If a function needs input data from another scope then it is given either as a parameter or globally. There are many ways to exchange data between the function body and the rest of the code. Function call necessary for function execution. If there is no call to a function, it will never execute.

When to use functions?

If you have a piece of code for something and you need it more than in one places in your program, enclose it in a function and call it everywhere you need it.
Functions and the upcoming topic deserves more of our time. I will write separate posts for each in detail.

5. Class and Objects:

You might have heard that class is a blueprint and an object is an instance of a class. I have read it like a lot time in books and blogs and it was the most confusing statement for me ever. Class and Object are the base concepts of Object Oriented Programming. If you a beginner (most probably you are if you are reading this), it will take you some time and some effort to understand what actually class and objects are and how to better use them. You will get some help from here and a lot more in the coming posts.

What is a Class?

In real-world problems we have several physical and non-physical objects like if you are working on a game, your main character is an object, a weapon is another object, enemies are separate objects. All have some features and functions. Based on their functions, we can categorize these objects. We can put all the similar objects in one category. For example, Weapons are similar to each other while different from humanoid characters.
In OOP, such categories become classes. In the above example, the weapon is one class while the enemy is another class.
In functions, a number of operations which belong to a single event are combined and used again and again with just one function call. Same is the case with classes. Functions which perform operations related to a single category or type of objects are combined into a single class. Obviously, these functions need some data to operate on, so the related data is also encapsulated with these function within the class.

What is an Object?

Can you sleep inside a blueprint of your house? Never. The map/blueprint must first be brought into a physical shape. You have to bring it into existence. It will occupy some space on earth. Yes, don't shout. I know people are trying to live on Mars but it may never happen. But there are crazy inventors are still alive. who knows. Anyhow, same is the case with classes. Once you create a class, it does not occupy any space in computer memory (RAM). So you cannot use it. Sorry.
Unless and until at least one object of that class is created. 
The object is used to access methods and data of a class. An object is created almost the same way a variable is created. It has a type (class name) and a name. The same naming conventions are followed to create an object of a class which is followed for functions and variable.

Comments

Popular posts from this blog

How Big Data Analytics Can Help You Improve And Grow Your Business?

Big Data Analytics There are certain problems that can only solve through big data. Here we discuss the field big data as "Big Data Analytics". The big data came into the picture we never thought how commodity hardware is used to store and manage the data which is reliable and feasible as compared to the costly sources. Now let us discuss a few examples of how big data analytics is useful nowadays. When you go to websites like Amazon, Youtube, Netflix, and any other websites actually they will provide some field in which recommend some product, videos, movies, and some songs for you. What do you think about how they do it? Basically what kind of data they generated on these kind websites. They make sure to analyze properly. The data generated is not small it is actually big data. Now they analysis these big data they make sure whatever you like and whatever you are the preferences accordingly they generate recommendations for you. If you go to Youtube you have noticed it kn…

AI Vs Machine Learning Vs Deep Learning

AI Vs Machine Learning Vs Deep Learning Artificial intelligence, deep learning and machine learning are often confused with each other. These terms are used interchangeably but do they do not refer to the same thing. These terms are closely related to each other which makes it difficult for beginners to spot differences among them. The reason I think of this puzzle is that AI is classified in many ways. It is divided into subfields with respect to the tasks AI is used for such as computer vision, natural language processing, forecasting and prediction, with respect to the type of approach used for learning and the type of data used. Subfields of Artificial Intelligence have much in common which makes it difficult for beginners to clearly differentiate among these areas. Different approaches of AI can process similar data to perform similar tasks. For example Deep learning and SVM both could be used for object detection task. Both have pros and cons. In some cases Machine Learning is …

How Computers Understand Human Language?

How Computers Understand Human Language? Natural languages are the languages that we speak and understand, containing large diverse vocabulary. Various words have several different meanings, speakers with different accents and all sorts of interesting word play. But for the most part human can roll right through these challenges. The skillful use of language is a major part what makes us human and for this reason the desire for computers that understand or speak human language has been around since they were first conceived. This led to the creation of natural language processing or NLP.
Natural Language Processing is a disciplinary field combining computer science and linguistics. There is an infinite number of ways to arrange words in a sentence. We can't give computers a dictionary of all possible sentences to help them understand what humans are blabbing on about. So, an early and fundamental NLP problem was deconstructing sentences into small pieces which could be more easily…

Introduction to Data Science: What is Big Data?

What Is Big Data First, we will discuss how big data is evaluated step by step process. Evolution of Data How the data evolved and how the big data came. Nowadays the data have been evaluated from different sources like the evolution of technology, IoT(Internet of Things), Social media like Facebook, Instagram, Twitter, YouTube, many other sources the data has been created day by day. 1. Evolution of  Technology We will see how technology is evolved as we see from the below image at the earlier stages we have the landline phone but now we have smartphones of Android, IoS, and HongMeng Os (Huawei)  that are making our life smarter as well as our phone smarter. Apart from that, we have heavily built a desktop for processing of Mb's data that we were using a floppy you will remember how much data it can be stored after that hard disk has been introduced which can stored data in Tb. Now due to modern technology, we can be stored data in the cloud as well. Similarly, nowadays we noticed …

The Limits of Artificial Intelligence

If you are here, it means that you are familiar with term artificial intelligence. Either you have read about it in school or have seen it in sci-fi movies or somewhere else. Talking about the limitations of AI, let me ask you one simple question first, do you know the definition of AI? You might be thinking to answer me with a yes, yes I know what is artificial intelligence. But what if I tell you that AI is a buzzword and it is almost impossible to properly define. It is this way because the definition of artificial intelligence is moving. People don’t call the things AI that they used to call. For example, a problem that seemed too complex to be solved by human and was solved by AI algorithm is no longer a problem of AI. Playing chess, is one of the examples. It was considered the peek level of artificial intelligence back in previous century. Now it hardly fits the criteria for AI. It is presented to the world as a super power that when given to a computer, it magically starts li…

How To Become A Successful Programmer?

How To Become A Successful Programmer? I have heard many novice programmers saying I want to get better at programming but there is hardly a slight improvement in their skills. I have observed that most of them say they want to get better but that is just a wish. They do not really mean it. They mere wish to improve their skills. They do not work for it. Your wish does not guarantee that you will become a successful programmer. Many other people who have developed an interest in computer programming do not know how to reach to a point where they will be called successful programmers. They either keep wandering in the middle of nowhere or just give up. The same response is for them too as it was for the wishers. Your interest does not guarantee that you will succeed. Programming is a field which requires intensive work to master. Along with improving your technical knowledge of programming, you need to work on your interest. You need to develop a habit of not giving up. You need to…

What is Multithreading? JAVA Multithreading Tutorial

It is almost end of 2017. The computer has evolved throughout its age from a simple, huge machine which was used for just simple numerical calculations to a small and swift electronic device which is affecting almost every aspect of our life. There are a lot of efforts involved in these enhancements in both hardware and software. Powerful hardware has been invented, and robust software techniques have been designed to improve hardware efficiency. One of these methods is multithreading and this is what we are going to talk about.
Multithreading is the ability of a single processing unit to execute multiple programs concurrently, apparently supported by the operating system. Multithreading is achieved either by multithreaded architecture or by software techniques or by both. All processors and OSs today support multi-thread execution.
We are talking about multithreading but what actually a thread is? A thread is a single unit a single processor can execute. The group consists of the sh…

5 Tips for Computer Science Students

You are in college now so I am skipping the basics the go to class do your homework study for tests stay out of the hospital. These are not all important pieces of advice but I am sure you have heard them. Instead, let’s talk computer science. Here are some tips I have specially collected by talking to students who wish they’d heard them when they were students. Listen up.

Seek help when you need it. Your classes are going to get harder, they are going to test your knowledge but that’s why you are there for. Some people find attending office hours or seeking extra help to be embarrassing. But these resources are there for a reason. Taking advantage of the help you are offered will not only help you prepare for future classes and learn the material better but a lot less harmful than bad grades or any other consequences of struggling.Don’t let yourself intimidated by large projects. The best thing to do, sit down a day at the assignment and break it up into smaller tasks. A lot of times…

Supervised Learning vs Unsupervised Learning vs Reinforcement Learning

Supervised Learning vs Unsupervised Learning vs Reinforcement Learning Machine learning models are useful when there is huge amount of data available, there are patterns in data and there is no algorithm other than machine learning to process that data. If any of these three conditions are not satisfied, machine learning models are most likely to under-perform. Machine learning algorithms find patterns in data and try to learn from it as much as it can. Based on the type of data available and the approach used for learning, machine learning algorithms are classified in three broad categories. Supervised learningUnsupervised learningReinforcement learning An abstract definition of above terms would be that in supervised learning, labeled data is fed to ML algorithms while in unsupervised learning, unlabeled data is provided. There is a another learning approach which lies between supervised and unsupervised learning, semi-supervised learning. Semi supervised learning algorithms are giv…

Machine Learning: A Truthy Lie?

For all these years, we all have been misguided by the term machine learning. We have been told that machines learning makes a machine capable of how to think, how to act like a human. Machine learning is the most misused term. It does not really mean what it sounds like. It is a lie, a truthy lie. What is meant by a truthy lie? Each year Merriam-Webster releases a top 10 list of most searched words. In 2003, the top word in the list was democracy. In 2004, the word blog made it to the top. The winning word for the year 2006 was trustiness, "Truth coming from the gut, not books; preferring to believe what you wish to believe, rather than what is known to be true". A word which could be a lie is used so often that it eventually feels like truth. "Bet on the jockey, not the horse" is a truthy lie. Similarly, "machine learning" has been used over time for any kind of activity to train a machine or a computer so it could think or act like a human. The word i…