29 terms

Programming Basics

STUDY
PLAY
What is programming in the most basic form?
Programming in the most basic form is issuing commands and seeing that they get executed. It is a mixture of math and linguistics: you define a lot of calculations and you need to tell machines to solve them by giving orders using the right grammar. Grammar in programming is syntax and differs a lot from language to language.
Interpreted programming
Programming languages are interpreted to be turned into actions or results. Some languages, such as JavaScript, are interpreted by a web browser; all you need to do to make them "do stuff" is put them inside an HTML document and open this in a browser.
Compiled programming
A compiled language is a programming language whose implementations are typically compilers (translators that generate machine code from source code), and not interpreters (step-by-step executors of source code, where no pre-runtime translation takes place).
Source Code
Source code is any collection of computer instructions, (possibly with comments), written using a human-readable computer language, usually as ordinary text. The source code of a program is specially designed to facilitate the work of computer programmers, who specify the actions to be performed by a computer mostly by writing source code.
Machine Code
Machine code or machine language is a set of instructions executed directly by a computer's central processing unit (CPU).
variables
In programming, variables are containers for values that can vary. Variables can hold all kinds of values and also the results of calculations. Variables have a name and a value separated by an equals sign (=). Variable names can be any letter or word, but bear in mind that there are restrictions from language to language of what you can use, as some words are reserved for other functionality.
What determines the end of a variable?
a semicolon (;)
Explain this variable in english:
var x = 5,
y = 6,
result = x + y;
- Define a new variable (that is the var keyword) called x and assign it the value 5.
- Define a new variable called y and assign it the value 6.
- Define a new variable called result and assign it the result of adding x and y as its value. End of statement. (As there is a calculation in the assignment of the variable result the interpreter then checks the value of x, checks the value of y, adds the two and sets the value of result to the outcome — 11.)
What are the variable types?
- Float: a number, like 1.21323, 4, 100004 or 0.123
- Integer: a natural number like 1, 12, 33, 140 but not 1.233
- String: a line of text like "boat", "elephant" or "damn, you are tall!"
- Boolean: either true or false, but nothing else
- Array: a collection of values like 1,2,3,4,'I am bored now'
- Objects: a representation of an object.
What is an object?
Objects are constructs that try to model instances of objects in the real world by having properties and methods. For example, you could model a cat as an object and define that it has four legs, one tail, and that it is ginger. You can also define that it has a way of purring by defining a purr() method and that it might demand a cheeseburger with a getCheeseBurger() method. You can also re-use the cat object and define another cat with all the properties of the original one but a gray color.
What does it mean when we say JS is a "loosely typed" language?
JavaScript is a "loosely typed" language, which means that you do not have to explicitly declare what type of data the variables are. You just need to use the var keyword to indicate that you are declaring a variable, and the browser will work out what data type you are using from the context and use of quotes.
What is an example of Floats and Integers?
There is nothing magical or strange going on with these. You define variables and set their values to any number type. Floats and integers can be modified with any mathematical operators.

var fahrenheit = 123,
celsius = (fahrenheit - 32) * 5/9,
clue = 0.123123;
What is an example of Booleans?
Booleans are simple "yes or no" definitions. You assign them by using the true or false keywords.

var doorClosed = true,
catCanLeave = false;
What is an example of a String?
Strings are lines of text that can contain any character. You define them in JavaScript by enclosing the text in single quotes or double quotes.

var surname = 'Heilmann',
name = "Christian",
age = '33',
hair = 'Flickr famous';
How do you concatenate (a technical term that means "join together") strings?
You can concatenate (a technical term that means "join together") strings using the + operator but you cannot subtract strings from one another. For string modification, you need to use functions the language provides you with. Simple concatenation, on the other hand, is as easy as this:

var surname = 'Heilmann',
name = 'Christian',
age = '33',
hair = 'Flickr famous',
message = 'Hi, I am ' + name + ' ' + surname + '. ';

