The Logic of Programming

logic-of-programming

What is the programming?

Programming is the process of taking an algorithm and encoding it into a notation using a programming language, so that it can be executed by a computer. But one thing to note though is that programming is not all about the programming language, it is really about solving problems.

I am Odinaka Joy and on this topic I will be writing about programming, it’s best practices, how it is different from coding and lots more. So let’s dig in

Programming is all about giving the computer instructions. To achieve that we need to give the instructions thinking like a computer. which means that we basically need to:

  1. Analyze the problem, break it down into chunks
  2. Create a series of instructions
  3. pick a programming language you are comfortable with
  4. Ensure you avoid ambiguity, always keep it simple

Computers cannot make assumptions or guess what we want, hence we have to explain every concept clearly so that it understands exactly what we want. To achieve this, we need to understand the logic flow.

The Building Blocks Of Programming

These are the building blocks of programming. They are ways a programmer not only give the computer instructions but also specify how it executes them.

Objects

Objects in programming can be used to model objects from the real World. It is a useful way to group together details. An Object is defined by what attributes it possesses:

  • Attributes: These are features an object has that describes it
  •  Methods: These are what an object can do, that is, it’s behaviors or actions it can perform
  • Access Modifiers: These are access levels that can be granted to an object’s methods and attributes – Public: This means that it can be used within or outside the object that defines it. Protected: This means that it can be used within or only by child objects that inherits the object that defines it. Private: This means that it can only be used within the object that defines it
  • Abstraction: Abstraction serves to hide the complex mechanisms within an object, leaving only the information that we need in order to interact with it
  • Encapsulation: Encapsulation is the building together of attributes, methods and access control in order to protect an object.
  • Inheritance: An object can be inherited and used by a child object, both it’s attribute and methods
  • Class: A class is a blueprint of an object. It defines the attributes, methods and access level of an object
  • Instantiation: This is the process of deriving a new object from a class. Each new object is an instance of the class

Data Storage

Variables – These are like storage boxes where you can put data you want to store in a program. These  variables have a name and a type. Names are used to easily to access the data when needed while the type ensures the right type of data is put into it.

Data Types

There are two categories of data types:

Primitive Data Types – These are single value types. Some of them are character, number, float, boolean, null, undefined

Complex Data Types – These are data types composed of one or more primitive or complex data types. Some of them are string, array, object

Understanding The Logic Flow:

Logic flow is simply a step by step set of instructions. Each step is done sequentially, which means one after the other.  Programs are written in sequence following the top-down approach and so it is executed by the computer. Every line is an order that flows from top to down.

Programming Concepts

 

  1. States: States in a program can be – Boolean (true or false), Empty (something that exists but has no value yet), Null (something that does not exist and will never have a value), Logical Operators(AND or &&, OR or ||, NOT or !)
  2. Conditions: This is used to alter the default flow of a program. The programmer specifies different conditions and only the block of code that is true gets executed and others are ignored. it uses comparison operators (<, >, <=, >=, ==, ===)
  3. Loops: This is also another way to alter the default flow of a program. Here, the programmer instructs the computer to run a particular condition repeatedly over a period of time as specified.
  4. Functions: This is simply instructions as instruction meaning that some similar instructions are put together and executed as one. This helps to make our program modular and DRY (Don’t Repeat Yourself) as it can also reused as many times as desired.

Steps To Solving A Problem

Understand The Problem: The first step to solving a problem is to understand the problem. For example, we want to pass in several values as input and return only the even numbers. To solve this, you should answer the following questions:

  • How will I specify this instruction? Ans: Function
  • How will I pass the values to the computer? Ans: Array
  • What will be the data types of the values passed? Ans:  Number (Integers)
  • How can a computer tell what is an even number? Ans: number % 2 === 0
  • What is the goal of this function? Ans: To return all even numbers in the array or empty array if none

Work through the problem manually with at least three sets of sample data. Following the example above: [1], [1, 2], [1, 2, 3, 4, 5, 6]

  • For sample one, check if it’s an even number. If no, return an empty array
  • For sample two, check it’s first element if it’s an even number. It’s not, so skip it and check the next value. It is, so push it to empty result array. Check if there are other values, return the result array.
  • For sample three, loop through all the values and push only the even numbers to result array, return the result array

Define The  Series Of Instructions: Next, you have to write out or define every instructions before hand. Based on our example, here are the list of instructions:

  • Create a function called selectEvenNumbers that takes an array of numbers as parameter
  • Create a new empty array called evenNumbers that will hold the results of the even numbers
  • Loop through the array of number passed and set a condition to check if each number is an even number. If true, push to evenNubmbers array else skip to the next number
  • Return evenNumbers array or the empty array if none

Write Pseudo Code: Here you convert the steps above to pseudo code. Pseudo Code is an informal high-level description of the operating principle of a computer program or an algorithm. Based on the example above:

function selectEvenNumbers (valuesArray)
  evenNumbers = []
  for ind = 0 to length of valuesArray
    if(valuesArray[ind] % 2 === 0)
      push to evenNumbers array
  return evenNumbers

Programming Concepts

Translate Pseudo Code Into Code:  Using your preferred programming language, you can now code each line of the pseudo code and debug where necessary.

function selectEvenNumbers(valuesArray) {
  let evenNumbers = [];
  for (let ind = 0; ind < valuesArray.length; ind++) {
    if(valuesArray[ind] % 2 === 0) {
      evenNumbers.push(valuesArray[ind]);
    }
  }
  return evenNumbers;
}

Simplify and optimize your steps: Look for patterns and see if there is anything you can generalize. See if you can reduce any step and if you are repeating any steps. After analyzing this, you tend to get a more optimal method to solve the problem at hand. Back to our example, we can drop loop and use filters instead.

function selectEvenNumbers(valuesArray) {
  let evenNumbers = valuesArray.filter(value => value % 2 === 0);
  return evenNumbers;
}

Test: Test are necessary but not required. You can write test to validate the function above

Comment: Comments help programmers understand the intention of codes if not obvious. Check you code to know where comment is necessary and write comment. But a good program would require few or no comment.

Get feedback through code review

Practice more…

So, that’s it. Hope you enjoyed and learnt something new. Please share and like if you did. Thank you.

admin

About

Software Engineer (React, Node/Express) || Facilitator/Mentor || Open Source Lover || Future World Class Developer

View all posts by →

4 Comments on “The Logic of Programming”

  1. Thanks for a marvelous posting! I definitely enjoyed reading it, you may be a great author. I will always bookmark your blog and will eventually come back in the future. I want to encourage you to ultimately continue your great writing, have a nice afternoon! Lurleen Vaughn Raul

  2. I would like to thank you for the efforts you have put in writing this blog. I am hoping the same high-grade blog post from you in the upcoming as well. Actually your creative writing skills has inspired me to get my own site now. Really the blogging is spreading its wings fast. Your write up is a good example of it. Vannie Wayne Childs

  3. I simply could not depart your website before suggesting that I extremely loved the usual info an individual supply in your visitors? Is gonna be back frequently to check up on new posts| Cammi Gabe Gereron

Leave a Reply

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