450 likes | 463 Views
Dive into the structures of programming languages, syntax, semantics, and types of values like primitives, composites. Learn about commands, function calls, and language processors.
E N D
Outlines • Structures of programming languages • Values, types, and variables • Commands • Function calls Elements of Programming Languages
Structures of programming languages • Syntax and semantics • Classes of languages • Language processors Elements of Programming Languages
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
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
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
Values, types and variables • Primitive values/types • Composite values/types • Expressions 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 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. Elements of Programming Languages
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
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
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
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
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
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
name: string address: string ID: integer phone: string gpax: float Records {“john”, “12 Phayathai Bangkok”, 5011231, “0812237654”, 3.2} 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=42000.0; • char grade=‘A’; • char name[30]=“john”; /* string */ • int score[5][50]; array Elements of Programming Languages
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
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
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
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
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
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
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
Commands • Assignments • Sequential commands • Jumps • Conditional commands • Iterative commands • Proper procedure calls 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 */ 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”; Elements of Programming Languages
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
Sequential Commands • Unless stated otherwise in your program, after finishing executing an command/instruction, the next command/instruction is executed. 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!!!”); 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. Elements of Programming Languages
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
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
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
Functions • Function prototype • Function definition • Function call • Local/Global variables Elements of Programming Languages
Function prototype • Function name • Return type • Arguments • Examples: • float average(int a, int b, int c); Elements of Programming Languages
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
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
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
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
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
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
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
Parameters/arguments • Copy parameter mechanisms • Reference parameter mechanisms • The Correspondence Principle Elements of Programming Languages
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