introduction to programming languages s1.3.1bina © 1998 liran & ofir introduction to...
TRANSCRIPT
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
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
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
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
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
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
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
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
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
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
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
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.
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
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
Introduction to Programming Languages S1.3.15Bina © 1998 Liran & Ofir
OperatorsOperatorsOperatorsOperators
Arithmetic OperatorsArithmetic Operators
+ , - , * , / , ...+ , - , * , / , ...
Relational OperatorsRelational Operators
< , <= , == , !=...< , <= , == , !=...
Logical OperatorsLogical Operators
&& , || , && , || , !!
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.
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
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
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)
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
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
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.
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.
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
}}