Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming...

24
roduction to Programming Languages Bina © 1998 Liran & Ofir Introduction to Introduction to Programming Languages Programming Languages Programming in Programming in C C

Transcript of Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming...

Page 1: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir

Introduction to Programming Introduction to Programming LanguagesLanguages

Introduction to Programming Introduction to Programming LanguagesLanguages

Programming inProgramming in

CC

Page 2: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.2Bina © 1998 Liran & Ofir

Programming LanguagesProgramming LanguagesProgramming LanguagesProgramming Languages

The need for Programming LanguagesThe need for Programming Languages

Programming Languages allow programmersProgramming Languages allow programmers

to express computations that are to be carriedto express computations that are to be carried

out by computersout by computers

Page 3: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.3Bina © 1998 Liran & Ofir

Programming LanguagesProgramming LanguagesProgramming LanguagesProgramming Languages

Machine level Programming Languages areMachine level Programming Languages are

understood by machines, but are difficult forunderstood by machines, but are difficult for

humans to understand and express ideashumans to understand and express ideas

The need for Programming LanguagesThe need for Programming Languages

High Level Programming LanguagesHigh Level Programming Languages

Page 4: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.4Bina © 1998 Liran & Ofir

Programming LanguagesProgramming LanguagesProgramming LanguagesProgramming Languages

High Level Programming Languages:High Level Programming Languages:

Abstract out the underlying complexities ofAbstract out the underlying complexities of computer by providing a view of thecomputer by providing a view of the computer that provides a higher level of computer that provides a higher level of abstractionabstraction

Page 5: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.5Bina © 1998 Liran & Ofir

Programming LanguagesProgramming LanguagesProgramming LanguagesProgramming Languages

Programming Languages are defined by:Programming Languages are defined by:

Syntax rulesSyntax rules

Semantics rulesSemantics rules

Page 6: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.6Bina © 1998 Liran & Ofir

SyntaxSyntaxSyntaxSyntax

The rules describing the forms of The rules describing the forms of Programming Languages constructsProgramming Languages constructs

Describes which sequence of characters are Describes which sequence of characters are valid programs of the languagevalid programs of the language

Page 7: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.7Bina © 1998 Liran & Ofir

SemanticsSemanticsSemanticsSemantics

The rules describing the meaning of The rules describing the meaning of syntactically correct programssyntactically correct programs

Describes what will the program do if executedDescribes what will the program do if executed

Page 8: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.8Bina © 1998 Liran & Ofir

Lexical RulesLexical RulesLexical RulesLexical Rules

Describe the valid words (lexems) from Describe the valid words (lexems) from which a valid program can be constructedwhich a valid program can be constructed

ConstantsConstants 9, 9.6, 1.7E+3, 'b'9, 9.6, 1.7E+3, 'b'

IdentifiersIdentifiers book, salarybook, salary

Reserved (key) wordsReserved (key) wordsif, return, doif, return, do

Page 9: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.9Bina © 1998 Liran & Ofir

Syntax RulesSyntax RulesSyntax RulesSyntax Rules

Describe which sequence of lexems form Describe which sequence of lexems form valid programsvalid programs

Assignment is Assignment is Identifier = Expression Identifier = Expression

Expression is Expression is Identifier or Constant Identifier or Constant

ratio = 0.9ratio = 0.9 (valid)(valid)Color = redColor = red

name = John Smithname = John Smith (invalid)(invalid)Ratio = 0.0.9Ratio = 0.0.9

Page 10: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.10Bina © 1998 Liran & Ofir

Semantic rulesSemantic rulesSemantic rulesSemantic rules

Describe consistency rules.Describe consistency rules. A variable must be declared before it is used.A variable must be declared before it is used.

Describe the meaning of valid programs.Describe the meaning of valid programs.

Count = 9;Count = 9;

Value of the variable Count becomes 9Value of the variable Count becomes 9

Count = Count + 1;Count = Count + 1;

Value of the variable Count is incremented by 1Value of the variable Count is incremented by 1

Page 11: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.11Bina © 1998 Liran & Ofir

CompilerCompilerCompilerCompiler

Compiler

Program in a high level languageProgram in a high level language

Equivalent program in the machine languageEquivalent program in the machine language

Page 12: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.12Bina © 1998 Liran & Ofir

Elements of Programming Elements of Programming LanguagesLanguages

Elements of Programming Elements of Programming LanguagesLanguages

VariablesVariables Variable TypesVariable Types OperatorsOperators ExpressionsExpressions AssignmentAssignment Control StructuresControl Structures Functions Functions etc.etc.

