Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8796179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:33:45+00:00 2026-06-13T23:33:45+00:00

I’m trying to read the output of this code, but it simply just doesn’t

  • 0

I’m trying to read the output of this code, but it simply just doesn’t make sense to me.

Even after learning that a loop without braces only loops through the first line, the output still makes no sense, at all. Some numbers do, but others just don’t.

My code:

int n = 8;
int i = 1;
int j = 1;

j = n + 2;

System.out.println (n + " " + i + " " + j );

while (n > 1)
{
    n = n/2;
    for (i = 2; i <= n; i = i+2)
        System.out.println (n + " " + i + " " + j );

    j++;
}
System.out.println (n + " " + i + " " + j );

And it outputs:

8 1 10
4 2 10
4 4 10
2 2 11
1 2 13

I get the 8-1-10
and the 4-2-10

but anything after that, I’m stumped, I don’t get how the computer calculates the rest.

Would someone mind going through the rest of the output with me, step by step?

Thank’s in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:33:46+00:00Added an answer on June 13, 2026 at 11:33 pm

    No braces means that the loop affects only the next statement that follows the loop.

    So

    for (i = 2; i <= n; i = i+2)
    System.out.println (n + " " + i + " " + j );
    

    is equivalent to

    for (i = 2; i <= n; i = i+2)
    {
        System.out.println (n + " " + i + " " + j );
    }
    

    Usually, indentation is used in those cases to make the code more comprehensible, like this:

    for (i = 2; i <= n; i = i+2)
        System.out.println (n + " " + i + " " + j );
    

    EDIT: Now, this is the actual answer to the question. It all depends on the different iterations the loop does and how do the variables get incremented.

    int n = 8;
    int i = 1;
    int j = 1;
    
    j = n + 2; //This means that j takes the value 10.
    
    System.out.println (n + " " + i + " " + j ); // 8 1 10 So far, so good.
    

    Now, on with the iteration:

    while (n > 1)
    {
        n = n/2;
        for (i = 2; i <= n; i = i+2)
            System.out.println (n + " " + i + " " + j );
    
        j++;
    }
    

    For the first iteration, we have n=8 i=1 j=10, so since n > 0 is true the loop takes place.

    n = n / 2; //n = 4
    

    Then, the for (note that it just assigns the value 2 to i):

    for (i = 2; i <= 4; i = i+2) //Since n = 4
    

    Since n = 4, the only values that i can take are 2 and 4, then the prints are:

    4 2 10
    4 4 10
    

    After that, j is incremented by one, making it j = 11. The condition for the while is met again because n = 4. n = n/2 makes n take the value 2, so it enters the while again. Let’s take a look at the for again:

    for (i = 2; i <= 2; i = i+2) //Since n = 2
    

    This time, the only value that i can take is 2 (note that the value of i is reset again to 2 while starting the iteration), and that’s the print you get.

    2 2 11
    

    Before iterating again, j++ makes j have the value 12.

    On the final iteration, n = n/2 results in n = 1 since n is an int but this is done inside the while, so it enters again. Now that n = 1 the loop looks like this:

    for (i = 2; i <= 1; i = i+2) //Since n = 1
    

    i is set to 2 and the condition for the for is not met (2 <= 1 is false). Then there is no print this time, yet j is incremented to 13 at the end of the while.

    In the next iteration you have n = 1, that means that the while‘s condition is not met so the iteration breaks. Finally you have n = 1, i = 2 and j = 13. That’s the last print you get.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.