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

Thread: for(int x = 0; x <= 4; x++){

  1. #1
    Junior Member
    Join Date
    Apr 2022
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Angry for(int x = 0; x <= 4; x++){

    Why
    ...
    for(int x = 0; x <= 4; x++){
    System.out.println(x + ' ');
    ---
    Give output
    32
    33
    34
    35
    36?
    see a recurring trio and I guess it has something to do with ASCII (although honestly I still haven't figured out why only 3 when I look at those tables) and I don't understand why it "jumps out" immediately 3 and doesn't go from 0 1 2 if x = 0 and increase it after what is being loaded?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: for(int x = 0; x <= 4; x++){

    The compiler treats the char ' ' as a integer value and adds its value to x.
    If you want a space at the end of the line use " ". But why do you want to add a space at the end of a line?
    If you don't understand my answer, don't ignore it, ask a question.

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

    ijakic (April 8th, 2022)

  4. #3
    Junior Member
    Join Date
    Apr 2022
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: for(int x = 0; x <= 4; x++){

    Thanks for the reply, it was not my intention to add a space at all. I saw a question like this on some java interview question list and I was a little confused and lost when I saw an answer that was completely different compared to my knowledge achieved so far.
    I could not understand where it starts from number 3 and why one triple repeats and the other increases ...? When I put 9 then I get a four at the end.
    I have found this ASCII table and there ' is 39 ... https://www.javatpoint.com/java-ascii-table
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    All of this is obviously still too complicated for me for this level I have reached at the moment.
    Last edited by ijakic; April 8th, 2022 at 03:17 PM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: for(int x = 0; x <= 4; x++){

    If ' ' has a value of 32
    then 0 + 32 is 32
    ...
    then what is 9 + 32?

    not my intention to get a gap at all
    Then why is the ' ' included in the println call?
    If you don't understand my answer, don't ignore it, ask a question.

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

    ijakic (April 8th, 2022)

  7. #5
    Junior Member
    Join Date
    Apr 2022
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: for(int x = 0; x <= 4; x++){

    Honestly, I have no idea. Question number 6.

    https://github.com/learning-zone/jav...ons-answers.md

    It was simply incomprehensible to me where 3 and not 0,1,2,3 appear ...

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: for(int x = 0; x <= 4; x++){

    where 3 and not 0,1,2,3 appear ...
    To get that output: 0,1,2,3 there would need to be
    a "," printed after every number
    and the print() method used instead of the println() method which ends the line that is printed and moves to the next line.
    With the println method, each number would be on its own line:
    3
    4
    5
    ...

    To get 0,1,2,3 change the method to print() and change the ' ' to ","
    If you don't understand my answer, don't ignore it, ask a question.

  9. #7
    Junior Member
    Join Date
    Apr 2022
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: for(int x = 0; x <= 4; x++){

    Thanks for helping me. I will keep learning Java, I have to admit, all this it confuses me a bit, but I believe that everything will fall into place.

  10. #8
    Junior Member
    Join Date
    Apr 2022
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Cool Re: for(int x = 0; x <= 4; x++){

    Haaa finally understood. I had to sort out in my head how all this works and it takes its time.

    step 1. go to char to ASCII and see how the spacing is essentially 32 numeric value

    step 2. repeat the java learning area in which X = 0 is just a sign of where the counting starts and X <= 4 means that there will be a total of 5 steps 0,1,2,3,4

    step 3. part x + ' ' means that this numeric value will be incremented as many times as above above specified by the for loop and that's it.

    Now I sound like a teacher to myself :-)