Mod funtion in c#?


I used to use the mod function in asp to figure out the following while looping through rows in a dataset to display a matrix style grid of products:

if n mod 3 = 0 then
   //start a new row
esle
  //continue adding table cells

How can I acheive this same kind of thing in c# for my asp.net page? I'm displaying a list of products 3 across and so I want to know when I'm on every third one in my for each...

does that make sense? thanks in advance!
animated405Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NetworkArchitekCommented:
Hi animated405,
Use the % character.

Cheers!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
animated405Author Commented:

thats what I thought but it didn't seem to do anything, is this syntax correct?

if((n mode 3) == 0)
   //do this
else
  //do that

sometimes I ask first and look into later so maybe its obvious and I'm too all over the place, million things going on at once...

thanks!
0
animated405Author Commented:
oops, I accidentally put mod in the above, I meant:

if((n % 3) == 0)
  // do this
else
  //do that
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

sajuksCommented:
//this should work
   if(n%3==0)
        {
            //do this
        }
        else
        {
            //do that          
        }
0
tomvergoteCommented:
Why would   if(n%3==0) work better than if((n % 3) == 0)?

I get this result from a command window in visual studio
>? (4%2==0)
true
>? ((4%2)==0)
true
>
0
sajuksCommented:
Both are the same, Do you rememeber the BODMAS rule-  bracketed sub expressions first, then multiplication and division left to right and then addition and subtraction left to right.
Thats the difference between your code and mine
0
tomvergoteCommented:
yes, but I meant this:

Comment from animated405

if((n % 3) == 0)
  // do this
else
  //do that

Comment from sajuks

//this should work
   if(n%3==0)
        {
            //do this
        }
        else
        {
            //do that          
        }

there is no difference, if you put brackets, the % gets executed first and then compared to 0, if you don't put brackets, the leftmost thus % gets executed first and then compared to 0.

I'm not solving any problem here but I was just pointing out that it should work with the brackets too
0
msdixonCommented:
// i don't like thie first one below. i always add {} to my code blocks even if it's only one line and i can get away without them. it's also easier if you have to go back and add a second or third line within the block.

// also, explicitly adding the parenthesis is nonsense. it makes the code unreadable. the modulus will be evaluated prior to comparing it to zero. it's uneccessary. it's the same as:
// if (((3*4) + (5 + (6*4))) < 2)
// i over exaggerated for effect, but you see what i mean. if you have such a complex statement that you need a ton of grouping, it's time to re-evaluate the statement and use a more variables.

if((n % 3) == 0)
  // do this
else
  //do that

// this is what i prefer
// it's clean and simple

        if (n % 3 == 0)
        {
            //do this
        }
        else
        {
            //do that          
        }
0
tomvergoteCommented:
I'm not debating that it was cleaner or not, I was simply questioning why it would work better
0
msdixonCommented:
but it wouldn't work better
0
ozoCommented:
% takes precedence over == regardless of whether it is on the left or the right
0
animated405Author Commented:
I'm sorry I forgot about this. I was actually doing something else wrong, points to the first person that answered me. thanks again...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.