introduction to programming by mufix commnity
Post on 18-Dec-2014
243 Views
Preview:
DESCRIPTION
TRANSCRIPT
Introduction to Programming“How to win friends
and influence computers”
A Brief History of Computers
General purpose minionsOrders
Human calculators
Mechanical calculators
Electronic calculators
General purpose machinesPrograms
What Is Programming
Programming is just telling a computer to do something,
BUTComputers know very little and have no common
sense what-so-ever, so your instructions have to be broken down to small, simple parts,
ANDComputers will do exactly what you tell them.
Basic Programming
Programming at it’s core is about two things: defining problems, and solving problems.
These two aspects are tightly integrated• To solve a problem you first must clearly know
what it is• Clearly and completely defining a problem
gets you a long ways towards a solution
Four Tools To Get Started
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
–Action/Event List–Flow Chart–Data Model–Iterative Refinement
Action ListThis is a good tool to use in applications that have
user interaction.Write down each action the user can take.Add a bit of detail info to each action• Circumstances in which the user may take the
action (1-3 sentences, and/or list)• What happens when they do the action (1-3
sentences)• What other actions this one allows and denies
(list format)
Flow Charts
Flow charts are a very useful tool to clarify and define programming problems
• They delineate key points in your program• They’re especially good for state-based
programs, such as web applications
Don’t get too bogged down in details when creating a flow chart – stay high level
Data Model
A data model is a detailed description of the information that the application will manipulate.
Start with a general idea/concept, then break it down into smaller parts, and repeat.
Aim for having each part be a string, a number, or a list or collection thereof.
Iterative Refinement - the Ur-Program
The process of iterative refinement
2. Say what you want the computer to do
3. If it’s all in terms the computer will understand, then stop, you’re done
4. Otherwise, break each part into simpler steps
5. Go to step 2
NOTE: works for small children as well as computers
The Real World
These are important concepts, and when done right you end up documenting your code as you go.
HOWEVER…
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.
What Computers Can Do Well
What Computers Can Do Well
Doing the same things over and over
Remembering things
Mathematics, ‘regular’ and true / false
Changing actions based on conditions
Taking in information
Sending out information
What Computers Can Do Well
Doing the same things over and over
Remembering things
Mathematics, ‘regular’ and true / false
Changing actions based on conditions
Taking in information
Sending out information
Loops and Subroutines
Variables and Assignment
Operators
Conditionals
Input
Output
Statements and Blocks
Statements, and Blocks
A statement is a basic instruction for the computer
Statements are executed in order, from first to last
One or more statements may be grouped together into a block
A block is marked by curly braces{ and }
LoopsA loop is a special kind of statement that tells a computer
to repeat a given set of instructions
A for-loop repeats a given number of timese.g. say “hi” five times: for (1..5) { say “hi” }
A while-loop repeats as long as a certain condition is truee.g. go somewhere : while (not arrived) { take step }
SubroutinesA 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.
Once a function finishes, the execution returns to the place from which it was called.
Functions can take parameters, or arguments, which allow values to be sent into the block of code
Functions can return values.
Variables
A variable has a name, or identifier, and may have a value associated.
Associate a given value with a given variable by making an assignment.
Variables can be declared, which creates them without any value. Some languages require this.
Variable Names
MUST:Start with a letterContain letters or numbers or underscores
SHOULD:Be meaningful (often 10+ characters)Be readable (use _ or mixCase to mark words)Follow convention (start lower case, i, x, y)
Basic ValuesLiteral values:
Numbers0, -3, 8.223419783
Strings (a series of characters)“zero”, ”0”, “cheddar cheese”
Expressions:One or more values, expressions, or variables combined by operators
8.3 * 4.2912
“smoked” . “ “ . “cheddar”A variable used in a expression is replaced by its value
ingredient_count + 3
OperatorsThere are all the standard math operators
+ - * / % ()
The string concatenation operator combines two or more strings into oneDepends on the language (often . or +)
There are many other operators tooBoolean mathComparisonsSpecialized
Operators combine values and expressions into larger expressions
Conditionals
A conditional, or branch, statement executes a block of other statements based on whether a given condition is true or false
Conditions are evaluated using boolean operators, as well as comparisons and other things that are true or false
General Conditional Statement
if (condition)
Block executed if condition is true
else if (other condition)
Block executed if other condition is true
else
Block executed if all conditions are false
Input and Output
Special statements get data into and out of a program.Details vary by language
Data can come from:keyboard, web page, mouse, file, other
Data can go to:screen, file, the internet, other
Comments
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.
Comments are usually denoted by# or // to comment to the end of the line
/* to comment out multiple lines
*/
Best PracticesAlways make backups of original working code before
messing with it
Use comments to mark what you change, how, and when, and to keep copies of original statements
Test early, test often
Work together – more eyes means fewer bugs
Read the manual – there are great references online
Look at examples
mr.islamelhosary@gmail.com
top related