Introduction To Programming (2009 2010)

34
Introduction to Programming “How to win friends and influence computers”

description

MUFIX Community SessionName : Introduction To ProgrammingSpeaker : Islam El-HosaryTerm 1 (2009-2010)

Transcript of Introduction To Programming (2009 2010)

Page 1: Introduction To Programming (2009 2010)

Introduction to Programming“How to win friends

and influence computers”

Page 2: Introduction To Programming (2009 2010)
Page 3: Introduction To Programming (2009 2010)
Page 4: Introduction To Programming (2009 2010)

A Brief History of Computers

General purpose minionsOrders

Human calculators

Mechanical calculators

Electronic calculators

General purpose machinesPrograms

Page 5: Introduction To Programming (2009 2010)

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.

Page 6: Introduction To Programming (2009 2010)
Page 7: Introduction To Programming (2009 2010)

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

Page 8: Introduction To Programming (2009 2010)
Page 9: Introduction To Programming (2009 2010)

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

Page 10: Introduction To Programming (2009 2010)

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)

Page 11: Introduction To Programming (2009 2010)

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

Page 12: Introduction To Programming (2009 2010)

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.

Page 13: Introduction To Programming (2009 2010)

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

Page 14: Introduction To Programming (2009 2010)

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.

Page 15: Introduction To Programming (2009 2010)

What Computers Can Do Well

Page 16: Introduction To Programming (2009 2010)

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

Page 17: Introduction To Programming (2009 2010)

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

Page 18: Introduction To Programming (2009 2010)

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 }

Page 19: Introduction To Programming (2009 2010)

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 }

Page 20: Introduction To Programming (2009 2010)

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.

Page 21: Introduction To Programming (2009 2010)

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.

Page 22: Introduction To Programming (2009 2010)

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)

Page 23: Introduction To Programming (2009 2010)

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

Page 24: Introduction To Programming (2009 2010)

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

Page 25: Introduction To Programming (2009 2010)

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

Page 26: Introduction To Programming (2009 2010)

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

Page 27: Introduction To Programming (2009 2010)

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

Page 28: Introduction To Programming (2009 2010)

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

*/

Page 29: Introduction To Programming (2009 2010)

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

Page 30: Introduction To Programming (2009 2010)
Page 31: Introduction To Programming (2009 2010)
Page 32: Introduction To Programming (2009 2010)
Page 33: Introduction To Programming (2009 2010)