Board index » delphi » Newbie question on the 'mod' operator

Newbie question on the 'mod' operator

Hello,

Can someone tell me what's wrong with the following statement?

{ i and j are integers}

j := (i mod 10);

Thanks!

Songmuh

 

Re:Newbie question on the 'mod' operator


Quote
>Can someone tell me what's wrong with the following statement?

>{ i and j are integers}

>j := (i mod 10);

Nothing.

In a meaningful context, of course. <g>

Maybe you could give a *little* more information
about your problem...
--
Jeremy Collins

Re:Newbie question on the 'mod' operator


Quote
songmuh wrote:

> Hello,

> Can someone tell me what's wrong with the following statement?

> { i and j are integers}

> j := (i mod 10);

> Thanks!

> Songmuh

Songmuh,

Nothing is wrong with the statement.  Remember that MOD stands for
modulus and is the remainder of the resulting division.  In other words,
the resulting remainder of i divided by 10 is assigned to j.

Example:
j := (5 mod 10);  // j = 5 since 10 doesn't evenly divided in 5
                  // so your remainder is 5
j := (20 mod 10); // j = 0 since 10 evenly divides into 20
j := (13 mod 10); // j = 3 since 10 goes into 13 once with a remainder
of 3

Later,
Dave

Re:Newbie question on the 'mod' operator


On Sun, 08 Feb 1998 20:22:20 GMT, "songmuh" <song...@MCI2000.com>
wrote:

Quote
>Hello,

>Can someone tell me what's wrong with the following statement?

>{ i and j are integers}

>j := (i mod 10);

'i' is on strike and 'j' has eloped with the neighbor's daughter...

No, seriously, there is nothing wrong with that statement (by itself).
Please post a little more of the code surrounding that statement and
we should be able to find out what's ailing you.

Re:Newbie question on the 'mod' operator


Hi,

Looks like no one identified my previous post as a mistake, but
this is the place where Delphi flagged it as an error and would
not continue. I am posting more code surrounding that statement:

var
        i, j: integer;

begin
        {...}

        for i := 1 to 255 do (
                j := (i mod 10);
                if (k := 0) then ...

        {...}
end;

I have also tried to combine the above two statements into one:

        for ... do (
                if ((i mod 10) := 0) then ...

Now the if statement is marked as an error. Could it be that
the for loop is wrong? But it worked somewhere else.

Very frustrated!

Re:Newbie question on the 'mod' operator


In article <01bd34e8$f9012220$db4e37a6@g2k486>, song...@MCI2000.com says...

Quote

>Hi,

>Looks like no one identified my previous post as a mistake, but
>this is the place where Delphi flagged it as an error and would
>not continue. I am posting more code surrounding that statement:
[...]

>        for i := 1 to 255 do (
>                j := (i mod 10);
>                if (k := 0) then ...

>        {...}
>end;

I think the problem is the ( after 'do'.  In pascal the block
(compound statement) indicators are begin and end, not ( and ).  Also, I doubt
K := 0 is what you intended to put in the if statement.  In pascal the equality
test operator is =, not := (assignment).

for i := 1 to 255 do begin
  j := (i mod 10);
  if (k = 0) then {...}
end;

--
Chris Hill
hil...@cs.purdue.edu

Re:Newbie question on the 'mod' operator


Quote
In article <01bd34e8$f9012220$db4e37a6@g2k486>, "songmuh" <song...@MCI2000.com> wrote:
>Hi,
>Looks like no one identified my previous post as a mistake, but
>this is the place where Delphi flagged it as an error and would
>not continue. I am posting more code surrounding that statement:
>var
>        i, j: integer;
>begin
>        {...}

>        for i := 1 to 255 do (
>                j := (i mod 10);
>                if (k := 0) then ...
>        {...}
>end;

Ooooops - I hope you're not trying to use ':=' to test equality - := is *only*
for assignment when you're *settings* something to equal something else.  
Otherwise, use '='.  And no, you can't have more than one assignment in the
same statement, so something like 'i := j := 0;' would also not work.

If that's not it, get back to us :)

  -- Ritchie Annand

(Anti-spam measure - e-mail address contains neither hyphens nor the
words 'at' or 'dot' :)

Re:Newbie question on the 'mod' operator


Quote
songmuh wrote:
> Looks like no one identified my previous post as a mistake, but
> this is the place where Delphi flagged it as an error and would
> not continue. I am posting more code surrounding that statement:

> var
>         i, j: integer;

> begin
>         {...}

>         for i := 1 to 255 do (
>                 j := (i mod 10);
>                 if (k := 0) then ...

>         {...}
> end;

Delphi is not C. You can't write "if (k := 0) ...", because an
assignment don't 'have a value'. You don't need to put parens around the
test in if statements.

And, in both languages, a ( does not open a block. Try

Quote
>         for i := 1 to 255 do
>           begin
>           j := (i mod 10);
>           k := 0;
>           if k <> 0 then {do something every time} ;
>           end;

--

http://www.midnightbeach.com - for info about me, my work, my writing,
                               and my homeschool resource pages.

Other Threads