Soon, you should learn how to input text by the user using 'string variables'. The following program is written showing an example of a string variable, prompting the user to input his name, whatsoever:
Program Lesson2_Program1;
Var name, surname: String;
Begin
Write('Enter your name:');
readln(name);
Write('Enter your surname:');
readln(surname);
writeln;{new line}
writeln;{new line}
Writeln('Your full name is: ',name,' ',surname);
Readln;
End.
|
If we take a look at this program, we notice a new variable type : 'String'. Both the name and surname variables are of type string. When the program is run and prompts the user to input his name, the name which is keyed in by the user goes directly to its place in the memory called 'name'. Same occurs to surname. *I'd like to remind you that the variables 'name' and 'surname' are not reserved words, but are used by the programmer as his variables. I could have used 'n' instead of 'name' and similarly 'sname' instead of 'surname'. The two 'idle' writeln's in lines 9 and 10 are used to move on for a new line. In this case, 2 lines are cleared. The next message displays the full name of the user using the above format. If a string variable is required to be displayed on screen, it should be put in between inverted commas and commas if it is concatenated with another message; example:
It could be in the form of: (please note where you must or you must not put the inverted commas following the commas)
*Writeln('Your name is: ',name);
or:
Writeln('Your name is:',name,'. Your surname is ',surname,'.');
*Note that you can even make it this way:
Writeln('Your name is: ',name,'');
BUT you should put the inverted commas properly (underlined) to close the message.
|
Apart from variables, there are also items in the program which are referred to as 'constants'. Unlike variables, constants keep their value or string unchanged for the whole program. Here I have made a program, not so much different from the previous one:
Program Lesson2_Program2;
Var
surname: String;
Const {the reserved word 'const'
is used to initialize constants}
name = 'Victor';
Begin
Write('Enter your surname:');
readln(surname);
writeln;
writeln;
Writeln('Your full name is: ',name,' ',surname);
Readln;
End.
|
In the above program, the constant 'name' is assigned to as 'Victor' and is of type string. However, in other cases, you might have used integer constants (whole numbers), i.e.:
Const
age = 15;
The constant 'age' is a value that could be used whever it is required in a program. Example:
age2 := 15;
age2 := age + 15;
The above example shows an addition of the value of the variable 'age' with the value 15. The value of the constant 'age' remains 15, but the value of the variable 'age2' becomes 30. The assignment statement is not only used for additions, but is also used to assign a variable: text if it is a string variable or a numeric value if it is an integer variable.
Try examine this yourself:
name := 'victor'; age := 15; {also: "age:='15';" BUT in this case, 'age' is an integer variable} writeln('Name:',name,'. Age:',age,'.');
I conclude lesson 2 with another simple program for you to read and think about:
Program lesson2_Program3;
Var PD, Dname, Cmodel : String;
TotalKM, CostPD, TCostPD, Distance : Real;
{real is a decimal (described later)}
begin
TCostPD := 0;
{note that this is called an 'initialisation'.
It is important to initialise variables to 0
so that it is 'refreshed' from the previous
'rubbish' value in the memory.}
Writeln('This program prompts you to '+
+'input the cost per litre of');
Writeln('the petrol/diesel you spend '+
+'in and the average distance you travel');
Writeln('with your car every week. Then '+
+'the computer calculates the total cost');
Writeln('you spend in fuel every week.');
Readln;
Write('Diesel or Petrol?: ');
Readln(PD);
Write('Name Of Driver: ');
Readln(Dname);
Write('Car Model: ');
Readln(Cmodel);
Write('Cost of Diesel/Petrol: (£) ');
Readln(CostPD);
Writeln('Average distance you travel '+
+'with your car every week: (kilometres) ');
Readln(Distance);
Writeln;
Writeln;
Writeln('Name of Driver:',Dname);
Writeln('Car Model:',Cmodel);
Writeln('Diesel/Petrol:',PD);
Writeln('Average distance covered '+
+'every week: ',Distance:1:2,'Km');
Writeln('Cost of ',PD,' per liter: £',CostPD:1:2,'/litre');
Writeln;
Writeln;
TCostPD := Distance * CostPD;
Writeln('Total cost of ',PD,' per week:'+
+'£',TCostPD:1:2); {note this,}
TCostPD := 0;
Writeln('Total cost of ',PD,' per week:'+
+'£',(Distance * CostPD):1:2); {this}
Writeln('Total cost of ',PD,' per week:'+
+'£',Distance * CostPD); {and this - without ':1:2'}
readln;
End.
|
|
0 comments:
Post a Comment