L a n g u a g e s 

Programming
CPT 106 · Franklin College · Erich Prisner · 2002-2006

A programming language is a formal language powerful enough to encode any possible program with it (it is Turing-complete). For this reason, HTML or CSS ar not programming languages. Semantics refers to the meaning of code written in the languages, whereas syntax refers to the way the parts are assembled. If a program contains syntactical errors, the computer will not accept it. If it contains semantical errors, it will accept it and run, but the result will not be what was intended.

The syntax of a language is mostly defined by formal rules, grammars, but we will describe the syntax of the Javascrip and PHP in a more informal way.

When a program is about to be executed, it must be compiled or interpreted. A compiler is a program translating the source code of the program into object code in a lower-level language, like assembly or machine language. An interpreter is a program that executes a program, of course line-by-line. Both Javascript and PHP are interpreted---you don't have object code stored somewhere.

Variables

Variables hold values. Declaring a variable means telling the program that the variable will be used. The syntax is int x,y,z; (in Java) or var x,y,z; (in Javascript). Variables are case-sensitive. Variables can be local (declared within a function, valid only within) or global (declared outside the functions, ). You assign a value using x=3 or x="Hello" for string variables.

Arrays

 

Data Types

In larger programing languages (like Java, for instance), you have to declare every variable, and when declaring it, decide its data type. The most basic data types are

but many programming languages have even different data types for different types of numbers (integers, reals, ...).

A programming language is typed if operations between variables or expressions only work for specific data types.

Comments

You may want to write notes between your code, describing what you did and why, what the variables mean. For this you use comments, which are hidden for the compiler and not executed.

Control Structures

Selection

Looping

Operators

Functions

Functions are subprograms (modules) that are called by other functions or by events like mouse clicks. They are defined using the schem function fctname(var1,var2, ...){some statements}. Functions with no parameters do still need the parantheses (). A function is called by the expression functionname(name1,name2,...), where name1, names2, ... are existing variables at the time of the call. These values are substituted for var1, var2, ... in the execution of the function. Sometimes a value is returned from the function. In this case, the call has a vlue, and something like x=functionname(name1,name2,...) makes sense. Then you need the return statement inside the function body, like return y;.

Output

To write something on the text, use document.write("some text here"). Then there are dialog boxes which you activate by using alert("some text") or confirm("sometext") or prompt("some text","default value"). The second one has to buttons ("OK" and "cancel"), is of type Boolean and becomes true if the user clicks "OK". The third one prompts you for some input. Here are examples.

Object Oriented Programming (OOP)

... Objects have properties and methods, that mainly are used to change the properties. In strict object-oriented programming you will not be able to change the properties directly, however Javascript allows that.

Events

 Events happen during execution of a program, either by external input (clicking, movings mouse over something, keystroke...) or by the computer (loading a page, ...).


Erich Prisner, 2002-2006