PROGRAMMING
WHY PROGRAMMING?
What are computers? Give examples of computers.
Why use computers? Give examples of uses of computers. Follow
these
How does a computer work?
instruction
• Input – Execute (instructions) – Output s
1. Input – numbers, words, image, sound, etc.
2. Execute
o depends on the instruction (PROGRAM)
o process, variables, procedures, methods, etc.
3. Output – numbers, words, image, sound, etc.
INSTRUCTIONS
Problem with instructions
Can you suggest a solution?
SOLUTION: TRANSLATOR
TRANSLATO
R
Programmi
ng
Interpret
Language
er
10110
Print 11010
“Hello
OR 11011
Studen
t” 01101
11…
Source Object
Compiler Machine
Human Code Code
Language
Language
Enter an integer: 5
5 is odd.
1. PROGRAMMING LANGUAGE
High-level language
There are rules on how to use a language…we call them SYNTAX
o Words , spellings, grammars, punctuations, statements, operators
The way you combine or the order of the construction of words could produce different
instructions…we call them SEMANTICS
o Be careful…”pack bag” is different from “bag pack” or
o (1 + 2 X 3 ) is not equal to 9 but 7
The sequence of steps is what we call ALGORITHM
Examples of Programming language are C++, C#, Python, Java, etc.
2. INTERPRETER / COMPILER
Translates codes (instructions written in high-level language) to machine language
INTERPRETER vs. COMPILER
o Both are used for translating but Interpreter (not only translates) also executes the instructions
o Compiler – translates the whole code in one go (faster)
• Used by C++, C#
o Interpreter – translates each line of code one at a time (requires less memory)
• Used by Python
3. MACHINE LANGUAGE
Computer’s language
0’s and 1’s
Can be produced by using switches (transistors)
0 = off, low, no, false, bad
1 = on, high, yes, true, good
WRITING A PROGRAM
TOOLS AND TIPS
COMPUTER PROGRAMMING
process of giving instructions to computer so it can perform a meaningful task and give
you an output
Execu Outpu
Input
te t
programs work with (1)numbers, (2)strings, which are a series of characters usually inside
a quotation mark, and (3) Booleans, which are either true or false…so we need to work on our
maths later
A SUCCESSFUL PROGRAM
Tools Logic
Langu Progra
for (has to
age make m
Writing sense)
Programs that doesn’t make sense (with errors) cannot be translated successfully
and will not work
Some programs that might work will have an unexpected output if the logic is
incorrect
TOOLS FOR WRITING
Planning
o Pseudocode
o Flowcharts
o Logic Diagrams
Writing
o Text editors
o CLI
o IDE
PROGRAMMING
PLANNING
WHY PLAN?
“Programs that contain spaghetti code has
a shorter life than one with structured
code”
Structure:
o Sequence – steps in correct order
o Decision – selects a path based on an answer from two choices
o Loop – repeats actions, mostly if condition is true
LOOPS & DECISIONS?
Loops
o to repeat a series of steps
• True = executes loop body until false
• False = exits the loop
o less instructions, less coding time, less errors, faster output time
Decisions
o to test a value and control repetitions
Examples of Loops and Decisions later on Pseudocode and Flowchart.
USING PSEUDOCODE TO PLAN
PLANNING
PSEUDOCODE
English-like representation of the logical steps it takes to solve a problem
o very similar to everyday English
uses variables
Statements (main steps) should have beginning word (i.e. start, begin) and ending word
(i.e. stop, end)
Loops and Conditions in Pseudocode:
o Loops – use While-Endwhile statement
o Decisions – use If-Else-Endif statement
PSEUDOCODE
Tips for writing Pseudocode:
o Write only one action statement per line
o Capitalize or bold keywords
• READ, WRITE, IF, ELSE, ENDIF, WHILE, ENDWHILE
o Indent to show hierarchy and structures
• Indent all statements that depend on a condition (IF-ELSE-ENDIF)
• Indent statements that fall inside the loop (WHILE-ENDWHILE)
Take note! 1 statement per line, indented to
USING FLOWCHART TO PLAN
PLANNING
FLOWCHART
Name Symbol Use in Flowchart
pictorial representation
Oval Denotes the beginning or end of the program
emphasizes individual
Parallelogram Denotes an input operation
steps and their
interconnections
Rectangle Denotes a process to be carried out
e.g. addition, subtraction, division etc.
geometric shapes or
symbols (which contain Diamond Denotes a decision (or branch) to be made.
statements) are connected The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)
with arrows
Hybrid Denotes an output operation
direction of arrow suggests
the flow of the process Denotes the direction of logic flow in the program
Flow line
WHY USE DIFFERENT SYMBOLS?
The different symbols found in
flowcharts represent a set of
instructions.
Each box or shape has its own
function
Let’s try to convert our Pseudocode example earlier into a
Flowchart…
FLOWCHART
Loops and
Conditions in
Flowchart:
o Loops – use “arrow
pointing back”
get Yes calculate
Begin display
aNumber aNumber ≠0 ? theAnswer
theAnsw
=
er
aNumber
No *2
o Decisions – use a
“diamond” shape End
TIPS FOR AN EFFECTIVE FLOWCHART
1. Label each flowchart with a title identifying the process that it illustrates.
o Example: "Order Entry Process“
2. Clearly indicate the starting and ending points of the process, using the standard
terminator symbols.
3. Keep the direction of flow consistent.
o Avoid confusion by keeping your flow lines moving from top to bottom and left to right.
o Don't reverse the direction in the chart unless the flow reverses itself in reality.
4. Break the steps down to a consistent level of detail.
o Don't include trivial sub-steps of one task while treating another equivalent task as a whole.
o If one step or task needs to be analyzed in detail, make a separate chart illustrating that sub-
process.
TIPS FOR AN EFFECTIVE FLOWCHART
5. Avoid crossing flow lines.
o In a well-designed chart, flow lines will not cross each other.
o By rearranging a chart you can usually get rid of crossed lines.
o If two lines must cross, use a "bridge" (also known as a "line hop") to
show that the lines do not intersect.
6. Make sure there are at least two outcomes from every decision
diamond.
7. Label your flowchart components.
o Use active verbs to label activity steps and questions to label
decisions.
o Clearly label the outcomes from a decision diamond in terms that
answer the question.
Group
Let us construct a Activity
flowchart that describes
the process of painting a
house.
Apply the lessons you have
learned in making an
effective flowchart.
ABOUT
LOOPS
LOOPS
o n = n +1
o usually for counting
o (new) n = (old) n + 1
o you need to give (old) n and initial value
o n=n+m
o usually for incrementing by the value of m
o (new) n = (old) n + m
o you need to give (old) n and initial value
Let’s try some examples…
PROGRAMMING
CODING
WRITING A SOURCE CODE
You may use:
Text editor
Visual programming tool
o Command line
o Object-oriented
Integrated Development Environment (IDE)
o often includes a compiler, text editor, debugger
THANK YOU
IT4107