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 8379613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:14:13+00:00 2026-06-09T16:14:13+00:00

PrintStream out = System.out; int sum = 0; for(int i = 0; i<5; i++)

  • 0
PrintStream out = System.out;

    int sum = 0;
    for(int i = 0; i<5; i++)
        for(int j=i; j<5; j++)
            sum++;
    out.println(sum);

outputs: 15. i dont know how this gets 15. i tried solving it in my head, i keep getting 25.

If we ignore the second for loop, and put sum++ in the first for loop, you get 5. But how do we get 15 from the 2nd for loop?

PrintStream out = System.out;

    int sum = 0;
    for(int i = 0; i<5; i++)
        for(int j=i; j<5; j++);
            sum++;
    out.println(sum);

outputs: 1. What happens if you put ; after for statement. does it stop?

  • 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-09T16:14:15+00:00Added an answer on June 9, 2026 at 4:14 pm
    i = 0 => j goes from 0 to 4 => sum = 5  
    i = 1 => j goes from 1 to 4 => sum = 5 + 4 = 9  
    i = 2 => j goes from 2 to 4 => sum = 9 + 3 = 12  
    i = 3 => j goes from 3 to 4 => sum = 12 + 2 = 14  
    i = 4 => j goes from 4 to 4 => sum = 14 + 1 = 15 
    

    your second code snippet does not compile as it is.

    EDIT

    Now that your second snippet compiles:

    for(int i = 0; i<5; i++)
        for(int j=i; j<5; j++);
            sum++;
    

    is equivalent to (and this is why you should always use braces):

    for(int i = 0; i<5; i++) {
        for(int j=i; j<5; j++) {
        }
    }
    sum++;
    

    That explains why it outputs 1.

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

Sidebar

Related Questions

I've been trying to redirect System.out PrintStream to a JTextPane. This works fine, except
Let's say I have this: PrintStream out = System.out; Scanner in = new Scanner(System.in);
In System.out.println ,'out' is a member variable of System class and it's also an
I am new to java, and to make clear of System.out, i read relevant
I was looking at someone's code and saw that he repeatedly declared PrintStream out
I'm trying to intercept System.out and System.err, but maintain the ability to write to
The first part of this Frankenstein-ed Java works perfectly, however the second part outputs
System.out.printf(%s%13s%\n, TarrifType, AnnualCost); System.out.printf(%s%d.%n, String 243.08); Exception in thread main java.util.UnknownFormatConversionException: Conversion = '
Whenever we write any statement to print to console in Java program: System.out.print or
I have created a file with printStream object as shown below. PrintStream outPutOffice =

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.