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 5 of 5

Thread: Trying to get an "if" statement to work in a "for loop".

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to get an "if" statement to work in a "for loop".

    This isn't homework, it's a question in a textbook practice example that I can't seem to get right.
    I got it to work with 3 for loops, but I don't know how to get it to work with 2 for loops and a if statement.
    Can someone show me how to get the following :

     
    /* 
    *Write a nested for loop to display the following matrix: 
    *1 2 3 4
    *   1 2 3
    *      1 2
    *         1
    * basically they just increase by 2 spaces every iteration it seems like.
    */
     
     
    //this is as much of a layout of my code i can do
     
    for (int i = 1; i <= 4; i++) {
        for (int j = 1; j <= 5 - i; j++) { //i have a feeling the if statement should go after the 2nd for loop, but i can't get it to work.
            System.out.print(j + " ");
        }
        System.out.println();
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Trying to get an "if" statement to work in a "for loop".

    Why do you need an if statement? You only need 2 loops. Outer loop controls the number of rows. For each row print x number of spaces then have the inner loop print the numbers.

    --- Update ---

    On reflection I assume they want you to use the if statement to decide if a space or a number should be printed.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to get an "if" statement to work in a "for loop".

    I just assumed there needed to be an if statement because I couldn't get each row to increase by " " (2 spaces) each time. How would you increase it by 2 spaces each iteration then? normally this would just print 1 2 3 4 for each row and have 4 columns, but instead its decreasing by 1 number (which i did by j<=5-i) and also shifting to the right every iteration which i can't figure out how to do. Is there a way to do that with just 2 for loops?

  4. #4
    Junior Member Mitch1337's Avatar
    Join Date
    Sep 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to get an "if" statement to work in a "for loop".

    I got this to work using a place holder that would increment each iteration of the outer for-loop.

    ...
    Last edited by copeg; April 1st, 2013 at 11:10 PM. Reason: spoonfeeding

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Trying to get an "if" statement to work in a "for loop".

    @Mitch, this is a problem solving exercise - please refrain from spoonfeeding (for reasons why, please read http://www.javaprogrammingforums.com...n-feeding.html )

    @JAKATAK, try writing this out on paper. If you use nested loops, does one of the loop value account for the number of spaces or space offset?

Similar Threads

  1. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 07:15 AM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  4. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM