programmingscripting


programming/scripting

most every coding language out there follows a common structure, and because of that, they say once you learn one language that you can easily pick up another with little effort (which is true). please note that html/css do not fall into this category, as these languages are for formatting and not processing.

in this section, we are not going to cover one specific programming/scripting language, but rather the terms and similarities that these coding languages follow. compared to english, this will be like learning about vowels, verbs, nouns, pronunciations, etc. these are the building blocks that make up a language, and once you understand these you should have no problem pursuing any language you desire. when choosing what language to learn, you should pick one that revolves around the technology you're interested in. for example, we'll be covering visual basic syntax in the next chapter, not because it's a good language (it's an incredibly ugly language) but because it revolves around windows and will help us accomplish the things we need accomplished.

i'd suggest you get a caffeinated beverage before reading on, as i am about to give a very straight-to-the-point overview. these explanations should be visualized as well as memorized, and you will probably get a headache. trust me when i say that it will definitely pay off in the long run. a programming language only does what it is instructed to do, nothing more, and nothing less.

a "statement" is a line of code that performs a command. statements usually have an ending marker, such as a semicolon or a new-line used to separate instructions.

a "variable" is a word or abbreviation that contains a value. it's the same as in mathematics: if 2 x y = 6 then "y" is a variable containing the value of 3. variables can have different types, the main types of which are integer (a whole number), long (a larger version of integer), float (a fractional number), double (a larger version of float), char (a character), string (a line of several characters), and boolean (true or false). some languages precede variable names with a special character, such as a dollar sign. if you want a variable to have no value, you can give it the value of "null."

coding languages that are referred to as "type-less" or "weakly-typed" imply that you do not have to specifically declare what type of value a variable contains: the engine is smart enough to figure it out on its own. you can also explicitly change the type of a variable through what is called "casting."

a "constant" is like a variable except the value cannot change. sometimes if code is dynamic, values will change along with user interaction or the like; constants can be used when you want to make sure a variable's original value is retained.

an "array" is like a variable, only holding many values instead of one (like a database). an array can even hold other arrays, creating a "multi-dimensional" array or matrix. every layer of an array has a key (or index), and a value associated with that key. the keys within an array usually start counting from 0, although the keys do not have to be numerical; if the keys of an array are not numerical then it is referred to as an "associative" array.

a "declaration" is the act of declaring something. a declaration is therefore a statement. a "conditional" is a statement that asks a question.

an "expression" is a statement that performs a calculation or results in a value. a "mixed-mode expression" is an expression performed by data that is not of the same type. for example, multiplying an integer by a double is a mixed-mode expression.

an "operator" is what is used to perform an expression. an operator can either be mathematical or comparison. following is a list of common operators; the "unary" implies that the operator works with a single argument rather than comparing multiple arguments:

mathematical:

+

addition

-

subtraction

++

increment, or plus one, unary, can be placed before (prefix) or after (postfix) a variable for different results

--

decrement, or negative one, unary, can be placed before (prefix) or after (postfix) a variable for different results

*

multiplication

/

division

%

modulus, or the remainder of a division

=, +=, -=, *=, %=

special/combination assignments


comparison:

==

equality

!

not

!=

not equal

>

greater than

<

less than

>=

greater than or equal to

<=

less than or equal to

&&

and

||

or


another operator is any symbol (such as a period or ampersand) used for "concatenation." concatenation is the act of appending one thing onto another. the @ symbol may be used in some languages before an expression to suppress errors.

"precedence" refers to an operator's importance when multiple operators are used within one expression. operators with greater importance are executed first, unless control is explicitly forced with parenthesis. for example, in: 1 * (2 + 3), the 2 + 3 will be executed first. if the parenthesis weren't there, then 1 * 2 would have been executed first, and the result of that would be incremented by 3.

"truncation" is the result of rounding off the fractional part of a number. "demotion" occurs when reducing the size of a value, while "promotion" is the exact opposite.

an "escape" character is usually a backslash. sometimes when using double quotes within double quotes, for example, the parsing engine might get confused; therefore you can escape the inside quotes by preceding them with a back-slash in order to let the engine know that they are not part of the statement. other special characters can include

\n

new line

\r

return

\t

tab

\0

null

\\

backslash


a "comment" is a way to make notes in your code, and to instruct the parsing engine to ignore these lines. there are single-line comments and multi-line comments. following are several examples of different comments.

 /* multi-line notes */ // note to self # note to self ' note to self ; note to self rem note to self 

code is usually executed from beginning to end, like reading a book. flow control can be implemented in several ways.

