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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:07:48+00:00 2026-05-29T04:07:48+00:00

The following is part of the code for my college assignment. else if (!codeList.contains(userCode))

  • 0

The following is part of the code for my college assignment.

else if (!codeList.contains(userCode)) {
                    i--; // i is the counter for the for-loop
                }
else if (userQuantity[i]==0) {
                    i--;
                }

The first part makes sure that if the user enters the wrong code, the counter i does not increment 1, or rather, it subtracts 1 from the recently incremented counter. This part works fine.

The second part however is what I seem to be having problems with. userQuantity[] is an int array and it has to be an array. This does not seem to do anything to the code. Even if 0 is entered for the quantity, it still incrememnts the counter which is not desireable.

I should explain, to avoid confusion, that this is an infinite for-loop (with a break statement). The reason I am doing a for-loop is because I am required to. Is it because of my for-loop that the condition isn’t working or am I doing something completely wrong with it?

This is for my college assignment so I would appreciate answers with explanation and not just quick-fixes. If you need me to explain, let me know please.

  • 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-29T04:07:49+00:00Added an answer on May 29, 2026 at 4:07 am

    Although it’s not strictly illegal in Java, it’s not a good idea to change the value of the for loop control variable from within the loop. (Such modification is illegal in some other languages.)

    By changing the loop iteration variable within the loop, you’re messing with the implicit assumptions offered by your use of a for loop. For example, if a reader sees:

    for (int i = 0; i < 10; i++) {
        // ...
    }
    

    the reader will rightfully assume that the loop is intended to execute exactly 10 times (or, no more than 10 if there is a break in there). However, if you go changing the value of i within the loop, this assumption is no longer valid.

    If you must change the value of the counter, I would suggest writing this as a while loop instead:

    int i = 0;
    while (i < 10) {
        // ...
        i++;
    }
    

    along with a comment that explains why you are changing i within the loop and what it means to do so.

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

Sidebar

Related Questions

I am using the following code as part of an autocomplete script to avoid
Google optimizer includes the following snippet as part of their conversion code. Unfortunately, the
I have the following code, which is run every 10ms as part of a
For a part of a program i need the following 2 methods. The first
I am having issues with the following part of my code. when nn is
The following code is part of a small XAML application that displays data in
Following is a part of my code for a project: public class Body extends
I am currently running the following code as part of a simple test iPad
I have the following part of code : for(int i=0;i<n;i++) { printf(Student %dn,i+1); printf(Enter
I've been trying to find what the problem is to this following code part.

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.