Presentation is loading. Please wait.

Presentation is loading. Please wait.

Elements of Programming Languages

Similar presentations


Presentation on theme: "Elements of Programming Languages"— Presentation transcript:

1 Elements of Programming Languages

2 Elements of Programming Languages
Outlines Structures of programming languages Values, types, and variables Commands Function calls 30/11/61 Elements of Programming Languages

3 Structures of programming languages
Syntax and semantics Classes of languages Language processors 30/11/61 Elements of Programming Languages

4 Structures of programming languages
Syntax: the form of programs how expressions, commands, declarations, and other constructs must be arranged to make a well-formed program. Semantics: the meaning of programs how a well-formed program may be expected to behave when executed on a computer. 30/11/61 Elements of Programming Languages

5 Structures of programming languages
Classes of Languages Imperative languages: C, Pascal, Java Object-oriented languages: C++, Java Concurrent languages: ADA, Modula Functional languages: Lisp, Haskell Logic languages: Prolog Language processors Compiler Interpreter Hybrid 30/11/61 Elements of Programming Languages

6 Elements of Programming Languages
Example Program float mean(float inp[], int s); float distance(float x1, float y1, float x2, float y2); int main(int argc, char *argv[]) { float x[4]={2,1,6,0}; float y[4]={1,3,1,4}; float d[6]; int i,j,k=0; for (i=0;i<4;i++) for (j=i+1; j<4;j++) d[k++]= distance(x[i],y[i],x[j],y[j]); printf("The mean is %f\n", mean(d, 6)); system("PAUSE"); return 0; } float mean(float inp[], int s) { int i; float res=0; for (i=0; i<s; i++) res+=inp[i]; return res/s; } float distance(float x1, float y1, float x2, float y2) {int dx,dy; dx=abs(x1-x2); dy=abs(y1-y2); return(sqrt(dx*dx+dy*dy)); 30/11/61 Elements of Programming Languages

7 Values, types and variables
Primitive values/types Composite values/types Expressions 30/11/61 Elements of Programming Languages

8 Elements of Programming Languages
Use of values A value is any entity that can be manipulated by a program. evaluated stored passed (sent) to different parts of programs 30/11/61 Elements of Programming Languages

9 Elements of Programming Languages
Types of values Different programming languages support different types of values C : integers, real numbers, structures, arrays, pointers C++ : integers, real numbers, structures, arrays, pointers, objects. JAVA : booleans, integers, real numbers, arrays, and objects. 30/11/61 Elements of Programming Languages

10 Primitive types/values
A primitive value cannot be decomposed into simpler values. A primitive type is one whose values are primitive. Built-in primitive types C, C++ integers, reals, characters, pointers. JAVA booleans, integers, reals, characters. Defined primitive types C : structures, arrays. C++ : structures, arrays, objects. JAVA : arrays, and objects 30/11/61 Elements of Programming Languages

11 Built-in primitive types/values
Boolean type false and true. Character type ‘A’, ‘+’, ‘1’, ‘.’ Integer type 121, , 0 Float type , , .9801E-908 30/11/61 Elements of Programming Languages

12 Elements of Programming Languages
Literals denote a fixed value of some type Examples: Integers: 345, Floats: , E-21 Booleans: TRUE, FALSE Characters: ‘3’, ‘a’, ‘+’, ‘\0’ (null) Strings: “This is a string.” 30/11/61 Elements of Programming Languages

13 Elements of Programming Languages
Example of literals float mean(float inp[], int s); float distance(float x1, float y1, float x2, float y2); int main(int argc, char *argv[]) { float x[4]={2,1,6,0}; float y[4]={1,3,1,4}; float d[6]; int i,j,k=0; for (i=0;i<4;i++) for (j=i+1; j<4;j++) d[k++]= distance(x[i],y[i],x[j],y[j]); printf("The mean is %f\n", mean(d, 6)); system("PAUSE"); return 0; } float mean(float inp[], int s) { int i; float res=0; for (i=0; i<s; i++) res+=inp[i]; return res/s; } float distance(float x1, float y1, float x2, float y2) {int dx,dy; dx=abs(x1-x2); dy=abs(y1-y2); return(sqrt(dx*dx+dy*dy)); 30/11/61 Elements of Programming Languages

14 Composite types/values
A composite value (or data structure) can be composed into simpler values. A composite type is a type whose values are composite. Examples: Arrays Collections of values of the same type Refer to by the index Structures, and records Collections of values of many types Refer to by name 30/11/61 Elements of Programming Languages

15 Elements of Programming Languages
Arrays 3.21 4.44 6.09 9.12 0.96 {3.21, 4.44, , 0.96} 3 4 6 9 -2 -5 1 2 -10 -11 5 -4 8 1 2 3 4 C A N {‘C’, ‘A’, ‘N’} 30/11/61 Elements of Programming Languages

16 Elements of Programming Languages
Records name: string address: string ID: integer phone: string gpax: float {“john”, “12 Phayathai Bangkok”, , “ ”, 3.2} 30/11/61 Elements of Programming Languages

17 Elements of Programming Languages
Variables Containers of values Have names Are of some types Simple composite Example: int credit=3, age=18; float gpax=3.12, fee= ; char grade=‘A’; char name[30]=“john”; /* string */ int score[5][50]; array 30/11/61 Elements of Programming Languages

18 Elements of Programming Languages
Example of variables float mean(float inp[], int s); float distance(float x1, float y1, float x2, float y2); int main(int argc, char *argv[]) { float x[4]={2,1,6,0}; float y[4]={1,3,1,4}; float d[6]; int i,j,k=0; for (i=0;i<4;i++) for (j=i+1; j<4;j++) d[k++]= distance(x[i],y[i],x[j],y[j]); printf("The mean is %f\n", mean(d, 6)); system("PAUSE"); return 0; } float mean(float inp[], int s) { int i; float res=0; for (i=0; i<s; i++) res+=inp[i]; return res/s; } float distance(float x1, float y1, float x2, float y2) {int dx,dy; dx=abs(x1-x2); dy=abs(y1-y2); return(sqrt(dx*dx+dy*dy)); 30/11/61 Elements of Programming Languages

19 Elements of Programming Languages
Constructions Construction expression that constructs a composite value from its component values Array constructor {10, 41, -7, 0, 9} Structure constructor {“somchai”, “1/2 Phayathai Bangkok”, , “ ”, 3.1} 30/11/61 Elements of Programming Languages

20 Example of array constructors
float mean(float inp[], int s); float distance(float x1, float y1, float x2, float y2); int main(int argc, char *argv[]) { float x[4]={2,1,6,0}; float y[4]={1,3,1,4}; float d[6]; int i,j,k=0; for (i=0;i<4;i++) for (j=i+1; j<4;j++) d[k++]= distance(x[i],y[i],x[j],y[j]); printf("The mean is %f\n", mean(d, 6)); system("PAUSE"); return 0; } float mean(float inp[], int s) { int i; float res=0; for (i=0; i<s; i++) res+=inp[i]; return res/s; } float distance(float x1, float y1, float x2, float y2) {int dx,dy; dx=abs(x1-x2); dy=abs(y1-y2); return(sqrt(dx*dx+dy*dy)); 30/11/61 Elements of Programming Languages

21 Elements of Programming Languages
Expressions Expression evaluated to yield a value. Expressions may be formed in various ways. In this section we shall survey the fundamental forms of expression: Constant Variables Constructions Function calls 30/11/61 Elements of Programming Languages

22 Example of expressions
float mean(float inp[], int s); float distance(float x1, float y1, float x2, float y2); int main(int argc, char *argv[]) { float x[4]={2,1,6,0}; float y[4]={1,3,1,4}; float d[6]; int i,j,k=0; for (i=0;i<4;i++) for (j=i+1; j<4;j++) d[k++]= distance(x[i],y[i],x[j],y[j]); printf("The mean is %f\n", mean(d, 6)); system("PAUSE"); return 0; } float mean(float inp[], int s) { int i; float res=0; for (i=0; i<s; i++) res=res+inp[i]; return res/s; } float distance(float x1, float y1, float x2, float y2) {int dx,dy; dx=abs(x1-x2); dy=abs(y1-y2); return(sqrt(dx*dx+dy*dy)); 30/11/61 Elements of Programming Languages

23 Elements of Programming Languages
Function calls Function call computes a result by applying a part of program called “function” or “method” to one or more arguments. A function call F (E1, ,En) F : the name of the function E1, ,En : the arguments in the form of expressions. Example: sqrt(2), sin(.23), pow(3.2, 3) An operator may be thought of as denoting a function. unary operator: -3, ~TRUE binary operator: (3+4)/5+6, 56%10 30/11/61 Elements of Programming Languages

24 Example of function calls
float mean(float inp[], int s); float distance(float x1, float y1, float x2, float y2); int main(int argc, char *argv[]) { float x[4]={2,1,6,0}; float y[4]={1,3,1,4}; float d[6]; int i,j,k=0; for (i=0;i<4;i++) for (j=i+1; j<4;j++) d[k++]= distance(x[i],y[i],x[j],y[j]); printf("The mean is %f\n", mean(d, 6)); system("PAUSE"); return 0; } float mean(float inp[], int s) { int i; float res=0; for (i=0; i<s; i++) res=res+inp[i]; return res/s; } float distance(float x1, float y1, float x2, float y2) {int dx,dy; dx=abs(x1-x2); dy=abs(y1-y2); return(sqrt(dx*dx+dy*dy)); 30/11/61 Elements of Programming Languages

25 Elements of Programming Languages
Commands Assignments Sequential commands Jumps Conditional commands Iterative commands Proper procedure calls 30/11/61 Elements of Programming Languages

26 Elements of Programming Languages
Assignments variable = expression Find the value from the expression Store the value in the variable Ex: average = x+y+z/3; variable++, variable-- Meaning: variable =variable+1 (or -1) Ex: i++; /* i=i+1 */ i--; /* i=i-1 */ 30/11/61 Elements of Programming Languages

27 Elements of Programming Languages
Assignments Variable initialization type variable = literal; Store the value of literal in the variable Ex: float average = 0; int day[7]={1,2,3,4,5,6,7}; char name[30]=“mary”; 30/11/61 Elements of Programming Languages

28 Example of assignments
float mean(float inp[], int s); float distance(float x1, float y1, float x2, float y2); int main(int argc, char *argv[]) { float x[4]={2,1,6,0}; float y[4]={1,3,1,4}; float d[6]; int i,j,k=0; for (i=0;i<4;i++) for (j=i+1; j<4; j++) d[ k++ ]= distance(x[i],y[i],x[j],y[j]); printf("The mean is %f\n", mean(d, 6)); system("PAUSE"); return 0; } float mean(float inp[], int s) { int i; float res=0; for (i=0; i<s; i++) res=res+inp[i]; return res/s; } float distance(float x1, float y1, float x2, float y2) {int dx,dy; dx=abs(x1-x2); dy=abs(y1-y2); return(sqrt(dx*dx+dy*dy)); 30/11/61 Elements of Programming Languages

29 Elements of Programming Languages
Sequential Commands Unless stated otherwise in your program, after finishing executing an command/instruction, the next command/instruction is executed. 30/11/61 Elements of Programming Languages

30 Elements of Programming Languages
Jumps Indicate the next instruction to be executed. goto label Ex: loop: x=x+1; y=y*2; goto loop; printf(“never get here!!!”); 30/11/61 Elements of Programming Languages

31 Elements of Programming Languages
Conditional Commands If statement if (condition) then-part [else else-part] else-part is optional When condition is true, then-part is executed. Otherwise, else-part is executed. 30/11/61 Elements of Programming Languages

32 Example of if statements
float mean(float inp[], int s); float distance(float x1, float y1, float x2, float y2); int main(int argc, char *argv[]) { float x[4]={2,1,6,0}; float y[4]={1,3,1,4}; float d[6]; int i,j,k=0; for (i=0;i<4;i++) for (j=i+1; j<4; j++) d[k++]= distance(x[i],y[i],x[j],y[j]); printf("The mean is %f\n", mean(d, 6)); system("PAUSE"); return 0; } float mean(float inp[], int s) { int i; float res=0; for (i=0; i<s; i++) res=res+inp[i]; return res/s; } float distance(float x1, float y1, float x2, float y2) {int dx,dy; if(x1>x2) {dx=x1-x2; …} else {dx=x2-x1; …} if (y1>y2) dy=y1-y2; else dy=y2-y1; return(sqrt(dx*dx+dy*dy)); 30/11/61 Elements of Programming Languages

33 Elements of Programming Languages
Iterative Commands While loop Do-while loop For loop for (initial-st ; condition ; incr-st ) loop-body Ex: factorial=1; for (i=1; i<10; i++) { factorial=factorial*i; printf(“%d ! =%d\n”, I, factorial); } 30/11/61 Elements of Programming Languages

34 Elements of Programming Languages
Example of iteratives float mean(float inp[], int s); float distance(float x1, float y1, float x2, float y2); int main(int argc, char *argv[]) { float x[4]={2,1,6,0}; float y[4]={1,3,1,4}; float d[6]; int i,j,k=0; for (i=0;i<4;i++) for (j=i+1; j<4; j++) d[k++]= distance(x[i],y[i],x[j],y[j]); printf("The mean is %f\n", mean(d, 6)); system("PAUSE"); return 0; } float mean(float inp[], int s) { int i; float res=0; for (i=0; i<s; i++) res=res+inp[i]; return res/s; } float distance(float x1, float y1, float x2, float y2) {int dx,dy; dx=abs(x1-x2); dy=abs(y1-y2); return(sqrt(dx*dx+dy*dy)); 30/11/61 Elements of Programming Languages

35 Elements of Programming Languages
Functions Function prototype Function definition Function call Local/Global variables 30/11/61 Elements of Programming Languages

36 Elements of Programming Languages
Function prototype Function name Return type Arguments Examples: float average(int a, int b, int c); 30/11/61 Elements of Programming Languages

37 Example of function prototypes
float mean(float inp[], int s); float distance(float x1, float y1, float x2, float y2); int main(int argc, char *argv[]) { float x[4]={2,1,6,0}; float y[4]={1,3,1,4}; float d[6]; int i,j,k=0; for (i=0;i<4;i++) for (j=i+1; j<4; j++) d[ k++ ]= distance(x[i],y[i],x[j],y[j]); printf("The mean is %f\n", mean(d, 6)); system("PAUSE"); return 0; } float mean(float inp[], int s) { int i; float res=0; for (i=0; i<s; i++) res=res+inp[i]; return res/s; } float distance(float x1, float y1, float x2, float y2) {int dx,dy; dx=abs(x1-x2); dy=abs(y1-y2); return(sqrt(dx*dx+dy*dy)); 30/11/61 Elements of Programming Languages

38 Elements of Programming Languages
Function Definition Function header Similar to function prototype Function body Variable declaration Sequence of commands Example real average(int a, int b, in c) { int sum; sum=a+b+c; return sum/3; } 30/11/61 Elements of Programming Languages

39 Example of function definition
float mean(float inp[], int s); float distance(float x1, float y1, float x2, float y2); int main(int argc, char *argv[]) { float x[4]={2,1,6,0}; float y[4]={1,3,1,4}; float d[6]; int i,j,k=0; for (i=0;i<4;i++) for (j=i+1; j<4; j++) d[ k++ ]= distance(x[i],y[i],x[j],y[j]); printf("The mean is %f\n", mean(d, 6)); system("PAUSE"); return 0; } float mean(float inp[], int s) { int i; float res=0; for (i=0; i<s; i++) res=res+inp[i]; return res/s; } float distance(float x1, float y1, float x2, float y2) {int dx,dy; dx=abs(x1-x2); dy=abs(y1-y2); return(sqrt(dx*dx+dy*dy)); 30/11/61 Elements of Programming Languages

40 Elements of Programming Languages
Function call Function name List of arguments/parameters Example: x=average(9, 5, -1); The value in x is (9+5+(-1))/3 aav=average(9, 5, average(2,-1, 8)); The value in aav is (9+5+(2+9-1)+8)/3) /3 30/11/61 Elements of Programming Languages

41 Example of function calls
float mean(float inp[], int s); float distance(float x1, float y1, float x2, float y2); int main(int argc, char *argv[]) { float x[4]={2,1,6,0}; float y[4]={1,3,1,4}; float d[6]; int i,j,k=0; for (i=0;i<4;i++) for (j=i+1; j<4; j++) d[k++]= distance(x[i],y[i],x[j],y[j]); printf("The mean is %f\n", mean(d, 6)); system("PAUSE"); return 0; } float mean(float inp[], int s) { int i; float res=0; for (i=0; i<s; i++) res=res+inp[i]; return res/s; } float distance(float x1, float y1, float x2, float y2) {int dx,dy; dx=abs(x1-x2); dy=abs(y1-y2); return(sqrt(dx*dx+dy*dy)); 30/11/61 Elements of Programming Languages

42 Global/local variables
Global variables Variables which can be used at any location in a program. Local variables Variables which can be used locally in a function. 30/11/61 Elements of Programming Languages

43 Example of local variables
float mean(float inp[], int s); float distance(float x1, float y1, float x2, float y2); int main(int argc, char *argv[]) { float x[4]={2,1,6,0}; float y[4]={1,3,1,4}; float d[6]; int i,j,k=0; for (i=0;i<4;i++) for (j=i+1; j<4;j++) d[k++]= distance(x[i],y[i],x[j],y[j]); printf("The mean is %f\n", mean(d, 6)); system("PAUSE"); return 0; } float mean(float inp[], int s) { int i; float res=0; for (i=0; i<s; i++) res=res+inp[i]; return res/s; } float distance(float x1, float y1, float x2, float y2) {int dx,dy; dx=abs(x1-x2); dy=abs(y1-y2); return(sqrt(dx*dx+dy*dy)); 30/11/61 Elements of Programming Languages

44 Parameters/arguments
Copy parameter mechanisms Reference parameter mechanisms The Correspondence Principle 30/11/61 Elements of Programming Languages

45 Elements of Programming Languages
Example of parameters float mean(float inp[], int s); float distance(float x1, float y1, float x2, float y2); int main(int argc, char *argv[]) { float x[4]={2,1,6,0}; float y[4]={1,3,1,4}; float d[6]; int i,j,k=0; for (i=0;i<4;i++) for (j=i+1; j<4; j++) d[k++]= distance(x[i],y[i],x[j],y[j]); printf("The mean is %f\n", mean(d, 6)); system("PAUSE"); return 0; } float mean(float inp[], int s) { int i; float res=0; for (i=0; i<s; i++) res=res+inp[i]; return res/s; } float distance(float x1, float y1, float x2, float y2) {int dx,dy; dx=abs(x1-x2); dy=abs(y1-y2); return(sqrt(dx*dx+dy*dy)); 30/11/61 Elements of Programming Languages


Download ppt "Elements of Programming Languages"

Similar presentations


Ads by Google