Sunteți pe pagina 1din 18

Slide 1

Programming
Fundamentals
Michiel van der Blonk

© Michiel van der Blonk


http://www.vanderblonk.com

Welcome to programming fundamentals. I am Michiel van der Blonk, and if you’re looking for a
place to start learning how to program, here’s the right place.
Slide 2

Contents
 Scripting

 Principles& Structures
 Types and variabeles

 Constants

 Functions

 Objects

© Michiel van der Blonk


http://www.vanderblonk.com

In this course we will cover the following topics:


Scripting, in which I will explain what scripting actually is. So if you want to become a script
kiddie, you can! Unless your not a kid.
Principles and structures, which will show you what almost any programming language can do
Types and variables, in which you will learn how a computer memorizes stuff
Constants, to hold things that are, well.. Constant
Functions, where you will see how to make the computer do lots of stuff for you
Objects, the LEGO building blocks of modern programs
Slide 3

Scripting
 What is a script language?
 What is compiling?

Sub x
11010101010101000
Do
01011010010010101

Loop
Compilatie 10010101010010101
01111100101010100
If
10101010010101010
..
10111010010101001
End If
01010101001010111
End Sub

© Michiel van der Blonk


http://www.vanderblonk.com

A script language is anything that doesn’t need to be compiled. Now compiling is a process that
makes a program into an executable, also known as .EXE. If you look on your hard disk you will
find a lot of .EXE files. All of those are compiled. They only run a specific type of computer, so if
you have a Mac (lucky you), then it won’t run on a PC. Only after compiling can you run the
program. A compiled language is e.g. C, or PASCAL.
A script language doesn’t compile, but performs things right away. It can run on any type of
computer. Examples of script languages are PHP, JavaScript, and VBScript. You write it once,
you can run it anywhere.
Slide 4

Differences
Compiler Interpreter (script)
 Fast  Slow

 Rigid  Flexible

 Strict typing  Loose typing

 Early Binding  Late binding

 Platform dependent  Platform independent

 Independent .EXE  Runtime Host (VM)

© Michiel van der Blonk


http://www.vanderblonk.com

Let’s have a look at the differences between compilers and interpreters.


Compilers are not fast, but they deliver fast code. The code is optimised for the processor, and
therefor runs a lot swifter than any interpreted script. Nowadays though, computers have such
speed that scripting is not necessarily _slow_.
Compiled code is rigid. It means that once compiled, you cannot change it during what we call
runtime (when the program is actually running). This may seem like a logical thing, but script
_can_ actually be changed during runtime, sometimes leading to very strange effects. It’s also
one of the most powerful aspects of script.
Strict typing means that the space a value holds in the program has to be fixed. Compare this to a
form that has a fixed number of spaces for your name, and one that just says: enter your name,
and leaves it up to you how you fill it in. Again this can be difficult but also powerful. On top of it,
the loose typing indicates that the values can change type anytime, so for instance a number can
be changed to a text. In compiled program trying to change a value to another type always results
in an error.
Binding is what we call attaching a type to a value, but for compound values called objects. I will
discuss objects later. This is similar to loose typing.
The platform is what we name the operating system, so e.g. Windows XP. Now the compiled
program can only run on one platform, which is the reason you see e.g. Photoshop for Mac and
Photoshop for PC. On the web this wouldn’t make any sense, and that’s why on webpages we
can only use interpreted script languages, or be tied to one browser. ActiveX is one such
exception, where using IE we can run compiled elements (ActiveX) in the browser.
The compiled code’s host or environment is the operating system itself. The script’s host is
always another program, called the runtime engine, or Virtual Machine. The runtime engine itself
is a compiled program, and is able to run the script.
Slide 5

Local Script TAG


 <script>

 E.g. type=“text/javascript”
 functions in <head> or

 In separate document

© Michiel van der Blonk


http://www.vanderblonk.com

