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: Help understanding for-loop

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help understanding for-loop

    I am fairly new to programming, I just had a quick question as to why the fallowing code prints the way it does. Also I am new to this forum and any direction to online resources would be greatly appreciated.

    public class print{




    public static void main (string[] args){




    int i=1;

    int j;




    while (i<=7){

    for(j=1;j<=i;j=j+3)

    System.out.print("*");

    i=i+2

    System.out.println();

    }

    }

    }


    the output is :
    *
    *
    **
    ***


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help understanding for-loop

    It helps for understanding code if it is properly formatted (nested statements indented) and the code inside of loops is enclosed in {}s.
    Please fix the loops and nesting and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help understanding for-loop

    To "Debig" sutch snipplets put more system prints inside the code.
    In a nitshell:
    On each while block method execution it will print as many *, as for loop will iterate times.

    Possible values for j are [1,4,7]
    Possible values for i [1..7]
    1. Loop *
    i=1 j=1 (because 1 + 3 = 4> 1)
    2. Loop *
    i=3
    j=1 (because 1 + 3 = 4> 3)
    3. Loop **
    i=5
    j=1, j=4
    4 .Loop ***
    i=7
    i=1,j=4,j=7

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help understanding for-loop

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    The Oracle Tutorials are a great place to start.

Similar Threads

  1. Difficulty understanding code with for loop?
    By chakana101 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 25th, 2014, 11:35 PM
  2. Not Understanding Polymorphism
    By superjacko in forum Java Theory & Questions
    Replies: 5
    Last Post: July 28th, 2013, 11:47 PM
  3. Need help understanding Java
    By Jafke104 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 30th, 2013, 10:52 PM
  4. Helping Understanding???
    By SuperDad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2012, 06:53 PM
  5. Help me Understanding Please...
    By Jabetha in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 17th, 2011, 01:55 PM