350 likes | 815 Views
Introduction to Programming Languages. Programming in C. Programming Languages. The need for Programming Languages. Programming Languages allow programmers to express computations that are to be carried out by computers. Programming Languages. The need for Programming Languages.
E N D
Introduction to Programming Languages Programming in C
Programming Languages • The need for Programming Languages • Programming Languages allow programmers • to express computations that are to be carried • out by computers
Programming Languages • The need for Programming Languages • Machine level Programming Languages are • understood by machines, but are difficult for • humans to understand and express ideas High Level Programming Languages
Programming Languages • High Level Programming Languages: • Abstract out the underlying complexities of • computer by providing a view of the • computer that provides a higher level of • abstraction
Programming Languages • Programming Languages are defined by: Syntax rules Semantics rules
Syntax • The rules describing the forms of Programming Languages constructs • Describes which sequence of characters are valid programs of the language
Semantics • The rules describing the meaning of syntactically correct programs • Describes what will the program do if executed
Lexical Rules • Describe the valid words (lexems) from which a valid program can be constructed • Constants 9, 9.6, 1.7E+3, 'b' • Identifiers book, salary • Reserved (key) words if, return, do
Syntax Rules • Describe which sequence of lexems form valid programs • Assignment is Identifier = Expression • Expression is Identifier or Constant • ratio = 0.9 Color = red (valid) • name = John Smith Ratio = 0.0.9 (invalid)
Semantic rules • Describe consistency rules. • A variable must be declared before it is used. • Describe the meaning of valid programs. Count = 9; Value of the variable Count becomes 9 Count = Count + 1; Value of the variable Count is incremented by 1
Compiler Compiler Program in a high level language Equivalent program in the machine language
Elements of Programming Languages • Variables • Variable Types • Operators • Expressions • Assignment • Control Structures • Functions • etc.
Variables • Abstraction of Computer Memory. Name A variable has a name
Variable • Variable Type: Abstraction of the set of values the variable may assume: • Integer (int) 9, 7, -12990 • Character (char) 'A', '\n', 'b' • Boolean True, False • WeekDay Monday, Wednesday
Operators • Arithmetic Operators + , - , * , / , ... • Relational Operators < , <= , == , !=... • Logical Operators && , || , !
Operators • Precedence rules: Define the order by which the operators are evaluated. • Associativity rules: Define the order by which the operators having the same precedence are evaluated.
Expressions • Arithmetic Expressions: • Operands: variables constants • Operators: + - * / ... • Example: 3+5*7 3*(a+b)/n
Expressions • Relational Expressions: • Operands: variables constants • Operators: > >= < <= == != ... • Example: 3 > 5 a+b <= 3*x
Expressions • Boolean Expressions: • Operands: Boolean variables Boolean constants: True, False • Operators: and - && or - || not - ! • Example: a && b ((a+b) >= 12 ) && (x != y)
A Assignment • Changes the value of a variable. <Assignment> ==> <Var> = <Expression> • Example: A = 5; 5
Control Structures • Describe the order of statement execution • Sequence • Selection • Iteration
Control Structures • Sequence Statement-1; Statement-2; . . . Statement-n; Statements are executed sequentially one after another.
Expr is evaluated. If the result is True, statements of Sequence-1 will be executed. Expr is evaluated. If the result is True, statements of Sequence-1 will be executed. Otherwise, statements of Sequence-2 will be executed. Control Structures • Selection: if ( Expr ) { Sequence-1 } if (Expr) { Sequence-1 } else { Sequence-2 }
Control Structures • Iteration: while (Expr) { Sequence-1 } Expr is evaluated. If the result is True, Statements of Sequence-1 will be executed. Expr is evaluated again. If the result is True, Statements of Sequence-1 will be executed. ...