The script tag is a browser standard tag that can ‘hook into’ runtime engines (VM’s). You can use
it to embed script into your browser. However the script tag is also used to indicate server code,
like PHP, that is actually executed before the page is sent to the browser.
In the script tag you can add the language to be used, which by default in all browsers is
JavaScript, although IE is supposedly using their variant Jscript, which is compatible with the
standard.
You can always place the code in the head of the document to make sure that it is loaded after
any other elements of the page. You can also put the code in another document and link to it from
the head tag. This is the preferred way.
E.g. (don’t worry if this looks like Chinese)
<script type=“text/javascript”>
function add(x,y)
{
return x+y;
}
</script>
Slide 6

The JavaScript language


 Similar syntax to
 C
 Java
 PHP
 Case sensitive

© Michiel van der Blonk


http://www.vanderblonk.com

In this course we will use JavaScript merely as a way of showing what can be done using
programming techniques. Most techniques can be used in other languages, although they will
require a different syntax. JavaScript style is based on the very popular languages C. C is
however a compiled language. Java and PHP lean on the same syntax style, and a lot of
functions have the same name.
One aspect these languages have in common is case sensitivity. This is tough to deal with for
beginning programmers. It means when you say ‘harry’ in lower case it means something
different from ‘Harry’ in upper case, although Harry himself might not agree.
Slide 7

Basis principles
 Algorithms
 Loop
 Choice
 Statement
 Compound statement
 Variables

 Routines

© Michiel van der Blonk


http://www.vanderblonk.com

The basic principles of popular languages are all the same. The so-called procedural languages
all have three concepts in common:
They can repeat pieces of code
They can make choices in code
They can execute single lines of code
A compound statement is sometimes called a block, and can make organizing code easy.
Variables are places to put the data of a program in. It is a way of memorizing stuff for later use.
Routines, sometimes referred to as a “function” can be executed multiple times, making it easy to
repeat actions. We will dive into this more later.
Slide 8

Algorithm

Input

Statement
Output Choice
Loop

© Michiel van der Blonk


http://www.vanderblonk.com

Let’s have a look at what we discussed as the three basic principles of programming: the
statement, the loop and the choice. It is proven mathematically that using these three principles
any program that has a limited number of steps can be written. The statement could be
something like “print Harry is a wanker”, which would output that to the screen. Writing foreach
(1..10) print Harry is a wanker would show the same thing 10 times. And writing if
(harryIsAWanker) print yes he is a wanker, would only show it if it’s really true. And consider
changing the word ‘wanker’ to something else, like ‘a cool guy’ and you’d have a completely
different program.
Slide 9

Structures
 for, foreach (LOOP)
 while (LOOP)
 If (CHOICE)

 Switch (CHOICE)

© Michiel van der Blonk


http://www.vanderblonk.com

In most languages the following loop and choice structures are available, although the names can
be different:
For: this makes a loop for a certain amount of times. So e.g., ‘wash my car 3 times’, even though
that might not make any sense.
Foreach: this makes a loop for a number of values. So e.g. foreach car in this street, wash it 1
time. That makes more sense, but it’s a hell of a lot of work.
While: while my wife is shopping, I can play videogames. That’s not only true, it’s also fun. The
while is not really the same as in spoken language, because we can also say “while that is true,
Harry is still a wanker” and then it’s something different. So the while is really something in the
sense of ‘during’.
If: if my wife is shopping, I lose money. True, and less fun. The if is used just as in normal
language.
Switch: although the switch does what it says (it switches between choices), it is usually not used
like that. It’s more of a elaborate IF. So e.g. if I eat fries, I take mayonaise, if I eat hot dogs, I take
ketchup, etc.
Slide 10

Types
 One value
 Number
 String
 Boolean
 Date
 Multiple values
 Object
 Array

© Michiel van der Blonk


http://www.vanderblonk.com