message = message + 'I am ' + age + ' years old and my hair is ' + hair;
alert(message);
What is an example of an Array?
An array is a collection of values, and each of the values can be a variable or a real value. For example:

var pets = new Array('Boomer','Polly','Mr.Frisky');

Arrays are great for describing collections of things that have something in common, and every language comes with dozens of handy functions to manipulate them—sorting, filtering, searching and so on.
What is the array counter?
You can access each of the values with the array counter, which is the index number in the array -— think of it as being like looking up chapters in a book. So for example pets[1] would give you the string "Polly". But wait! I hear you ask—shouldn't it be pets[2] for Polly, given that it is the second value in the array? No—the counter is not 2, as computers start counting at 0, not at 1! This is a very common cause of confusion and errors.

The syntax is arrayname[index].
What is array length?
Arrays automatically get a special information source for you: length. If you check the value of pets.length you will get 3 as there are 3 items in this array.
What is an an object?
If you have a collection of items that need more detailed descriptions than just a running number, then an Array will not quite give what you need, and you will need to create an object. Objects are constructs in programming that represent or model real things, such as people, vehicles or tools. Let's just say that an object is a thing that has several properties.
What is an example of an object?
Say for example you have a person object; you can define the different properties by appending them with a dot:

var person = new Object();
person.name = 'Chris';
person.surname = 'Heilmann';
person.age = 33;
person.hair = 'Flickr famous';
How do you access the properties of an object?
You can access the properties with dot notation (person.age would give you 33) or with the square bracket notation (person['name'] gets you "Chris").
What is a condition?
A condition is a test for something. First of all, conditions can be used to ensure that your program works, regardless of what data you throw at it for processing. The other thing conditions can do for you is allow for branching. The two most common conditions are if statements and else statements.
What is branching?
You might have encountered branching diagrams before, for example when filling out a form. Basically, this refers to executing different "branches" (parts) of code, depending on if the condition is met or not.
What is an example of an if statement?
The easiest condition is an if statement and its syntax is if (condition) { do this ... }. The condition has to be true for the code inside the curly braces to be executed. You can for example test a string and set the value of another string dependent on its value:

var country = 'France',
weather,
food,
currency,
message;

if (country === 'England') {
weather = 'horrible';
food = 'filling';
currency = 'pound sterling';
}

if (country === 'France') {
weather = 'nice';
food = 'stunning, but hardly ever vegetarian';
currency = 'funny, small and colourful';
}

if (country === 'Germany') {
weather = 'average';
food = 'wurst thing ever';
currency = 'funny, small and colourful';
}

message = 'this is ' + country + ', the weather is ' + weather + ', the food is ' + food + ' and the ' + 'currency is ' + currency;
alert(message);
How do you concatenate a condition with "and"?
"and" is written as &&_ Say you want to test if the value of x is between 10 and 20—you could do that with a condition stating if(x > 10 && x < 20)_
How do you concatenate a condition with "or"?
If you want to make sure that country is either "England" or "Germany" you use:
if(country== 'England' | country== 'Germany').
What is an else statement?
There is also an else clause that will be applied every time the first condition isn't true. This is very powerful if you want to react to any value, but single out one in particular for special treatment:

var umbrellaMandatory;
if(country == 'England'){
umbrellaMandatory = true;
} else {
umbrellaMandatory = false;
}
What is a loop?
Loops are repetitive conditions where one variable in the loop changes. The easiest form of a loop is the for statement. This one has a syntax that is similar to an if statement, but with more options:

for(condition;end condition;change){
// do it, do it now
}
What is an example of a loop?
Normally what you do with a for loop is to execute the code in the curly braces several times. For this you need to define an iterator variable and keep changing it during the loop until the variable value meets the end condition (which causes the interpreter to exit the loop and carry on to the next part of the code). For example:

for(var i = 0;i &lt; 11;i = i + 1){
// do it, do it now
}
YOU MIGHT ALSO LIKE...