Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: why is my for loop using modulo giving me a weird output

by perlynewby (Scribe)
on Jun 28, 2015 at 07:06 UTC ( [id://1132343]=note: print w/replies, xml ) Need Help??


in reply to Re: why is my for loop using modulo giving me a weird output
in thread why is my for loop using modulo giving me a weird output

first, I tried with five just to check out how modulo worked

nothing to do with even number yet. this is a incremental test code for me to learn perl code

I expected the output to look like this:

1 2 3 4

this is because as I go thru the for loop, I goes thru 1,2,3,4,5,6,7,8,9,10,...,15,...20

but the for loop returns "0" 1 2 3 4( not as expected with "0") then it gives me the same output another 4 times. I don't understand the output being given 4 times.

sorry for not being clearer

perhaps this helps explain my question on iteration

  • Comment on Re^2: why is my for loop using modulo giving me a weird output

Replies are listed 'Best First'.
Re^3: why is my for loop using modulo giving me a weird output
by afoken (Chancellor) on Jun 28, 2015 at 07:24 UTC
    I tried with five just to check out how modulo worked

    https://en.wikipedia.org/wiki/Modulo_operation

    I expected the output to look like this:

    1 2 3 4

    Why? Modulo is the remainder of the division. 0 divided by 5 is 0, remaining 0. 1 divided by 5 is 0, remaining 1. 2 divided by 5 is 0, remaining 2. 3 divided by 5 is 0, remaining 3. 4 divided by 5 is 0, remaining 4. 5 divided by 5 is 1, remaining 0. 6 divided by 5 is 1, remaining 1. And so on.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      totally mistaken on modulo.my bad..

      I get it and thank you!

      I guess , what I wanted to get out of this was the quotient NOT the remainder

      so if I divide the number 20 by 5, I want the quotient 4. ok, let me try this again

      then ,next step, is to write a program to find the smallest number that are evenly divided by all the given max_range numbers. ie: like 0-10.

      use strict; use warnings; use integer; use constant number => 5; my %result; for ( my $i=0; $i<=20 ;$i++){ #my ($quo, $rem)= ($i/5, $i%5); #need to understand modulo #print "this is quotient",$quo,"when this is divident $i \n"; #print "this is remainder",$rem,"\n"; if ($i%5==0 && $i!=0){ print "this is the quotient",$i/5,"\n"; print "this is the divident",$i,"\n"; } }

        I think you might benefit from the use of intin order to chop off the fractional component for $quo.

Re^3: why is my for loop using modulo giving me a weird output
by 2teez (Vicar) on Jun 28, 2015 at 07:16 UTC

    Please read Corion again!

    Is there difference between printing, what modulo operator returns and using it to test?

    i.e

    print 4%2; # or if (4%2 == 0) { # blah..blah... }
    Is there a difference?

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      print, you are performing the task print

      while when you use IF then you are checking to see if the statement is true or false, If true then go and do what's inside the brackets. False, then don't go inside the brackets

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1132343]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-18 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found