Author Topic: Fundamentals of Programming  (Read 736 times)

0 Members and 1 Guest are viewing this topic.

Offline Rytiou

  • Noob Zombie
  • VIP
  • Knight
  • *
  • Posts: 199
  • Cookies: 45
  • EZ's Noob
    • View Profile
Fundamentals of Programming
« on: February 18, 2015, 01:46:04 am »
Introduction

Seeing how this is the "Programming newbies" section of the forum I thought it'd be a good idea to have a thread dedicated to the Fundamentals of Programming. The more experienced folk out there most definitely wouldn't benefit from this since this is aimed towards people who have never programmed before or know a little about programming but don't have a good grasp on key concepts. While I'm still in the learning process as well at the time of writing this I believe I have a good grasp on many different concepts and should be able to explain each of them in a clear and concise way. This thread should cover the majority of concepts but if I don't explain anything correctly or I miss something important please let me know and either I or one of the mods / admins will edit the thread accordingly. With that all being said, let's get started.

What is a computer program?

Before I start going over key concepts you must first understand what a computer program is. In short, a computer program is just a set of instructions. Whether it be display something on the screen or play a HD video each instruction you make does a very specific task in order to reach your end goal. Each program can consist of three things: start the program, do some work, end the program. The art of programming involves taking an idea that you have and breaking it down into individual instructions / steps. One analogy that I see people say to help them grasp this concept is this. Imagine a recipe. A recipe has a set of steps that you must follow in order to create a certain type of food. Prep ingredients, add flour, pour half a cup of vinegar, etc. This is more or less what a computer goes through to accomplish something since computers are pretty stupid. Because of this you need to be careful not to give out the wrong instructions otherwise it could lead to many bugs, cause your program to not work at all, lead to many instabilities, etc.

Statements

Statements are pretty easy to understand. Statements allow you to write the instructions in your program. You can think of statements as sentences. They use words, numbers, punctuation, etc to specify a specific instruction. Depending on the language you're using the words numbers, punctuation will be different. Some languages have you end each of your statements with a semicolon ( ; ), which acts like a period, that way the computer knows that the individual statement has ended.

Compilers

Computers only understand binary or 1's and 0's. If we had to write our programs like that nowadays it would take forever to get anything. That's where compilers come in handy. You can think of compilers as translators. They take the set of instructions you create and convert it into binary for you for the computer to understand and execute thus making your life a lot easier.

Data Types

There are many different data types that can be used throughout programming but strings, numbers, and booleans are the most frequently used at least from what I've seen. Strings are pretty easy to understand. A string is just a piece of text usually surrounded by single or double quotes (' ', " ").  Numbers have many different data types associated with them but I'm just going to discuss two of them for simplicity and time sake. Those would be integers and floats. Integers are just your standard whole numbers (10, 9001, 213) and floats are your standard decimal numbers (1.123123, 1.1, 1.345). Lastly there are booleans. Booleans can only be one of two values, True or False. Simple enough right?

Variables

Variables are one of the most important things when it comes to programming. What variables allow you to do is hold a specific value which can be used later throughout your program. You also need to assign each variable a name. For example, myVariable = someValue. Then later in your program you can do something with that stored variable whether it be display the value of the variable, assign the value of the variable to another variable (myOtherVariable = myVariable), etc.

Operators / Comparison Operators

Operators allow you to do your everyday math problems. From 2 + 2, 6 - 2, etc. Comparison operators, however, compare two or more values and sees whether it's true or false. Example: Is 5 > 6? TRUE IS 3 < 1? FALSE. Since I don't want this section to be pretty lengthy I'm going to link you to a list of the majority of operators / comparison operators here. It's python but most languages should be the same when it comes to this but if not it shouldn't be hard to figure out the different syntax and implement it.

Selection

