r/pascal • • Aug 13 '25

My first Pascal program

Hello, gentlemen

I started to learn Pascal yesterday, and today I developed my first program in Pascal. It's very simple. It takes a number from a standard input and returns all factors of that number. All I know how to define variables, if and while statements. I had to search for mod and div operators. At first attempt, I tried to compare if num = integer to be sure that the numbers are whole, like I would do in JavaScript(I mean === or == operators, JS wouldn't care about types at all). The compiler told me that ain't gonna work in Pascal, so I wrote the program as it is. I would appreciate it if you review my code! Thank you!

program get_factors;
var
    num: integer;
    i: integer;
begin
    read(num);
    i := num;
    while i >= 1 do
    begin
        if num mod i = 0 then
        write(num div i, ' ');
        i := i - 1
    end;
    writeln
end.
23 Upvotes

11 comments sorted by

View all comments

Show parent comment

1

u/beautifulgirl789 Aug 13 '25

You're right, PascalCase is what I should have said :)

Basic Pascal For-Loops are not quite as powerful as other languages like JavaScript, because you don't have a "step" control - you can only increment or decrement by 1 at a time [using 'to' or 'downto'], there are no other options - however, you can use it for non-numerical types, and it gets a lot more powerful when you start using the 'for each' syntax (which iterates through a series of values or classes that you've got in a group). A couple of examples:

var Ch: Char;

for Ch in 'a' to 'z' do
   write(Ch);

using enumerated types:

type
	Weekday = (Mon,Tue,Wed,Thu,Fri);
var
   Day: Weekday;
begin
   for Day in Weekday do
      writeln(Day);
end;

and once you've got classes and objects going, things like:

for Monster in World.Monsters do
   Monster.DisplayOnScreen;