if statement - Java - Turning the for-loop counter back based on a conditional -
if statement - Java - Turning the for-loop counter back based on a conditional -
the next part of code college assignment.
else if (!codelist.contains(usercode)) { i--; // counter for-loop } else if (userquantity[i]==0) { i--; } the first part makes sure if user enters wrong code, counter not increment 1, or rather, subtracts 1 incremented counter. part works fine.
the sec part seem having problems with. userquantity[] int array , has array. not seem code. if 0 entered quantity, still incrememnts counter not desireable.
i should explain, avoid confusion, infinite for-loop (with break statement). reason doing for-loop because required to. because of for-loop status isn't working or doing wrong it?
this college assignment appreciate answers explanation , not quick-fixes. if need me explain, allow me know please.
although it's not strictly illegal in java, it's not thought alter value of loop command variable within loop. (such modification is illegal in other languages.)
by changing loop iteration variable within loop, you're messing implicit assumptions offered utilize of for loop. example, if reader sees:
for (int = 0; < 10; i++) { // ... } the reader rightfully assume loop intended execute 10 times (or, no more 10 if there break in there). however, if go changing value of i within loop, assumption no longer valid.
if must alter value of counter, suggest writing while loop instead:
int = 0; while (i < 10) { // ... i++; } along comment explains why changing i within loop , means so.
java if-statement for-loop
Comments
Post a Comment