Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: how does this work?

  1. #1
    Junior Member somebodyonearth's Avatar
    Join Date
    Nov 2011
    Posts
    14
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how does this work?

    can somebody tell me how does this code work i can get it

        public static void main(String [] args) {
        	int row = 10;
        	int column ;
        	while (row >=1){
        		column=1;
        		while(column<=10){
        			System.out.print (row % 2== 1? "<" : " >");
        			++column;
        		}
        		--row;
        		System.out.println ();
        		    	}
        }
    Last edited by helloworld922; March 10th, 2012 at 01:28 PM. Reason: please use [code] tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,343
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: how does this work?

    Please explain what you mean by "work".
    Does the code compile and execute?
    What is printed out when the code is executed?

    Most of the code is very simple.
    What line(s) of the code are a problem for you?
    Do you know about the tertiary operator? The ? and :
    It is like an if else statement in a small method:
    if(row %2 == 1) return "<" else return ">"

  3. #3
    Junior Member somebodyonearth's Avatar
    Join Date
    Nov 2011
    Posts
    14
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how does this work?

    yes i do but i dont get this part ( row %2==1 ) i dont understand how does it run or work

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,343
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: how does this work?

    % is the modulus/remainder operator.
    To see how it works, write a small program with a loop and print out the values of the loop index when you use that operator. Something like this:
    int var = 3;
    for (int i=0;i,20;i++){
    S.o.println(i %var);
    }

    Change the values of var to see what happens with different values

Similar Threads

  1. Does this work?
    By marmanq in forum What's Wrong With My Code?
    Replies: 17
    Last Post: February 20th, 2012, 10:51 AM
  2. Why doesn't this work!
    By Alex-Green in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2012, 04:25 AM
  3. I cant work it out!!!
    By tuts73 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: January 18th, 2012, 10:16 AM
  4. Why doesn't this work?
    By mailman in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2012, 11:19 PM
  5. Why does this not work?
    By Spidey1980 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 12th, 2011, 10:47 AM