Introduction to Programming
When I started learning to program, I didn’t know anything like the basics. I just knew the basics of A+ and Network+. But C, Java, and Python were different worlds for me. Besides, there was hardly anyone to help me since I was the only one among my friends from an IT background. So, I finally decided I would start learning from the most common language out there. I then came to know about C and C++. I started learning C, but I was head over heels since importing modules, and all those stuff usually went bouncer for me. I learned C for a month; then, I thought C was a bit tough and thus started learning Bash. Though Bash is a scripting language and not a programming language, it was a bit difficult to understand. I got stuck at cron jobs and stuff. Finally, with nowhere to go, I started learning Python. But heck no…It was the worst. When I used to write codes in C, I had to write every piece of code. But in Python, a lot of magic was going around to understand what was happening. And yup, this magic is different from what you think, and ‘magic’ is the official word in Python. Magic in Python means that you want to do something, and that thing happens, but you don’t know how it happened. Yeah, and that’s where Python tends to get complicated.
Fundamentals of Programming
When I started to learn all these programming languages, the most important thing I could not understand was why I should import any specific module. For example, when writing a hello world program in C, we usually write ‘include stdio. h’ or ‘include conio. h’. So, my question was, why only this? I saw some people not even typing in the conio part.
Similarly, in Python, to do complex math, we import cmath, but why don’t developers already combine math and cmath and shorten the whole process of importing it twice? But then I thought, let’s do one thing. Let’s start with the very basics of programming. I decided to hit rock bottom and start back from there. And as of now, I can write any program in almost any language, such as Java, C, C++, Haskell, Scala, Python, Ruby, and many more. Over the years, I have built a rock-solid foundation in programming. And the main reason for this because I cleared all of my fundamentals in programming. You may hardly hear anyone talking about the fundamentals, but they are the most important to get your concepts right in programming, and that is the main reason I thought of writing this blog. So let’s get started with the fundamentals and terminologies in programming.
Pseudo codes, Mathematics, and Comments
If you know math, then most things will come by easily. Writing a program is not much different from solving a math problem. Besides, when learning functional languages like Haskell, math is the only thing that can be a prerequisite for learning that language. Most problems can be solved by simple mathematics and pseudo codes. Math and pseudo codes go hand in hand. For example, whenever you want to solve a specific problem, write it down in simple algebraic and geometrical format in the form of theorems and hence proved formats. Now write down these pieces of code in pseudo-code format. When I say pseudo codes, I mean writing the program in such a way that when you write an actual program, you would only need to change some values and vocabulary, and the program would work. For example say, to calculate the sum of all the numbers from 1 till it reaches 20, one can write a pseudo-code in the following manner:-
|let x = 1x = x + 1
if x = 20 then stop and print x
else continue and repeat everything again
This code is a bit buggy, but we are not here for that. As you can see, I first assigned the value of x to 1 and then added 1, 2, and 3 till it reached 20. If x reaches 20, the program will stop and print the output; it will continue and keep repeating the same thing. So, converting this pseudo-code into an actual program becomes extremely easy when you write it. And also, make sure you write comments next to all those lines which you think are confusing. The main reason for writing comments is because it doesn’t get executed first; second, you can always point out what you did, where you did it, and why you did something. Comments are not of much use in 5-10 lines of code, but when you have multiple files with around 40-50 lines of code in each file, it becomes easy to find where the bug lies or why some files were imported by just looking at the comments.
Variables, constants, and Data types
A variable can be used to store data, strings, numbers, or almost anything. A collection of such data is known as an array. If I refer to the previous example, you can see that I have assigned the value of one to X at the start. Thus it makes X a variable. Here the one is an integer, and X is the assigned variable. Similarly, I can input string into a variable as well. A simple example would be:-
|X = ‘Hello world’echo $X|
The above is an actual program that can be executed in bash, an awesome Linux terminal. X is assigned the value of the string ‘hello world and echo prints whatever is inside of X. The dollar sign represents that we are calling the value of X here.
Similarly, constants are also variables,, but unlike variables, constants cannot be changed. In the first example, I assigned the value of one to X; then, I kept increasing it in ascending order. But for a constant, X will always be one,, and the value cannot be changed until the program is quit. Data types are just bifurcation of different types of data. For example, real numbers, integers, floats,, or even Boolean and strings. Boolean refers to True and False and is often represented by 1 and 0.
Functions and Procedures
In programming, functions are just assigned values to large pieces of code. They are usually made that way so that a person won’t have to write the whole code down repeatedly and can call the whole code with just one simple syntax. A Function is just an encapsulated task that contains multiple lines of instructions to be executed. When writing large code, functions are usually pre-developed and stored in separate files inside a folder. Later whenever that piece of code is required, one can call for the file name or the function name defined,, and the whole piece of code within the file will get executed.
Functions have their workspace, which means that all variables inside of a function are usable only until the execution of the code gets completed. Once it gets completed, the variable gets undefined (except in the case of garbage collection, which I have not mentioned here since that is a huge topic to discuss). And functions can also be nested within a function. This means that one function can call any other function, including itself. But the latter is not recommended since that will result in a boot loop until otherwise executed acutely.
Procedures, on the other hand, are almost similar to Functions except that functions always return a value, whereas a procedure is just the execution of commands. When starting to learn to program, you may see many people use the terms functions and procedures interchangeably. But this isn’t the case if you start learning functional languages like Haskell or Scala. So when writing functions, one thing to keep in mind is to ensure they don’t have side effects.
Conditions and Loops
Conditions are something I have explained previously in the example. They go by the manner of something like this ‘If it rains, I will be wet. Else I will not be. Yeah, that sounds like a dumb example, but that is the easier it can get. Conditional statements depend on each other. Usually, they are interconnected and go in the form of “if, then, else, and elif.” Even conditional statements can be nested as well. Conditions within conditions are very common these days, but if indentations and comments are not made properly, the program will become extremely buggy and hard to understand.
On the other hand, Loops are used to repeat the the execution of codes, functions, and procedures till the desired result is returned. If you check my first example, you will see that I have stated something to repeat everything. Thus, loops are very powerful,, and they make programs extremely compact. But too much use of loops will make the program slow. They should be used only when necessary. Loops go in the form of “for, While, Do-While loop and for-each loop.” The most commonly used loops are the while, do-while,, and the for a loop. The pseudo codes for the while, do-while,, and for loop would go in the following manner:-
|While condition is false,
execute the code and check whether the condition is true
Stop when condition becomes true.
|Do – While Loop:-|
execute a code
} while (check whether a statement is true, else repeat the Do)
|for ( a, b, c)
|In the above code, a is the condition that gets executed once and first, b is the condition of the loop, and c is the code that gets executed once the loop stops.|
Control Structures in Fundamentals of Programming
Control Structures in fundamentals of programming are just a combination of conditions, loops, and other sets of code. Control structure is a block of codes, which analyzes the whole structure of the program, and then decides which to go further, as in whether to stop, repeat, call for a function, or execute the other block of codes. In short, to be more specific, Control structures in programming are just a decision-making process that decides a program’s flow. One full piece of code performs in the following block type of manner:-
|Pre ConditionControl Structure
End of Control Structure
Object Oriented Programming and Functional Programming
Now, this is one of the most debated topics by developers in fundamentals of programming. Object-oriented programming deals with just ‘objects.’ Don’t take that statement too literally. Object-oriented programming or OOP deals with data that contain fields, attributes, procedures, and methods. C is an object-oriented language. Python is not purely object-based. But there have been books where some say that Python can work in an object-oriented way. Similar to object-oriented programming, there is also something known as Functional programming languages. Functional languages are more math based. A purely functional language I can remember is Haskell. Another one is Scala, which is not fully functional but can be made to work that way. On the other hand, Haskell doesn’t tend to work like other languages and is based on pure logic. Having a bug in Haskell is very less since there are less or atleast no side-effects of functions in Haskell, unlike Java, C, Python, or most other languages.
Conclusion – Fundamentals of Programming
All in all, fundamentals of programming are very easy to learn. One needs to input proper logic into a problem and find multiple ways of solving a problem rather than just one to sort a problem. Once you have the basics right, developing softwares or learning frameworks based on them would be a piece of cake.
This is a guide to Fundamentals of Programming. Here we discuss the introduction, functions and procedures along with while conditions and loops. You may also have a look at the following articles to learn more –