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

  • Home
  • SEARCH
  • 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 6056261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:21:24+00:00 2026-05-23T08:21:24+00:00

I have here a simple Java Program. I am writing this to walk through

  • 0

I have here a simple Java Program. I am writing this to walk through the logic of the program step by step and verify why it returns the output that it does. I would appreciate any feedback on the way I’m interpreting this.

I’ll display the program first, and then explain how it works afterwards.

public class Test9 {
    public static void main (String [] args) {
        int x = 0;
        int y = 10;
        for (int z = 0; z < 5; z++) {
            if ((y-- > 7) || (++x > 2)) {
                y--;
            }
        }
        System.out.println(y + "" + x);
    }
}

What is the output and why?

In order to know the correct output of this program, it’s necessary to keep a running tally of all three int variables, x, y, and z, during the course of the “for” loop.

At the beginning of the loop, z=0, x=0, and y=10.
It then proceeds to the if statement, where it looks to see if the current value of y is greater than 7. It’s important to note that y– means that it will post-decrement y by 1. Which means AFTER the conditional statement has been been decided True or False.

So, since y=10, currently, 10>7, and the statement is True. Since this conditional statement is true and it sees that an OR operator is next, it skips the ++x>2 since it doesn’t need to know whether it is TRUE or FALSE. The contents of the loop are now performed, y–, which makes y=8. Decremented once in the conditional statement and once inside the if statement. x remains 0.

The next iteration begins with, z=1, y=8. x=0. The same rules apply, so 8>7, y will be decremented by 2 and x will remain the same.

The third iteration begins with z=2, y=6, x=0. This time around, 6>7 is false, but y is still post decremented. So y=5, and ++x>2, will evaluate as 1>2, since ++x is the prefix version and adds 1 before the operation takes place. Both operations are false, and y will not be decremented a second time.

The forth iteration begins with z=3, y=5, x=1. 5>7 is false, y is decremented to 4. 2>2 is false as well, therefore no 2nd decrement to y.

The fifth iteration begins with z=4, y=4, x=2. 4>7 is false, y is decremented to 3. 3>2 is true! So y will be decremented a 2nd time, to 2.

The ending values are y=2, x=3. Therefore the program output will be:
2 3

It’s important to understand when the increments and decrements happen and when they don’t, especially when they are inside the conditional statements of an if statement, since it will impact the final output.

If there are terms/jargon, ways of saying things, or flaws in my logic please let me know. I’m trying to understand all the fundamentals of java. Thanks.

  • 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-05-23T08:21:25+00:00Added an answer on May 23, 2026 at 8:21 am

    You logic is correct… But what is actually the question?

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

Sidebar

Related Questions

I've just finished reading 'Java for Dummies' and have begun to create a simple
I'm trying to launch an external program from my java swing app using this:
Have you ever thought about the implications of this change in the Java Programming
OK, so this should be simple... But I still can't figure it out. I
I have a simple question. I'm trying to upload a file to my ftp
I'm making a simple address book GUI and I don't have a very good
Hi all this is my post on stackoverflow. I am normally a lurker and
So I want to do the simplest possible thing. Assume I have a MYSQL
I need to send some data to a C program from my app in
I'm trying to use the Simple hill climbing algorithm to solve the travelling salesman

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.