DIG1108 Lesson 5
-
Upload
vc-dig1108-fall-2013 -
Category
Technology
-
view
366 -
download
0
Transcript of DIG1108 Lesson 5
INTRO TO SERVER SIDE PROGRAMMINGLesson Five
Thursday, September 26, 13
Lists and DictionariesIn computer science, a list (or sequence) is an abstract data type that implements an ordered collection of values. Each instance of a value on a list is usually called an item, entry or element of the list, and if the same value appears multiple times in the list, each is considered a distinct item of the list.An associative array (or dictionary) is an abstract data type composed of a collection of key/value pairs, such that each key appears at most once in the collection. Pairs can be added or removed, and values can be modified or retrieved based on the associated key.
Thursday, September 26, 13
What's The Difference?A list is always ordered, can usually return elements or items by numeric index(starting at zero), and might not allow modification of values. A decent implementation of a list should provide specific operations for adding and removing elements to enable them to be used:
push - append to the end(or tail) of the list
pop - remove from the tail of the list
queue(unshift) - append to the beginning(or head) of the list
dequeue(shift) - remove from the head of the list
A hash is usually not immutable and may be indeterminately ordered. Values can usually be altered.
Thursday, September 26, 13
ArraysAn array type is a data type that is meant to describe a collection of values or variables, each selected by one or more indices(keys) tha can be computed at run-time of the program.
By default, the array type in PHP generates computed, numerical indices, starting with zero, to make a list: var_dump(array( 'one', 'two', 3, 4, 4.1, 4.2 )); $list = array(); $list[] = 'one'; $list[] = 'two';
The value for the key can also be specified as any scalar literal (neither array, object nor resource), often a string var_dump(array( 'one' => 1, 2 => 'two' )): $list[4] = 'four'; $list['five dot one'] = 5.1;
Thursday, September 26, 13
Array TricksPHP relies heavily on arrays, so there are a lot of array-specific functions, like to use an array as a queue or stackarray_unshift(), array_shift() - append to and remove from head
array_push(), array_pop() - append to and remove from tail
PHP arrays have an "internal position" pointer that can be used as an iterator:current(), key() - return the current element or key
next(), prev(), end(), reset() - advance forward or back
each() - return the current key and value and advance the pointer
Thursday, September 26, 13
Even More Array Tricks
array_merge() - merge two or more arraysarray_slice() - remove an array sectionarray_splice() - insert or replace a sectioncount(), sizeof() - calculate lengtharray_fill(), array_pad(), range() - generate valuesstr_split(), explode(), implode(), join() - array to string
Thursday, September 26, 13
ASSIGNMENT 5.1Find Some Lists and Arrays
Thursday, September 26, 13
Lists of Lists
Open Github and Cloud 9
Look through your forked projects for examples of lists and dictionary definitions and methodsCopy adn paste your examples into a file named "homework-5.1.md" with code delimiters as neededWrite comments identifying the lists, keys, indexes and other list and dictionary pieces discussedSave, add and commit, then push to Github when done
Thursday, September 26, 13
Recursion
Thursday, September 26, 13
Control Flow StatementsControl Flow - refers to the order in which the individual statements, instructions or function calls of an imperative or declarative program are executed or evaluated. Execution results in a choice being made as to which of two or more paths should be followed.
Types of Control Flow statements:
continuation at a different statement (unconditional branch or jump)
execute statements only if some condition is met (conditional branch)
execute statements until some conditional is met(loop, conditional branch)
execute defined statements and return (sub/co-routines, continuations)s
stop executing statements (unconditional halt)
Thursday, September 26, 13
LoopsA loop is a sequence of statements which is specified once but which may be carried out several times in succession, a specified number of times or indefinitelySpecific number of times: for ( $count = 0; $count < $max; $count++ ) do_something();
Once per each item in a collection(array): foreach ( $collection as $item ) do_something(); foreach ($collection as $key => $value ) do_something();
Until some condition is met: while ( $condition == true ) do_something(); do something(); while ( $condition );
Indefinitely(infinitely): while ( true ) do_something(); do something(); while ( true);
Thursday, September 26, 13
Infinite Loops
An unconditional(or infinite) loop returns to a fixed point in the diagram, usually the top of the workflow. Without a breaking statement or escape clause, it executes forever.
Thursday, September 26, 13
Conditional Loops
A conditional loop (while, do-while, for, foreach) returns to a condition check ($count < $max). Until the condition evaluates FALSE, the loop will continue to execute
Thursday, September 26, 13
Diagramming Part TwoIn your pair, find a section of a project that has some significant looping and conditional logic (at least three branches)
Individually, sketch a simple workflow diagram of the logic; assemble a truth table if neededDiscuss differences in your diagrams and make a new diagram and truth table for the logic to demonstrate
Thursday, September 26, 13
ASSIGNMENT 5.2Loops and Conditionals
Thursday, September 26, 13
Create a file called "homework-5.2.md" in your "assignments" workspace on Cloud 9
Find a section in your projects that has some decent looping and branching code: at least five branches that you can diagram.
Copy and paste your example into your file and attempt to identify the loop conditions with comments:
while ( $count < $max ) { // while $count is less than $max
foreach ( $collection as $item ) { // until there are no more $items in the $collection
Save your file, then git add, git commit -m "explain why" and git push
Thursday, September 26, 13