Types describe basically how much room a value will take. It’s like where you live. You live in a
condo, an appartement, a house, a villa, a trailer, all of those. They define the size and
characteristics of your place. In programming languages we have
Number: this can be anything numeric, including 1.5, 3, 6 million. There’s not really a distinction
between money and weight, it’s all just a number.
String: this is the place for anything text-like. It can be as simple as the letter H, the word Harry,
the text “Harry is a wanker” or the entire text of wikipedia.
Boolean: this one is simple, it is either true or false, and some languages implement it as 1 and 0.
Date: this is a date value, so Jan 1, 2006. It has to point to a specific date. The date is mostly
stored as a number, and sometimes the value can also indicate a time by using decimals.
Then there are spaces that can hold multiple values. In the analogy of houses we have
skyscrapers and appartment buildings. In programming we have Object and Array
An Object is a space that can actually hold entire programs. It can hold values and their functions.
A value inside an object can be of any type, so an object can actually hold other objects.
An Array is more like a street. It is just a collection of values, usually of the same type. In scripting
languages all the values can be of a different type.
Slide 11

Variables
 PHP: $x=1;
 JavaScript: x=1;
 Basic:x=1
 Special values
 Null
 Empty

© Michiel van der Blonk


http://www.vanderblonk.com

So now that we have looked at the places where values live, let’s look at the values themselves.
We call them variables. They have a name, and a type. In programming languages the way to
give a variable a value differs a lot. Look at the various language examples here. The most
important thing to know is that a variable will hold it’s name and value within a code block, aka
scope or namespace. So just like in one house you can have a guy named Harry there can be
another Harry next door. Depending on where you are you mean the one or the other. A simple
rule is of course, never bring them together or make sure they have a unique name (like including
Harry’s last name)
Slide 12

Constants and operators


 Define(‘PI’, 3.14);
 Operators
 Math: +, -, *, /
 Comparison: ==, !=, <, >
 Special: typeof

© Michiel van der Blonk


http://www.vanderblonk.com

Constants are what they say, they are constant. They should not be used for anything that ever
changes. A lot of scripting languages just don’t have this concept, since they realize that during
scripting, anything could change.
Operators are the Math things of programming. They can calculate, and compare, and stuff like
that. It helps in making choices in the program.
There are also special operators that could have also been made as functions, like the typeof.
There is no real reason for it to be an operator.
Slide 13

Function examples
 Array: count, current, map
 Conversion

 Date and time


 Format and rounding

 Math: sin, cos, pow, random

 String: str_replace

 Logic: isempty

© Michiel van der Blonk


http://www.vanderblonk.com

Functions are the important LEGO blocks in a language that help you build programs quickly. If
we wouldn’t have predefined functions we’d have to write everything ourselves. And that sucks,
believe me, I know. I’ve programmed in the eighties. Anyway, most popular languages come with
such huge function packs (named libraries) that it’s incredibly hard to even know of all the
functions in the language. An exception is JavaScript actually. Javascript’s function set is quite
acceptable and easy to learn. On the other hand, after a while you start looking for functions and
realize there is none, and you have to write it yourself.
Let’s have a look at the different categories of functions we can use in most languages:
Array functions: these are handy to get information out of arrays, like get the number of items,
and to do stuff to all elements in an array. A function like map does stuff to all elements, and you
just define what.
Conversion functions: these are still important, because even though technically a value doesn’t
have a type, it’s still handled in a certain way. E.g. 12+5 could become 17, but it could also
become 125 (as a string).
Date and time: languages that have a Date and Time type, also usually provide lots of functions
that deal with them. This is because 24 hours + 1 is not 25 hours, but 1 day, 1 hour. Dates are
extremely difficult to calculate with, so if you can prevent writing it, do so.
Format: that’s just to pretty print any stuff.
Rounding: useful for any calculations, especially with currency
Math: speaks for itself, go look on your calculator if you forgot what all those mean. One very
typical function in programming is the random function. This usually gives you a random number
between zero and one, which you can then multiply to get any sort of random number. This way
you can make a program behave more unpredictable, especially handy in games.
String functions help you deal with text, which is almost always some form of input and output of
your program. If someone enters their name in lowercase, you might want to have it in
uppercase. Replacing strings is a very handy tool for templating, which you do when you have
files with ‘fill in the blanks’ places.
Logic functions help make choices in IF, SWITCH and WHILE. This special one looks at if a
variable actually has a value.
Slide 14

