Blog
  • Albert Banks
  • Alex Runde
  • Brett McCoy
  • Caleb Loffer
  • Daniel Parker
  • Eddie Paik
  • Elliott Antal
  • Katelyn Sellers
  • Liz Hill
  • Mallory Starnes
  • Mark Conachan
  • Michael Chatten
  • Myjive
  • Ron Edelen
  • Shelton Clinard

In an effort to exercise the left side of our brain, this week I’m covering some programming fundamentals.

Syntax
In computer science, the syntax of a programming language is the set of rules that define the combinations of symbols that are considered to be correctly structured programs in that language. All of the examples shown are in PHP.

Variables (declaration)
A Variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents.

$x = true;
$y = 7;
$z = 8.5;
array $a = array(2, 4, 8);
$b = "hello";

Data Types
Boolean: A TRUE or FALSE value.
Integer: A whole number (no fractions).
Float: A number (usually not a whole number) that includes decimal places.
Array: Stores a number of elements in a specific order.
String: A series of single characters.
There are many more data types, but the above are the most basic.

$x = true; // $x is a Boolean
$y = 7; // $y is an Integer
$z = 8.5; // $z is a Float
$a = array(1, 5, 9); // $a is an Array
$b = "hello"; // $b is a String

Conditions
In a Condition, a comparison operator (>, <=, ==, !=, &&, ||) is used to determine a boolean value.

$x == true
$y > 5 && $z < 10 
$y > 5 || $z < 5

If Statement
An If is conditional statement that perform different executions depending on whether a specified boolean condition evaluates to true or false.

if ($x == true)
	// consequent statements

If Else Statement
The Else statement provides an opportunity to perform alternative executions when the If statement is evaluated to false.

if ($y < 10)
	// consequent statements
else
	// alternative statements

Else If Statement
The Else If statement allow for additional conditions to be checked. Only the statements following the first condition that is found to be true will be executed. All other statements will be skipped. The statements of the final else will be executed if none of the conditions are true.

if ($y >= 10)
	// consequent statements
else if ($y > 5 && $z < 10)   
	// different statements 
else if ($y > 5 || $z < 5)
	// alternative statements
…
else
	// other statements;

Switch (Case) Statement
A Switch (or Case) statement is a type of selection control mechanism that allows the value of a variable or condition to control the flow of execution via a multiway branch.

switch ($y) {
	case 7:
		// statements
		break;
	case 8:
		// different statements
		break;
	default:
		// other statements
}

For Loop (Iterator-based)
A For Loop is a statement which allows code to be repeatedly executed. Distinguished from a while statement due to use of an explicit counter or variable used in loop.

for ($i = 1; $i <= 3; $i++) {   
	// statements; 
} 

foreach ($a as $key => $value) {
	// statements;
}

While Loop
A While Loop is a statement that allows code to be executed repeatedly based on a given condition. The while loop can be thought of as a repeating if statement.

$c = 0;
while ($c < count($a)) {
	// statements

	if ($c == 1)
		break;

	// other statements
}

Function
A function is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code. Often functions accept and return values.
Many programming languages have common “built-in” functions.

function totalArray($array) {
	$total = 0;

	// loop through array
	foreach ($a as $value) {
		$total += $value;
	}

	// return from function
	return total;
}

$arrayTotal = totalArray($a); // $arrayTotal gets the total of the values in $a

These are just a few of the basic concepts of programming. Understanding conditions, loops and code execution very much requires the logical, Left-side of your brain. Practicing even these basic concepts will exercise this half or your brain, and perhaps even give you a great appreciation of developers...

Sources:
http://us3.php.net/manual/en/
http://en.wikipedia.org

Be Sociable, Share!

    Leave a Comment

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>