Page 13: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.13Bina © 1998 Liran & Ofir

VariablesVariablesVariablesVariables

Abstraction of Computer Memory.Abstraction of Computer Memory.

A variable has a nameA variable has a name

NameName

Page 14: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.14Bina © 1998 Liran & Ofir

VariableVariableVariableVariable

Variable Type:Variable Type:

Abstraction of the set of values the variable Abstraction of the set of values the variable may assume:may assume:

Integer (int) Integer (int) 9, 7, -129909, 7, -12990

Character (char) Character (char) 'A', '\n', 'b' 'A', '\n', 'b'

Boolean Boolean True, False True, False

WeekDay WeekDay Monday, WednesdayMonday, Wednesday

Page 15: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.15Bina © 1998 Liran & Ofir

OperatorsOperatorsOperatorsOperators

Arithmetic OperatorsArithmetic Operators

+ , - , * , / , ...+ , - , * , / , ...

Relational OperatorsRelational Operators

< , <= , == , !=...< , <= , == , !=...

Logical OperatorsLogical Operators

&& , || , && , || , !!

Page 16: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.16Bina © 1998 Liran & Ofir

OperatorsOperatorsOperatorsOperators

Precedence rules: Define the order by Precedence rules: Define the order by which the operators are evaluated.which the operators are evaluated.

Associativity rules: Define the order by Associativity rules: Define the order by which the operators having the same which the operators having the same precedence are evaluated.precedence are evaluated.

Page 17: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.17Bina © 1998 Liran & Ofir

ExpressionsExpressionsExpressionsExpressions

Arithmetic Expressions:Arithmetic Expressions: Operands: variablesOperands: variables constantsconstants Operators: +Operators: + -- ** // ...... Example: 3+5*7Example: 3+5*7 3*(a+b)/n3*(a+b)/n

Page 18: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.18Bina © 1998 Liran & Ofir

ExpressionsExpressionsExpressionsExpressions Relational Expressions:Relational Expressions:

Operands: variablesOperands: variables constantsconstants Operators: >Operators: > >=>= << <=<= ====

!=!= ...... Example: 3 > 5Example: 3 > 5 a+b <= 3*xa+b <= 3*x

Page 19: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.19Bina © 1998 Liran & Ofir

ExpressionsExpressionsExpressionsExpressions

Boolean Expressions:Boolean Expressions: Operands: Boolean variablesOperands: Boolean variables

Boolean constants: True, FalseBoolean constants: True, False Operators: Operators: and - &&and - &&

or - ||or - ||

not - !not - ! Example: a Example: a &&&& b b

((a+b) >= 12 ) ((a+b) >= 12 ) && && (x ! (x != = y) y)

Page 20: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.20Bina © 1998 Liran & Ofir

AssignmentAssignmentAssignmentAssignment

Changes the value of a variable.Changes the value of a variable.

<Assignment> ==> <Var> = <Expression><Assignment> ==> <Var> = <Expression>

Example:Example:

A

A = 5;A = 5;

5

Page 21: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.21Bina © 1998 Liran & Ofir

Control StructuresControl StructuresControl StructuresControl Structures

Describe the order of statement executionDescribe the order of statement execution

SequenceSequence

SelectionSelection

IterationIteration

Page 22: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.22Bina © 1998 Liran & Ofir

Control StructuresControl StructuresControl StructuresControl Structures

SequenceSequence

Statement-1;Statement-1;

Statement-2;Statement-2;......

Statement-n;Statement-n;

Statements are executed sequentially one afterStatements are executed sequentially one after

another.another.

Page 23: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.23Bina © 1998 Liran & Ofir

Control StructuresControl StructuresControl StructuresControl Structures Selection:Selection:

if ( Expr ) {if ( Expr ) {

Sequence-1Sequence-1

}}

Expr is evaluated. If the result is True,

statements of Sequence-1will be executed.

if (Expr) {if (Expr) {

Sequence-1Sequence-1

}}

else {else {

Sequence-2Sequence-2

}}

Expr is evaluated. If the result is True, statements of Sequence-1 will be executed.

Otherwise, statementsof Sequence-2 will be executed.

Page 24: Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.

Introduction to Programming Languages S1.3.24Bina © 1998 Liran & Ofir

Control StructuresControl StructuresControl StructuresControl Structures

Iteration:Iteration:

Expr is evaluated. If the result is True,

Statements of Sequence-1will be executed.

Expr is evaluated again. If the result is True,

Statements of Sequence-1 will be executed. ...

while (Expr) { while (Expr) {

Sequence-1Sequence-1

}}