1 / 45

Elements of Programming Languages

Dive into the structures of programming languages, syntax, semantics, and types of values like primitives, composites. Learn about commands, function calls, and language processors.

bise
Download Presentation

Elements of Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Elements of Programming Languages

  2. Outlines • Structures of programming languages • Values, types, and variables • Commands • Function calls Elements of Programming Languages

  3. Structures of programming languages • Syntax and semantics • Classes of languages • Language processors 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. 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 Elements of Programming Languages

  6. 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)); } Example Program Elements of Programming Languages

  7. Values, types and variables • Primitive values/types • Composite values/types • Expressions Elements of Programming Languages

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

  9. 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. 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 Elements of Programming Languages

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

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

  13. 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)); } Example of literals 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 Elements of Programming Languages

  15. Arrays 0 1 2 3 4 {3.21, 4.44, 6.09. 9.12, 0.96} 0 1 2 3 4 0 1 2 3 4 0 1 2 {‘C’, ‘A’, ‘N’} Elements of Programming Languages

  16. name: string address: string ID: integer phone: string gpax: float Records {“john”, “12 Phayathai Bangkok”, 5011231, “0812237654”, 3.2} Elements of Programming Languages

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

  18. 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)); } Example of variables Elements of Programming Languages

  19. 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”, 50123415, “0812322333”, 3.1} Elements of Programming Languages

  20. 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)); } Example of array constructors Elements of Programming Languages

  21. 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 Elements of Programming Languages

  22. 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)); } Example of expressions Elements of Programming Languages

  23. Function calls • Function call • computes a result by applying a part of program called“function” or “method” to one or more arguments. • A function callF (E1, . . . ,En) • F : the name of the function • E1, . . . ,En: the argumentsin 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 Elements of Programming Languages

  24. 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)); } Example of function calls Elements of Programming Languages

  25. Commands • Assignments • Sequential commands • Jumps • Conditional commands • Iterative commands • Proper procedure calls Elements of Programming Languages

  26. 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 */ Elements of Programming Languages

  27. 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”; Elements of Programming Languages

  28. 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)); } Example of assignments Elements of Programming Languages

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

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

  31. 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. Elements of Programming Languages

  32. 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)); } Example of if statements Elements of Programming Languages

  33. 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); } Elements of Programming Languages

  34. 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)); } Example of iteratives Elements of Programming Languages

  35. Functions • Function prototype • Function definition • Function call • Local/Global variables Elements of Programming Languages

  36. Function prototype • Function name • Return type • Arguments • Examples: • float average(int a, int b, int c); Elements of Programming Languages

  37. 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)); } Example of function prototypes Elements of Programming Languages

  38. 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; } Elements of Programming Languages

  39. 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)); } Example of function definition Elements of Programming Languages

  40. 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 Elements of Programming Languages

  41. 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)); } Example of function calls 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. Elements of Programming Languages

  43. 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)); } Example of local variables Elements of Programming Languages

  44. Parameters/arguments • Copy parameter mechanisms • Reference parameter mechanisms • The Correspondence Principle Elements of Programming Languages

  45. 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)); } Example of parameters Elements of Programming Languages

More Related