Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Lecture 1.docx
Скачиваний:
1
Добавлен:
01.07.2025
Размер:
704 Кб
Скачать
☆

2.5. Summary

This lecture presents (using simplified and modified version of the REXX language):

  • elements of the syntax of a program

  • using literals and variables as a means of storing data

  • operations on data

  • writing down and running programs

The student should pay special attention to the following important notions:

  • literals

  • variables

  • strings

  • operators

  • expressions

  • instructions, statements

  • precedence of operators

  • assignment operator

Having read this lecture, the student should acquire the ability to write simple programs in modified REXX, which for example perform arithmetical operations on some data.

2.6. Exercises

All exercises should be done as programs written in modified REXX.

  1. Modify greeting program (the first example program in this lecture) to make it display each string surrounded by a border made of '|' and '*' characters.

  2. Write a program, which displays a template of an application letter. In the upper left corner of the letter the sender data should be placed, whereas in the upper right corner - that of addressee. The letter has to start with some official introduction, and has to be signed with the full name of addressee. Try to design the program so, to make any modifications of the data (addressee or header) easy.

  3. Write a program for converting temperature data given in Fahrenheit degrees to Celsius.

Lecture 3 Fundamentals of programming II

Continuing our presentation of programming fundamentals, we will discover how to make decisions in a program and how to repeatedly execute instructions. We will also learn about arrays and functions.

3.1. Making decisions

A sequence of instructions executed by a program often depends on some conditions. Checking these conditions, a program makes decisions which instructions to execute. For making decisions mainly the if statement is used. It has the following form (in Java the word "then" is omitted):

    if ( condition ) then instruction ;

where:

  • condition - an arbitrary relational or logical expression

  • instruction - an instruction which is to be executed if the condition is fulfilled

A very useful extension to the above instruction is the if-else statement.

        if ( condition ) then instruction1;     else instruction2; which works as follows: if the condition is fulfilled, the instruction1 is executed, otherwise the instruction2 is executed.

Note: in Java the word then is omitted.

Let us consider some simple examples.

c = 1; if (a < b) then c = a + b; d = c + 1;

If a = 10, b = 11 (the condition is true) the variable c gets the value of 21 and the variable d gets the value of 22 If a = 2, b = 1 (the condition is false), instruction c = a + b is not executed and the variable d gets the value of 2.

if (a = b) then c = 1; else c = 2; d = c + 1;

If a = b (condition is true) c gets value 1, the instruction after "else" is not executed and d has value 2. If a is not equal to b (condition is false) the "else" instruction is executed (c = 2) and as result of d = c+1, d gets the value of 3.

if (a > b) then max = a; else max = b;

Finds the maximum of two numbers: a and b, and assigns it to the variable max.

if (a > b) then txt = "a> b"; else if (a <b) then txt = "a < b"; else txt = "a = b"; say txt;

Displays message informing whether a is greater than, less than or equal to b.

Now we are ready to write the program implementing the algorithm for tax calculation discussed earlier.

/* Calculation of a tax */

say "Give your income:";

doch = linein();

if (doch > 74048) then pod = 17048.44 + 0.4 * (doch - 74048);

else if (doch > 37024) then podk = 6541.24 + 0.3 * (doch - 37024)

else pod = 0.19 * doch - 493.32;

say "The tax:" pod;

The instruction after the "if" or "else" may be a block consisting of a number of instructions.

Note: in Java the braces ({ and }) are used to group instructions in a block.

Indeed, often after fulfilling some condition we would like to execute many instructions, not just one. Instructions are grouped with the help of keywords: do and end.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]