22
Programming Elementary Concepts

Elementary Concepts - Técnico Lisboa - Autenticação Elements 18 Programação (Pro364511) Oval Parallelogram Rectangle Diamond Hybrid Name Symbol Use in Flowchart Denotes the beginning

Embed Size (px)

Citation preview

Programming

Elementary Concepts

Summary} Computer

} Notion of an algorithm

} Programming language} Lexicon, syntax and semantics} Grammars

} Flowchart

Programação (Pro364511)2

Why C ?} http://crashworks.org/if_programming_languages_were_vehicles/

} http://spectrum.ieee.org/computing/software/the-2015-top-ten-programming-languages

} http://www.slant.co/topics/25/~programming-language-to-learn-first

Programação (Pro364511)3

Processor} Functional unit of a computer

} Accesses memory locations} Place where you store data} Scalar data

} Sequentially performs very simple instructions} Reads value, value stores} Sums, subtracts, etc..} Compare} Conditional jumps

Programação (Pro364511)4

Machine Code7F454C4601010100000000000000000002000300010000008080040834000000F400000000000000340020000200280006000500010000000000000000800408008004089D0000009D000000050000000010000001000000A0000000A0900408A09004080E0000000E0000000600000000100000000000000000000000000000BA0E000000B9A0900408BB01000000B804000000CD80B801000000CD8000000048656C6C6F2C20776F726C64210A000000546865204E65747769646520417373656D626C657220302E39382E333900002E7368737472746162002E74657874002E64617461002E627373002E636F6D6D656E7400000000000000000000000000000000000000000000000000000000000000000000000000000000000B000000010000000600000080800408800000001D00000000000000000000001000000000000000110000000100000003000000A0900408A00000000E00000000000000000000000400000000000000170000000100000001000000AE900408AE00000002000000000000000000000001000000000000001C000000010000000000000000000000B00000001F0000000000000000000000010000000000000001000000030000000000000000000000CF0000002500000000000000000000000100000000000000

Programação (Pro364511)5

Low-level Language (Assembly)} .cstring} LC0:} .ascii "%d \0"} .text} .globl _main} _main:} LFB3: pushq %rbp} LCFI0: movq %rsp, %rbp} LCFI1: subq $16, %rsp} LCFI2: movl $0, -4(%rbp)} jmp L2} L3: movl -4(%rbp), %esi} leaq LC0(%rip), %rdi} movl $0, %eax} call _printf

Programação (Pro364511)6

Algorithm} Informal definition:

} Sequence of instructions to perform a task

} Examples:} Recipes} Kits assembly instructions} Instructions for washing hands

Programação (Pro364511)7

Algorithm} Formal definition:

} Finite sequence of well-defined and unambiguous instructions } Instructions with precise execution in finite time and finite

effort

} Examples:} Directions to somebody’s house} A recipe for cooking a cake} The steps to compute the cosine of 90°} Instructions to sort a list of integers

Programação (Pro364511)9

Programming Language} Well-defined notation

} Allows to describe a sequence of instructions that the computer can execute

} Machine language} Elementary instructions} Dependent of the processor

} High-level languages} Complex instructions} Independent of the processor} Example

} If (a <0) print ("negative")

Programação (Pro364511)10

Programming Language} Lexical level: how characters are combined to produce

language elements } i and f produces if

} Syntactical level: how language elements are combined to produce language expressions} If (42 == answer) } Exit(1)

} Semantic level: how language expressions are combined in order to form a meaning} Exit from the program if the answer is 42

} Two programs written in different languages could do the same thing (semantics) but the symbols used to write the program may be different (syntax).

Programação (Pro364511)11

Syntax and Grammar} Grammar

} Formal description of the syntax rules of a language} Precise rules to construct a program

} Comply with the rules of grammar} Compile errors

} Compiler checks the syntax of your program} But doesn’t find all semantic errors !

} Grammar description:} BNF (Backus Naur-Form)} Syntactic diagrams

Programação (Pro364511)12

Example: Control Flow} if (value <10) instructions} Phrase consisting of:

} Word “if”} Condition value <10 surrounded by parentheses} Instructions

} Terminal symbols} if (,) and <

} Non-terminal symbols} Condition and instructions} Defined elsewhere in the grammar

Programação (Pro364511)13

How do You Write a Program?} Decide what steps are needed to complete the task} Write the steps in pseudocode (written in English) or

as a flowchart (graphic symbols)} Translate into the programming language} Try out the program and debug it (fix it if necessary)

What is Pseudocode?} List of steps written in English} Like the instructions for a recipe} Must be in the right sequence

} Imagine saying “bake the cake” and then “mix it up”

Sample Pseudocode} Task: add two numbers} Pseudocode:

} Start } Get two numbers} Add them} Print the answer} End

Another Sample: Calculating Age} Pseudocode:

} Start } Get year born} Calculate age} Print age} If age > 50 print OLD} End

Flowcharts Elements

Programação (Pro364511)18

Oval

Parallelogram

Rectangle

Diamond

Hybrid

Name Symbol Use in Flowchart

Denotes the beginning or end of the program

Denotes an input operation

Denotes an output operation

Denotes a decision (or branch) to be made. The program should continue along one of two routes. (e.g. IF/THEN/ELSE)

Denotes a process to be carried oute.g. addition, subtraction, division etc.

Flow line Denotes the direction of logic flow in the program

Flowchart Example} The pseudocode would look like this as a flowchart

Programação (Pro364511)19

Start

Get 2 numbers

Add them

Print answer

End

Symbol Explained !

} START/END} Used at the beginning and end of each

flowchart

} INPUT/OUTPUT} Shows when information/data comes

into a program or is printed out

} PROCESS} Used to show calculations, storing of

data in variables, and other processing steps that may occur

Symbols Explained !

} DECISION} Program must decide whether some statement (usually a

comparison) is true or false.} YES and NO (or T/F) branches are usually shown.

Programação (Pro364511)21

Y

N

X>7?

To Review} Marques de Sá

} Capítulo 1

} Damas} Introdução

Programação (Pro364511)22