Questions about this topic? Sign up to ask in the talk tab.

Difference between revisions of "User:Hatter/programming principles"

From NetSec
Jump to: navigation, search
(Loops)
(Comparative Operators: lol)
Line 64: Line 64:
 
|-
 
|-
 
|Less than or equal to
 
|Less than or equal to
|lt
+
|le
 
|<=
 
|<=
 
|}</center>
 
|}</center>

Revision as of 09:30, 11 July 2012

Variables

Variables, as the name implies, are dynamic objects in programming which change based on their context. That is for instance $variable can always be recalled with $variable but what you recall may change. There are generally three types of variables: Scalars (strings), Arrays (or lists), or Associative Arrays (or hashes).

Scalars

A scalar is the simplest form of a variable, as it is a single object. You may recall a person's name or a single number. These are typically used for basic things like running the same equation (Think X + 10 =) when you'll need changing data to test.

Arrays

An array is a bit more complex than a Scalar. Arrays are formed from multiple strings and can contain Scalars or predefined, static strings. You may have a variable for a classroom, which inside lists all the names rather than one.

Associative Arrays

An associative array is similar to an Array, but with two fields that compare each other instead of individual strings. For instance you might have an array that compares the classes first names to last so that at any point if you know the person's last name, you can find their first

Combining Variables

The true benefit of variables comes when you combine them. You may have a scalar called number to recall an element in array called months to pull a month's name by its chronological number, or use a scalar called last_name that refers to your hash called full_names so that every time you recall last_name it associates the first.

Validity

Validity is a simple concept. If a condition is true, the conditional or loop will act accordingly. Likewise if it's false, that is a 0, empty string, or incorrect comparative operator, it will treat the loop or conditional in the opposite manner.

Comparative Operators

Comparative operators are used when you want to return a true value by comparing two other values. You might have a 7 and a 5. Since they're not empty strings, (7) would always be true. (5) would likewise be true, but is 7 less than 5? Common comparative operators are and, or, equal to, not equal to, greater than, greater than or equal to, less than, and less than or equal to. They return true when the mathematical value is satisfied.

Comparative Operators and Symbols
Literal Shorthand Symbolic
And and &&
Or or ||
Equal to eq ==
Not Equal to ne !=
Greater than gt >
Greater than or equal to ge >=
Less than lt <
Less than or equal to le <=

Conditionals

Typically found as if, unless, else, or else if, conditionals take action according to the validity of a condition. That means they either do an action contained in the block of code, or skip past the block altogether depending on the conditional used.

If

The most common conditional is if. If your condition returns true, for instance, if (5 < 7) or if (5 != 7), it proceeds with the block and performs actions. If it returns false, it ignores the block altogether and goes to the next.

Unless

Unless is the exact opposite of if. It only continues with the block if the condition isn't met and returns a false value. You can say unless a variable is less than a limit (maybe unless ($variable < $limit)) allow it to do anything in the block. This conditional can be reworded into an if, and you should use what feels more natural to you ("if exists $variable" is the same as "unless not exists $variable")

Else

Else is plain conditional in that it doesn't test anything except the previous conditional. For instance you could have a statement that reads unless this variable is less than a limit, allow it to do this else print "You're not allowed!" If the previous condition is not satisfied, it will always run. If it was, it will never run.

Else if

Else if, or more commonly elseif is exactly what it sounds like: A combination of if and else. It tests the previous condition and then tests a new condition if the previous one was false. A good example is Goldilocks and the Three Bears is if $temperature is greater than 10, print "This porridge is too hot" elseif $temperature is less than 10 print "This porridge is too cold" else print "This porridge is just right!" Only if the first condition isn't already met, it moves on to test the next one. If the next one isn't met, either, the else at the end assumes itself true.

Loops

Loops are very similar to conditionals in that they test a condition in order to function, but unlike conditionals instead of continuing with the program, they go back to the start of the block they're in until that condition is no longer true. A loop that is always true, either intentionally or not, is called an infinite loop and will run until the program is stopped, or the loop is escaped. This means to avoid infinite loops, you must change the condition in some way. Generally loops test against a variable that are changed inside the loop. $variable++ (The common form for incremented, or +1) would be taking a variable and adding one, so if you have a loop that tests your variable to be less than a number (say 10), adding $variable++ would repeat until it's larger (say 10 times from $variable equaling 0) Common loops are while and until.

While

While is very similar to if, generally both in syntax and function. If a condition returns true, it runs the block, and then checks again to see if the block is still true. If it is, it runs it again. This means if you don't do something to change the condition somewhere in the block, it will always be true and run infinitely. 1, since it is not an empty string, false comparative operator, or 0 would always test as true, so while (1) will perform the same action or set of actions over and over.

Until

Until is while's polar opposite. It tests for a false value, and runs the loop until the value tests positive. Taking the Goldilocks example again, you could say unless a random number between 1 and 20 is 10, print "I'm not eating that!" This would make your program keep testing random numbers until a 10 is reached, at which point you could program what would happen with the best porridge.

Control Flow

Functions

Data Structures

Objects