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: Can't seem to understand how this output came

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Location
    India
    Posts
    18
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Unhappy Can't seem to understand how this output came

    so I am a new to java and coding and doing some basic stuff please don't be hard on me if i am stupid
    public class forloop {
    	public static void main(String[] args) {
    int N,M;
    for (N=6; N>=0; N--) 
    { for (M=1; M<=N-1;M++)
    System.out.print("*"); 
    System.out.println(""); 
    }
    }
    }

    the output is
    *****
    ****
    ***
    **
    *

    It's supposed to be this way but when I practically tried to calculate the code I wasn't able to get it right
    For the 1st run of loop N=6 and M=1 and Prints * (1st line)
    2nd run of loop N=5 and M=2 and prints *(1st line)
    3rd run of loop N=4 and M=3 and Prints * (1st line)
    4th run of loop N=3 and M=4 and it Printsln " " (goes to 2nd line) [as the Value of M needs to be lesser than N to print a new "*" on the same line]
    so the output seems to be
    ***
    (and the switches to 2nd line and continues the calculation)

    I am having a hard time explaining what I want to convey but if anyone gets it can you please guide me on where I am going wrong while calculating in real life?
    Last edited by kukupie123; March 26th, 2019 at 08:07 AM. Reason: to make it easier for the readers

  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: Can't seem to understand how this output came

    One problem with the code is the formatting. Poor formatting makes the code hard to read and understand.
    There should not be any code on a line following a { or before a }
    The statements controlled by a for statement should be enclosed by {}s
       for(int x=1; x < 4; x++) {
         // some statement(s)
       }

    Please edit your post and wrap your code with code tags:

    [code]
    **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
    Mar 2019
    Location
    India
    Posts
    18
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Can't seem to understand how this output came

    Sorry for that I am a total novice in here, I fixed it.
    Would you mind explaining me how we got that output if you can and free enough to deal with my problem?

  4. #4
    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: Can't seem to understand how this output came

    The code has lost all its formatting. Logically nested statements should be indented. See the code sample I posted.
    The statements should not all start in the first column.
    The statements controlled by the for statement are not wrapped in {}s.
    There should not be a statement on the same line following a {

    Proper formatting makes code much easier to read and understand.

    The way to see what the code is doing is to use paper and pencil to write down the values of the variables at the end of the line for each time a line is executed.
    Write the values in columns with each column showing the values on one iteration of the loop.
    for (M=1; M<=N-1;M++) // N = 6, M = 1 || N = 5, M = 1 etc
    Continue for all the source lines.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    kukupie123 (March 26th, 2019)

Similar Threads

  1. Replies: 5
    Last Post: September 7th, 2014, 08:25 AM
  2. [SOLVED] Java runtime get result output from prompt problem with a larger output in type
    By kingwang98 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 14th, 2014, 08:52 AM
  3. Please Help me to understand
    By Theillusion in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 17th, 2013, 03:09 AM
  4. how could I output to a text area the output of a method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 12th, 2012, 07:49 PM
  5. Help me to understand this
    By Madhushan in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2011, 08:47 AM

Tags for this Thread