Selection is another big programming concept. Selection allows your program to make choices based on certain conditions. These are usually handled with if, else if, or else statements. For example let's say you have a variable with a string value of "orange". You might want to have different pieces of code execute based on the value that's held within your variable. For example, (If) if the var your checking is equal to "orange" print out orange.(Else if) Else if the var your checking is equal to blue print out "blue". (Else) If neither of those conditions are true print out "Not orange or blue". This gives your program much more flexibility and gives the end user some control / freedom.

Loops

Loops are useful in many different ways since they allow to repeat a specific piece of code until a certain condition is met. There are many different kinds of loops such as a while, do while, and for loop. A while loop will continue to run until the condition you set is true (while myVar >= 10). Be careful though for if you don't provide a way to exit the loop your loop will run forever (which is known as an infinite loop). You can get around this by manually setting your condition to true or create a counter variable that increments each time the loop is ran (var++, var--, etc). Do while loops are a little bit different though in that they execute the loop first then check the condition whereas a while loop does the opposite. Lastly there is the for loop.  For loops are used for repeating a bit of code a fixed number of times. They have a built in counter variable declaration, a condition, and the incrementation of the counter variable. Example: for (var myVar = 25; var >= 0; myVar--) {} (varies syntax wise for each language which I assume you're aware of by now).
 
Arrays

Arrays come in handy in programming. Think of arrays as big lists which allow you to hold a plethora of different values in one variable. This way, you can keep large data organized and you don't have to make a new variable each time you want to store a new value plus you can easily find specific values stored. What's even neater is that the values stored in your array don't have to be the same data type. You can store stings, integers, booleans, etc all in one array.

Functions

Functions are pretty useful. Let's say you have a piece of code that can be used several times throughout your program. You could re write the same bit of code several times but that's horribly sloppy / inefficient. This is where functions come into play. You can write the piece of code you want to use once then later in your program you can call the piece of code to be executed as many times as you want too. Also with functions are things called parameters and arguements. Parameters are what's in your parenthesis and arguments are the actual statements that go within your function. Example: myFunction(myParameter) { myArgument = myParameter * 5; return myArgument;}. Keep in mind that any parameters / variables you create for any specific function will only be good with the function. Variables created in these functions are known as local variables whereas global variables are your typical variables that can be used anywhere throughout your program. It is possible to use local variables outside your function provided that you return it and assign it to a global variable (sorry if that sounds a bit confusing).

OOP

The last topic I'm going to go over is Object Oriented Programming (OOP). I'm going to go over this briefly since I'm still learning this myself so I don't want to spread misinformation but I can go over what I do understand. Before OOP you had languages that were procedural meaning the code was read top to bottom. This type of programming had many limitations as it's not as modular (subdividing a computer program into separate sub-programs). Thus this is where OOP comes into play. OOP can allow you to split apart your program into many different self contained objects (will talk about later). You can think of these objects as sort of mini programs inside of your main program. Each object contains it's own data / logic which they can then communicate between each other. There are also classes. Classes can often be confused with functions since they do similar things but allow me to break this down. Classes are a blueprint. They describe what something is but it isn't the thing itself. They can help you better organize your program and make your code more modular by allowing you to use those classes in different programs entirely. All classes make up of two things: properties and methods. Properties are the characteristics of your class whereas methods specify what your class can do. Well if the class is the blueprint then how do you use it? Well, that's where objects come into play. Just like the class is the idea the object is the thing itself (if that makes sense). You create objects based on your class(es). It's even possible to create multiple objects based on one class. That way you can reused the methods you created in your class. The last thing I'm going to discuss here is encapsulation. This is the idea that classes are self contained units that represent both the data and the code that works on that data. You take your properties / methods and bundle them up into one unit. I know that some languages go farther in OOP than others and as I'm still learning OOP I'm going to leave it at here for now.


Conclusion

Well you made it to the end or you just skipped to the end (shame on you). For anyone who actually read this entire thread you have my thanks as this took quite a while to write. As I said in the beginning let me know If I missed anything, didn't explain anything properly, or have any spelling / grammatical mistakes. Hopefully this helped some newer folk out there and have given them a head start into the wonderful world of programming.