Your own functions


 Calculate the area of a land property you
own:

function Area(width, height)


result = width * height
return result

myarea = Area(10,25)
otherArea = Area(40,20)
© Michiel van der Blonk
http://www.vanderblonk.com

Writing your own functions is the next step. By collecting these you can create your own libraries
or components. Look at this short function sample. It has inputs (width and height) and it has an
output, result. The result is calculated from the input. Below you see how easy it is to put two
different Area calculation in variables. This sample shows you the principe of DRY in its most
basic form. DRY means don’t repeat yourself. I said: ‘DRY means, don’t repeat yourself.’ In this
case by specifying the Area function I prevented myself from having to write the code twice.
See: http://www.pragmaticprogrammer.com/ppbook/extracts/rule_list.html
Slide 15

Objects
 Created with templates (classes)
 Include functions and data
 PEAR library

 Roll your own


 Class Rectangle
 Width
 Height
 Function: Area()

© Michiel van der Blonk


http://www.vanderblonk.com

Objects are another one of those great principles that most modern languages have
implemented. Using objects you can separate code a lot better than with functions. Because what
would you do if you had written over 100 functions, and names start to look the same. You will
have to make names longer or prefix them with something else, it all becomes tedious. With
objects you can let one object handle something and another object another thing and they could
both have 100 functions, all with the same name but a different meaning. In daily life we see a lot
of objects, take e.g. the mobile phone. It cannot only hold data, it can also do stuff. It’s like a
variable on steroids. The phone can call, forward, take a message. But so can my answering
machine (well, not call, ok). So by splitting the program into phone and answering machine, I can
have them do their own thing, and the program is easier to understand.
In modern languages we usually also have libraries of objects (actually classes, the definition of
an object), just like we have libraries of functions.
Of course we can also roll our own. See the example where the Rectangle object now holds it’s
width and height as a variable, and the Area function as well. The function inside an object is
named a method, but that’s just a name.
Slide 16

Databases
 Use DB-libraries
 Functions
 E.g. mysql_connect(db,user,pw)
 Classes
 E.g. DB::connect

© Michiel van der Blonk


http://www.vanderblonk.com

Databases are so important almost every language will give you a way of interacting with them. In
PHP e.g. we can call mysql_connect to make the database know we need access (logging in).
After that we can get data from the database and use it in our program. Using class libraries this
again becomes much easier.
Slide 17

Resources
 Programming

 JavaScript
 Tutorial
 Function reference
 PHP.net
 Tutorial
 Function reference

© Michiel van der Blonk


http://www.vanderblonk.com

There are a lot more resources to look at, here I am going to give you the basic reference stuff
http://www.devguru.com – this site is an excellent reference for all script programming stuff. They
give references in a very technical manner, but they are very complete
http://www.w3schools.com – this site is more for learning, but also an excellent reference. It’s
easier to look up, but now always complete.
http://msdn.microsoft.com/scripting - you don’t always expect it from Microsoft, but this reference
is excellent. Check out the tutorials for some good stuff.
http://www.php.net – this is the best reference you can find for PHP. The real pearls are in the
user comments on each function in the help section.
Slide 18

Summary
 Scripting

 Principles

 Structures

 Types and variables


 Constants

 Functions

 Objects

© Michiel van der Blonk


http://www.vanderblonk.com

Well that concludes this short course. I would like to thank you for joining me, and I hope you will
write a bunch of awesome programs, and if you do let me know (at blonkm@gmail.com). At least,
have fun while doing it.

S-ar putea să vă placă și