an if or if/else statement is known as a "branch." code usually follows some form of convention of formatting that can make it easier to read, or simply not to confuse multiple programmers working on the same project. the following format of a branch seems to be the most common in my experience:

 if (something) {     // do something }  elseif (something else) {     // do something else }  else {     // at least do something } 

a popular alternative to the if/else statement is as follows:

 if (something) ? do something : do something else; 

the switch statement is almost the same as an if/else statement, the main difference being that in a switch you are testing the expression against multiple possibilities as opposed to just one.

 switch (expression) {   case 1:     // do something     break;   case 2:     // do something     break;   default:     // do something } 

the "break" statement tells the code to exit the loop. you can usually use the break statement within any form of flow control. additionally you can use a "continue" statement within a loop (in the same way break is used) to take you back to the beginning of the loop rather than exiting.

the while, do…while, for, and foreach statements are known as "loops." a loop is a way to repeat code. often beginners get stuck in infinite loops, causing the code to crash (although in some cases infinite loops are intentional). some loops contain an initialization (a starting value), a conditional (something compared against the starting value), and an increment (raise the starting value for the next duration of the loop). here are several examples separated in their own code blocks:

 while (condition) {     // keep doing something } 

 initialization; while (condition) {     do {         // something     }     increment; } 

  do {     // something } while (condition); 

  for (initialization; conditional; increment) {     // do something } 

  foreach (array as $key=>$value) {     echo "$key : $value\n"; } 

you can put anything inside of flow control structures; you can even put loops within loops, which would then be a "nested-loop." when traversing through code such as a loop or an array, it is known as "iterating."

a "function" is a block of code that can be called at any point in the script. coding languages usually have a library of built-in functions for you to use, or you can create your own. when defining a function you must declare what "arguments" (or values) the function expects to receive, or a function can have no arguments at all. a function can return a value, or not. in nontype-less languages (where you have to declare what type everything is, not just variables) you can have multiple functions with the same name so long as they have different types, and this is referred to as "function overloading."

here are several examples of user-defined functions:

 function myfunc (arg1, arg2) {     // mess with arguments     return value; } 

notice the function returns a value, which i will be assigning to a variable. to call the above function (and assign its return value to a variable), i would type:

 $value = myfunc(onevalue, anothervalue); 

some functions have default values.

 function myfunc (arg1, arg2 = default) {     // if arg2 is not passed     // it has a default value } 

notice this function does not return a value. to call the above function i would type:

 myfunc(somevalue); 

finally, here is a more generic example:

 function say_hello ($name) {     echo "hello there, $name";     exit(); } say_hello("john"); 

the "exit" seen above simply tells the script to stop executing; you can also use "die" in some cases. defining a call to a function with a variable (so that the variable's value acts as the name of the function) is known as a "dynamic function." defining the name of a variable with another variable is a "variable-variable."

when you pass a value to a function, the function makes a copy of the variable in memory to be used within the function. in other words, manipulating a value passed to a function does not manipulate the original value that exists outside of the function. if you want a value to be permanently changed, then you could pass it to the function as a "reference" by preceding the variable name with an "&" symbol in the function's argument declaration. variables that point to other variables in this respect are known as "pointers."

"scope" more or less refers to what can be accessed and where. for example, if you define a variable outside of every function, then that variable's scope is "global" and can be accessed anywhere in the script. whereas if you create a variable within a function, then the variable's scope is "local" to that function unless you specifically declare it global or return its value. if you declare a variable to be "static" within a function, then it will retain its value for the next time the function is called; otherwise the function's variables will all be reset.

when you create a function within a function, scope also applies. the outside function could be referred to as the "parent" while the inside function is the "child" and children can inherit the traits (or values) of their parents, but not vice versa. if a function calls itself within itself, it is "recursive."

functions are executed from the inside-out. for example, in the function three(two(one())), function one is executed before function two.

using functions in a logical manner to organize your code is known as "procedural" or "event-driven" programming. another way to organize code is through oop (object oriented programming). i'm not going to say a whole lot about oop, as it doesn't really add any extra functionality to what we've already covered; it's more or less just a better way to organize extremely large projects.

while a function is a block of code, in oop, you have "classes," which are basically blocks of functions. classes are usually programmed in a hierarchical structure so that you have one main class, with many children classes spawning off of it. oop supports "encapsulation" so you can add somewhat of a permissions system to your code, allowing you to say what functions can access these other functions, etc.

think of an "object" as a girl, so you create a class for girl. within this object, you can define "methods" and "properties." properties are things about the girl (variables) such as appearance, while methods are what the girl can do (functions) such as eat, sleep, etc. now think of looking at yourself in a mirror, with another mirror behind you so you see an endless number of reflections. the items within an object can be objects themselves, then you've got child classes, and it soon becomes a nauseating cycle that makes you wanna vomit to even think about, but it sure is fun!

now what part of e/@c= 3 ? int($v) : -i; don't you understand!? hopefully all that sunk in… you should now have at least a vague idea of how programming languages are organized, and with this knowledge, all you need to start coding is a specific language's rules/syntax. standards exist for programming languages (proper ways to write it) to make it easier for people to share and read each other's code. standards aren't required, but certainly shouldn't be ignored without a valid reason. back to some less complicated stuff for a bit.




Tapeworm - 1337 Hax or Handbook
Tapeworm - 1337 Hax or Handbook
ISBN: N/A
EAN: N/A
Year: 2005
Pages: 74

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net