tl;dr Basic programming concepts for programming newbies
Quote from: Evilzone IRC
<EZBot> life, you're so lame we decided to change your nick to Rytiou's bitch.
<EZBot> Rytiou is lord of the fags and will suck dicks for shells.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Fundamentals of Programming
« Reply #1 on: February 18, 2015, 01:20:17 pm »
Hi Rytiou,

 thanks for writing this overview. Let's go over it a bit.

 I would add an explanation of the term algorithm. What is the difference between a program and an algorithm?

 
Quote
You can think of compilers as translators. They take the set of instructions you create and convert it into binary for you for the computer to understand and execute thus making your life a lot easier.

 Correct so far but too narrow. Compilers are translators for one language into another language. The result is not necessarily binary. E.g. The Java compiler compiles Java code into bytecode. .NET code is compiled to CIL.

 Data types:

 You correctly explained that the computer only knows 0 and 1. So what is a datatype essentially? It tells the computer how to interpret the sequence of zeroes and ones that is data. The same sequence will have a different meaning depending if it is, e.g., a string or a number.
 The data type also determines how a value is saved. If you save the number 1 as float, it will use up different space and saved differently than saving the number 1 as integer.

 Variables:

 Variables are characterized by six things: A name, a scope, a type, a value, a lifetime and an address that refers to the stored data. Maybe add these in.

 Add the term control (flow) statement. That's every statement that controls the flow of the program. It includes loops and selections, but also subroutines and jumps.
 http://en.wikipedia.org/wiki/Loop_%28computing%29

 Arrays:

 You actually don't explain arrays. What makes an array and what makes a list?
 Note, that lists can be implemented with arrays, but don't have to. So what is really typical for an array?

 
Quote
What's even neater is that the values stored in your array don't have to be the same data type

 That is language dependent, not generally true.

 Functions:

 You explain subroutines.
 What is the difference between a function and a procedure (subroutine is the overall term that covers both)?
 Note that some languages call their subroutines always functions (e.g. C) not matter if they are actually functions.

 
Quote
  Parameters are what's in your parenthesis and arguments are the actual statements that go within your function. Example: myFunction(myParameter) { myArgument = myParameter * 5; return myArgument;}

The explanation is too vague. "What's in your parenthesis" --> arguments are usually also put in parenthesis and actually this is language dependent
Arguments are the values (in some cases expressions) that are passed to the function. Parameters are the names that the arguments get within the function.
The example is wrong for arguments.
A proper one would be:

Code: [Select]
myFunction(2, 3) // 2 and 3 are the arguments

myFunction(myParameter1, myParameter2) {
  return myParameter1 + myParameter2;
}

Quote
Variables created in these functions are known as local variables whereas global variables are your typical variables that can be used anywhere throughout your program.

The scope is language dependent. You cannot generalize it like that.

Quote
It is possible to use local variables outside your function provided that you return it and assign it to a global variable (sorry if that sounds a bit confusing).

In which case you use a different variable instead of the local one.
You do not use the local variable outside of its scope, but you use its value.

Quote
Before OOP you had languages that were procedural meaning the code was read top to bottom.

This is quirky on several levels.
Procedural code is not read top to bottom. You can have jumps or, as the name suggests, procedures, which may be defined before they are used.
Another level: OOP does not mean it is not procedural. Actually OOP languages are usually procedural too. E.g. you will find Java in this list: http://en.wikipedia.org/wiki/List_of_programming_languages_by_type#Procedural_languages
Java is just preferably called OOP, because that is the more prominent paradigm for it.
Another level: Before OOP you did not only have procedural languages as it might seem from your statement. There were declarative languages too (Prolog, LISP).

So far so good. Keep up the work. If you are interested in programming language concepts, read this book: http://www.amazon.com/Concepts-Programming-Languages-10th-Edition/dp/0131395319
« Last Edit: February 18, 2015, 01:26:15 pm by Deque »