Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,296 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,415 people online right now. Registration is fast and FREE... Join Now!




help on for statement and % operator

 
Reply to this topicStart new topic

help on for statement and % operator, and a bonus question on array

csgwms
3 Sep, 2008 - 07:26 PM
Post #1

New D.I.C Head
*

Joined: 7 Aug, 2008
Posts: 10

if given a set of ints such as int[] numbers = {5, 6, 7, 8};
what would the following:
for (int i = 0; i < numbers.length; i++) do?
and also what does %= mean? i read about this on an online java course but im so confused!

Edited for a more meaninful title

This post has been edited by pbl: 3 Sep, 2008 - 07:49 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Help On For Statement And % Operator
3 Sep, 2008 - 07:40 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
QUOTE(csgwms @ 3 Sep, 2008 - 08:26 PM) *

if given a set of ints such as int[] numbers = {5, 6, 7, 8};
what would the following:
for (int i = 0; i < numbers.length; i++) do?
and also what does %= mean? i read about this on an online java course but im so confused!


for (int i = 0; i < numbers.length; i++)

means:

lets start with i = 0
while i < the lenght of the array numbers (which is 4)
do something and then increment i by 1
so it will will pass through your array element by element

CODE

int [] numbers = {5, 6, 7, 8};

for (int i = 0; i < numbers.length; i++)
  System.out.println(numbers[i]);

would print

5
6
7
8



The % operator is the modulo operator... the reminder of an integer division

10 % 5 = 0
12 % 5 = 2
20 % 19 = 1
7 % 3 = 1
12 % 10 = 2

Hope this helps

This post has been edited by pbl: 3 Sep, 2008 - 07:43 PM
User is offlineProfile CardPM
+Quote Post

csgwms
RE: Help On For Statement And % Operator
4 Sep, 2008 - 02:17 PM
Post #3

New D.I.C Head
*

Joined: 7 Aug, 2008
Posts: 10

thanks alot this really helped!
i have one more question though, if i have some sort of element after this array such as variable[i], what does this do? why is the i put in brackets after this certain variable?
User is offlineProfile CardPM
+Quote Post

pbl
RE: Help On For Statement And % Operator
4 Sep, 2008 - 02:46 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
QUOTE(csgwms @ 4 Sep, 2008 - 03:17 PM) *

thanks alot this really helped!
i have one more question though, if i have some sort of element after this array such as variable[i], what does this do? why is the i put in brackets after this certain variable?

Yeah... it is really back to school...
OK
You can have single variables into which you can put values

int a,b,c;

a = 10;
b = 20;
c = 30;

or you can have an "array" of variables which means a bunch of these variables. They are indexed between [] and the first one is [0] the last one [array.length-1]

So

int[] x = new int[5]; // makes an array of 5 x
x[0] = 10;
x[1] = 20;
x[2] = 30;
x[3] = 40;
x[4] = 50;
x[5] = <---- NO NO there are 5 x from x[0] to x[4]

So if you have a program to play a card game you are not to make
int cardValue1, cardValue2, cardValue3, ..... cardValue52;
but
int[] cardValue = new int[52];

then you can reference cardValue[i] where "i" is between 0 and 51

Hope this helps

P.S.
don't say you have "one" more questions... I am sure a lot of other ones are coming icon_up.gif
but we are here to help


User is offlineProfile CardPM
+Quote Post

csgwms
RE: Help On For Statement And % Operator
4 Sep, 2008 - 06:24 PM
Post #5

New D.I.C Head
*

Joined: 7 Aug, 2008
Posts: 10

wow thanks alot! sorry about all the stupid questions sad.gif im new to this stuff
User is offlineProfile CardPM
+Quote Post

pbl
RE: Help On For Statement And % Operator
4 Sep, 2008 - 07:00 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
QUOTE(csgwms @ 4 Sep, 2008 - 07:24 PM) *

wow thanks alot! sorry about all the stupid questions sad.gif im new to this stuff

there are no stupid question, don't worry. Our pleasure to answer them
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 06:01AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month