### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Recent

#### godik

• New Member
• Posts: 23
« on: October 31, 2018, 05:05:00 pm »
procedure TForm1.Button1Click(Sender: TObject);
var a,b:real;
begin
a:=StrToInt(Edit1.Text);
b:=StrToInt(Edit2.Text);
if a>b then (Label3.Caption)=FloatToStr(a)
else (Label3.Caption):=FloatToStr(b);

end;

end.

I dont know what to do

#### BasicOne

• New member
• Posts: 8
« Reply #1 on: October 31, 2018, 05:14:49 pm »
why is Label3.Caption in brackets?

remove the brackets and replace = by :=

if a>b then Label3.Caption:=FloatToStr(a)
else Label3.Caption:=FloatToStr(b);

#### lucamar

• Hero Member
• Posts: 2962
« Reply #2 on: October 31, 2018, 05:18:03 pm »
[... some code ...]

I dont know what to do

Neither do we, since you don't say  what's your problem.
Anyway, here is a more correct version of your code:

Code: Pascal  [Select][+][-]
1. procedure TForm1.Button1Click(Sender: TObject);
2. var
3.   a, b: real;
4. begin
5.   a:=StrToFloat(Edit1.Text);
6.   b:=StrToFloat(Edit2.Text);
7.   if a > b then
8.     Label3.Caption := Edit1.Text
9.   else
10.     Label3.Caption := Edit2.Text;
11. end;
12.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

• Hero Member
• Posts: 10276
« Reply #3 on: October 31, 2018, 06:01:02 pm »
Maybe we should introduce a full set of assignment operators between strings and whatever simple type, something like this (untested):
Code: Pascal  [Select][+][-]
1. {\$mode objfpc}{\$H+}
2. uses sysutils;
3.
4. operator := (const a:double):string;
5. begin
6.   result := floatToStr(a);
7. end;
8.
9. operator := (const a:string):double;
10. begin
11.   result := StrToFloat(a);
12. end;
13. var
14.   a:double = 0.001;
15.   b:string;
16. begin
17.   b := a;
18.   writeln(b);
19.   a := b;
20.   writeln(a:6:6);
21. end.

Note: not tested except for brain compiler. Which is still in Alpha...
Brain compiler apparently has error detection. first version was the other way around.

and real compiler agrees

« Last Edit: October 31, 2018, 06:28:17 pm by Thaddy »
I am more like donkey than shrek

#### Bart

• Hero Member
• Posts: 3854
« Reply #4 on: October 31, 2018, 06:43:29 pm »
Yeah, back to BASIC.

Bart

#### godik

• New Member
• Posts: 23
« Reply #5 on: November 02, 2018, 10:47:39 pm »
I dont know if I can make here another subject on this topic but if I can

procedure TForm1.Button1Click(Sender: TObject);
var N,y,amount:integer;
begin
N:=StrToInt(Edit1.Text);
amount:=1;
for y:=1 to N do begin
suma:=amount*y;

end;
edit2.text:=IntToStr(amount);

end;

end.

I dont really understant what is the difference between real and integer in this program and while doing this program i have been really struggling with this line:  for y:=1 to N do begin ( I dont understant what role is the begin doing here)

any help appreciated.

#### Bart

• Hero Member
• Posts: 3854
« Reply #6 on: November 02, 2018, 11:08:13 pm »
I dont really understant what is the difference between real and integer in this program

"This" program does not use any real AFAICS.
An integer is a number that has no fractionional part. E.g -10, 2, 1024
A real number is a value of a continuous quantity that can represent a distance along a line. (In mathematics this also includes all integer numbers, but in computers we mean floating point numbers like 3.14, 99.99 etc.)

The type "real" is rather ancient.
Nowadays we use single, double or extended (the latter is not supported on all platforms though)

and while doing this program i have been really struggling with this line:  for y:=1 to N do begin ( I dont understant what role is the begin doing here)

The for syntax is like:
Code: Pascal  [Select][+][-]
1.   for loopcounter := startvalue to endvalue do statement;

"statement" can be a single statement like writeln(loopcounter), or a compund statement, a group of statements separated by semicolons (;) and surrounded by the keywords begin and end.

It is also perfectly OK to have a single statement enclosed in begin..end, like in your example.

As my explanation of the syntax shows, in your example the begin..end in the for loop can be omitted.

Bart
« Last Edit: November 03, 2018, 07:30:18 pm by Bart »