Recent

Author Topic: Memo Program  (Read 1860 times)

godik

  • New Member
  • *
  • Posts: 23
Memo Program
« on: November 17, 2018, 08:00:25 pm »
procedure TForm1.Button1Click(Sender: TObject);
  var i,n:integer;
begin
  n:=StrToInt(Edit1.Text);
  for i:=1 to n do if i mod 2=0
  then memo1.lines.add(IntToStr(i));
end;

end.
     

So Good Evening to everyone who is reading this I wanna ask a simple question how does this line work somehow I dont understant it.

 for i:=1 to n do if i mod 2=0

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Memo Program
« Reply #1 on: November 17, 2018, 08:08:12 pm »
Suppose that n equals 10
Then we iterate the loopcounter i from 1 to 10 and if the condition (i mod 2 = 0) is true we add the number i to the memo (after converting it to a string, because the memo only accepts strings).
The result should be:
2
4
6
8
10

Bart

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Memo Program
« Reply #2 on: November 17, 2018, 08:46:58 pm »
It may be easier to understand if it were well formatted:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i, n: integer;
  4. begin
  5.   n := StrToInt(Edit1.Text);
  6.   for i := 1 to n do { loop i from 1 to n }
  7.     if i mod 2 = 0 then { Is "i" an even number? if so... }
  8.       memo1.lines.add(IntToStr(i)); { ... show it on memo1}
  9. end;
  10.  
  11. end.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

godik

  • New Member
  • *
  • Posts: 23
Re: Memo Program
« Reply #3 on: November 17, 2018, 10:05:45 pm »
Thank you guys appreciate it  8)

 

TinyPortal © 2005-2018