Читайте также: |
|
There are some important elements here:
In Java, besides "/*" and "*/" characters, there are other ways of commenting code, for example by using "//" characters.
Firstly, the text between "/*" and "*/" characters is treated as a comment, which is not part of the program and does not influence its execution. It is a good idea to comment parts of program to explain their meaning. Of course one shouldn't exaggerate: obvious instructions are not commented (in the above example comments describe ways of using them).
Blue color distinguishes keywords representing instructions of the language. A program is a text consisting of symbols - sequences of characters separated by separators (spaces, commas, semicolon, brackets). Each symbol except for comments has specific meaning for the program and can't appear in arbitrary position. Some - precisely specified - symbols (or words) specify instructions to execute (for example checking of a condition, iteration over a loop).
Generally, each sequence of program symbols terminated with semicolon (";") is an instruction.
Therefore let's get used to terminating instructions with semicolon (although this stipulation is not essential in REXX, it is in Java).
In many programming languages (in Java too) input and output of data from/to shell (command prompt) is realized by means of functions. REXX allows using of such functions as well as some special instructions.
The first line in the above program contains such instruction: say displays the given string at the command prompt. The string is put in quotation marks literally. For this reason it is called literal.
Some strings in a program denote variables. A variable stores data and always has a name. In the first program there is variable named name. It stores the name read from command prompt.
Reading of data from the command prompt is done by linein() function. Functions are isolated pieces of code which perform some well defined tasks (marked green in the source code). They are called by adding parentheses at the end of function's name. Between parentheses is placed information passed to the function, called argument(s) (in this case there is none). Call of linein() function results in passing control to other block of code - function's body (in this case defined in REXX - such function is called "built in" and its code is unknown for the programmer). This code waits for input data to appear at the command prompt. After a user has pressed the ENTER key, it reads the input line and returns it to the place in code where the function has been called. The returned value is assigned (by means of "=" character) to the variable name. Thus, if a user answering question "What is your name?" has entered the text "Alice" and has pressed the ENTER key afterwards, the string "Alice" is stored in the variable name. If the user hasn't written anything but has just pressed the ENTER key, the variable name stores an empty string which can be expressed in a program with the sequence "" (quotation marks with no character between them).
Special statements in any programming language are flow control statements. They influence execution of the program. One of such instructions is if statement, which tests validity of some condition and based on that makes decision which instructions are to be executed if the condition holds.
Our program checks, whether the variable name stores an empty string (if name = "") and if it does, only the word "Hello" is displayed (then say "Hello!"). In other case (else) the word "Hello," followed by the string stored in the variable name and the "!" character is displayed. (else say "Hello, " name "!").
Program's last line displays the current time using the built-in function time().
While writing a program one must take into account syntactic rules of the language. They determine which strings are permissible. For example names of variables cannot be arbitrary strings (a name "-123" is not allowed). They specify how statements are build too (if name = "" else say "Hello!") and require that parentheses and quotation marks must be matched (say "Hello!). All syntactic details are checked during compilation of a program (if it is written in a compiled language) or interpretation of program (if it is written in an interpreted language). If a program contains syntactic errors it won't compile nor execute.
Compilers and interpreters display information about syntactic errors in the program. It allows tracing the cause of an error and find the place in the source code where an error occurred. That's why one should carefully read all information displayed during compilation, especially when there are errors.
For example parsing the following code:
say "What is your name
the REXX interpreter displays message:
Error 6 running "Greet.rexx", line 3: Unmatched "/*" or quote
Error 6.3: Unmatched double quote (")
or similar. The error, located in line 3, consists in unmatched quotation mark.
Thanks to this information the error can be quickly found and corrected.
Дата добавления: 2015-11-16; просмотров: 57 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
Fundamentals of programming I | | | Data in a program (variables and literals) |