Introduction To Programming


Published on

A brief, language-no-specific introduction to programming concepts - some ways to approach a programming problem, and general characteristics of programming languages (with a bit of a slant towards scripting languages).

Published in: Technology, Education
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Introduction To Programming

  1. 1. Introduction to Programming “ How to win friends and influence computers”
  2. 2. A Brief History of Computers <ul><ul><ul><ul><li>General purpose minions </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Orders </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>Human calculators </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Mechanical calculators </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Electronic calculators </li></ul></ul></ul></ul><ul><ul><ul><ul><li>General purpose machines </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Programs </li></ul></ul></ul></ul></ul>
  3. 3. What Is Programming <ul><li>Programming is just telling a computer to do something, </li></ul><ul><li>BUT </li></ul><ul><li>Computers know very little and have no common sense what-so-ever , so your instructions have to be broken down to small, simple parts, </li></ul><ul><li>AND </li></ul><ul><li>Computers will do exactly what you tell them . </li></ul>
  4. 4. <ul><li>FACT *: The Great Sphinx was supposed to be a foot rest, but an ancient programmer accidentally told the construction computers to use furlongs instead of inches </li></ul>* for certain values of true
  5. 5. Basic Programming <ul><li>Programming at it’s core is about two things: defining problems , and solving problems . </li></ul><ul><li>These two aspects are tightly integrated </li></ul><ul><li>To solve a problem you first must clearly know what it is </li></ul><ul><li>Clearly and completely defining a problem gets you a long ways towards a solution </li></ul>
  6. 6. Four Tools To Get Started <ul><li>Just getting started is often the hardest part of the project. Before diving in to the code, do the design document. Four tools to help are </li></ul><ul><ul><li>Action/Event List </li></ul></ul><ul><ul><li>Flow Chart </li></ul></ul><ul><ul><li>Data Model </li></ul></ul><ul><ul><li>Iterative Refinement </li></ul></ul>
  7. 7. Action List <ul><li>This is a good tool to use in applications that have user interaction. </li></ul><ul><li>Write down each action the user can take. </li></ul><ul><li>Add a bit of detail info to each action </li></ul><ul><li>Circumstances in which the user may take the action (1-3 sentences, and/or list) </li></ul><ul><li>What happens when they do the action (1-3 sentences) </li></ul><ul><li>What other actions this one allows and denies (list format) </li></ul>
  8. 8. Flow Charts <ul><li>Flow charts are a very useful tool to clarify and define programming problems </li></ul><ul><li>They delineate key points in your program </li></ul><ul><li>They’re especially good for state-based programs, such as web applications </li></ul><ul><li>Don’t get too bogged down in details when creating a flow chart – stay high level </li></ul>
  9. 9. Data Model <ul><li>A data model is a detailed description of the information that the application will manipulate. </li></ul><ul><li>Start with a general idea/concept, then break it down into smaller parts, and repeat. </li></ul><ul><li>Aim for having each part be a string, a number, or a list or collection thereof. </li></ul>
  10. 10. Iterative Refinement - the Ur-Program <ul><li>The process of iterative refinement </li></ul><ul><li>Say what you want the computer to do </li></ul><ul><li>If it’s all in terms the computer will understand, then stop, you’re done </li></ul><ul><li>Otherwise, break each part into simpler steps </li></ul><ul><li>Go to step 2 </li></ul><ul><li>NOTE: works for small children as well as computers </li></ul>
  11. 11. The Real World <ul><li>These are important concepts, and when done right you end up documenting your code as you go. </li></ul><ul><li>HOWEVER… </li></ul><ul><li>Real-world programming often doesn’t explicitly involve these techniques except at high levels in the design document. You should definitely use them there, and for actual coding keep them in mind as fallback strategies if your stuck on a problem. </li></ul>
  12. 12. What Computers Can Do Well <ul><li>Doing the same things over and over </li></ul><ul><li>Remembering things </li></ul><ul><li>Mathematics, ‘regular’ and true / false </li></ul><ul><li>Changing actions based on conditions </li></ul><ul><li>Taking in information </li></ul><ul><li>Sending out information </li></ul>
  13. 13. What Computers Can Do Well <ul><li>Doing the same things over and over </li></ul><ul><li>Remembering things </li></ul><ul><li>Mathematics, ‘regular’ and true / false </li></ul><ul><li>Changing actions based on conditions </li></ul><ul><li>Taking in information </li></ul><ul><li>Sending out information </li></ul>Loops and Subroutines Variables and Assignment Operators Conditionals Input Output Statements and Blocks
  14. 14. Statements, and Blocks <ul><li>A statement is a basic instruction for the computer </li></ul><ul><li>Statements are executed in order, from first to last </li></ul><ul><li>One or more statements may be grouped together into a block </li></ul><ul><li>A block is marked by curly braces </li></ul><ul><ul><li>{ and } </li></ul></ul>
  15. 15. Loops <ul><li>A loop is a special kind of statement that tells a computer to repeat a given set of instructions </li></ul><ul><li>A for-loop repeats a given number of times </li></ul><ul><ul><li>e.g. say “hi” five times: </li></ul></ul><ul><ul><li>for (1..5) { say “hi” } </li></ul></ul><ul><li>A while-loop repeats as long as a certain condition is true </li></ul><ul><ul><li>e.g. go somewhere : </li></ul></ul><ul><ul><li>while (not arrived) { take step } </li></ul></ul>
  16. 16. Subroutines <ul><li>A subroutine , function or procedure is a block of statements that’s been given a name and which is executed when ever that name is used. Essentially, it’s a miniature sub-program that you can use in your larger program. </li></ul><ul><li>Once a function finishes, the execution returns to the place from which it was called. </li></ul><ul><li>Functions can take parameters , or arguments , which allow values to be sent into the block of code </li></ul><ul><li>Functions can return values. </li></ul>
  17. 17. Variables <ul><li>A variable has a name , or identifier , and may have a value associated. </li></ul><ul><li>Associate a given value with a given variable by making an assignment . </li></ul><ul><li>Variables can be declared , which creates them without any value. Some languages require this. </li></ul><ul><li>Depending on the language, variables can be marked with a symbol, often $ </li></ul>
  18. 18. Variable Names <ul><li>MUST : </li></ul><ul><li>Start with a letter </li></ul><ul><li>Contain letters or numbers or underscores </li></ul><ul><li>SHOULD : </li></ul><ul><li>Be meaningful (often 10+ characters) </li></ul><ul><li>Be readable (use _ or mixCase to mark words) </li></ul><ul><li>Follow convention (start lower case, i, x, y) </li></ul>
  19. 19. Basic Values <ul><li>Literal values : </li></ul><ul><ul><li>Numbers </li></ul></ul><ul><ul><ul><li>0, -3, 8.223419783 </li></ul></ul></ul><ul><ul><li>Strings (a series of characters) </li></ul></ul><ul><ul><ul><li>“ zero”, ”0”, “cheddar cheese” </li></ul></ul></ul><ul><li>Expressions : </li></ul><ul><ul><li>One or more values, expressions, or variables combined by operators </li></ul></ul><ul><ul><li>8.3 * 4.291 </li></ul></ul><ul><ul><li>2 </li></ul></ul><ul><ul><li>“ smoked” . “ “ . “cheddar” </li></ul></ul><ul><ul><li>A variable used in a expression is replaced by its value </li></ul></ul><ul><ul><li>ingredient_count + 3 </li></ul></ul>
  20. 20. Operators <ul><li>There are all the standard math operators </li></ul><ul><ul><li>+ - * / % () </li></ul></ul><ul><li>The string concatenation operator combines two or more strings into one </li></ul><ul><ul><li>Depends on the language (often . or +) </li></ul></ul><ul><li>There are many other operators too </li></ul><ul><ul><li>Boolean math </li></ul></ul><ul><ul><li>Comparisons </li></ul></ul><ul><ul><li>Specialized </li></ul></ul><ul><li>Operators combine values and expressions into larger expressions </li></ul>
  21. 21. Advanced Values <ul><li>More complicated things, data structures , can also be values. </li></ul><ul><li>An array – a list, with individual elements found by position: </li></ul><ul><ul><li>if $ar is (4,8,1,3,-3) then $ar[0] is 4 (NOTE: arrays are 0-based) </li></ul></ul><ul><li>A hash , record , or associative array – a list, with individual elements found by label: </li></ul><ul><ul><li>If $ha is (“bread_type” => “wheat”, </li></ul></ul><ul><ul><li>“ greens” => “lettuce”, </li></ul></ul><ul><ul><li>“ dressing” => “none”) then $ha[‘greens’] is ‘lettuce’ </li></ul></ul>
  22. 22. Variable Assignment <ul><li>The variable goes on the left. </li></ul><ul><li>The = goes in the middle. Read it as “is assigned the value of”. </li></ul><ul><li>The expression goes on the right. </li></ul><ul><li>ingredient_count = ingredient_count + 1; </li></ul>
  23. 23. Conditionals <ul><li>A conditional , or branch , statement executes a block of other statements based on whether a given condition is true or false </li></ul><ul><li>Conditions are evaluated using boolean operators , as well as comparisons and other things that are true or false </li></ul>
  24. 24. Basic Conditions <ul><li>0, ‘’ (empty string), and () (empty array or hash) are false . 1 and any non-empty value are true . </li></ul><ul><li>Some comparisons: </li></ul><ul><ul><li><, >, <=, >=, ==, != </li></ul></ul><ul><li>Other things: </li></ul><ul><ul><li>The value of the variable is_currently_raining </li></ul></ul><ul><ul><li>The string “fruit” contains the string “it” </li></ul></ul>
  25. 25. Advanced Conditions <ul><li>Conditions are really boolean expressions (boolean means true/false) </li></ul><ul><li>Combine boolean values using boolean operators: </li></ul><ul><li>AND – true if all parts are true (contains all) </li></ul><ul><li>OR – true if any parts are true (contains any) </li></ul><ul><li>NOT – opposite of what it would be </li></ul><ul><li>(…) – treat the enclosed as a sub-expression </li></ul>
  26. 26. Evaluating Conditions Examples <ul><li>&& AND </li></ul><ul><ul><li>T && T is T T && F is F T && T && T && F is F </li></ul></ul><ul><li>|| OR </li></ul><ul><ul><li>T || T is T T || F is T F || F is F </li></ul></ul><ul><li>! NOT </li></ul><ul><ul><li>!T is F !F is T </li></ul></ul><ul><li>(…) sub-expressions </li></ul><ul><ul><li>( T && F ) || (! (F || !T )) </li></ul></ul><ul><ul><li>( F ) || (! ( F || F )) </li></ul></ul><ul><ul><li>F || (! ( F )) </li></ul></ul><ul><ul><li>F || T is T </li></ul></ul>
  27. 27. General Conditional Statement <ul><li>if (condition) </li></ul><ul><li>Block executed if condition is true </li></ul><ul><li>else if (other condition) </li></ul><ul><li>Block executed if other condition is true </li></ul><ul><li>else </li></ul><ul><li>Block executed if all conditions are false </li></ul>
  28. 28. Input and Output <ul><li>Special statements get data into and out of a program. </li></ul><ul><ul><li>Details vary by language </li></ul></ul><ul><li>Data can come from: </li></ul><ul><ul><li>keyboard, web page, mouse, file, other </li></ul></ul><ul><li>Data can go to: </li></ul><ul><ul><li>screen, file, the internet, other </li></ul></ul>
  29. 29. Comments <ul><li>Comments are a way to put text in the program code which does not get executed. This is very useful for leaving notes / descriptions / explanations, and for testing. </li></ul><ul><li>Comments are usually denoted by </li></ul><ul><ul><li># or // to comment to the end of the line </li></ul></ul><ul><ul><li>/* to comment out multiple lines </li></ul></ul><ul><ul><li>*/ </li></ul></ul>
  30. 30. Example <ul><li>// takes: a number </li></ul><ul><li>// returns: the square of the number </li></ul><ul><li>function squareIt (x) { </li></ul><ul><li>if (! isNumber(x)) { die(“trying to square a non-number”); } </li></ul><ul><li>return x * x; </li></ul><ul><li>} </li></ul><ul><li>$areaOfShape = squareIt(5); </li></ul><ul><li>If ($areaOfShape > 20) { </li></ul><ul><li>print “pretty big ”; </li></ul><ul><li>} else { </li></ul><ul><li>print “not too large ”; </li></ul><ul><li>} </li></ul>
  31. 31. Best Practices <ul><li>Always make backups of original working code before messing with it </li></ul><ul><li>Use comments to mark what you change, how, and when, and to keep copies of original statements </li></ul><ul><li>Test early, test often </li></ul><ul><li>Work together – more eyes means fewer bugs </li></ul><ul><li>Read the manual – there are great references online </li></ul><ul><li>Look at examples </li></ul>