Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this loop bad practice? [closed]

Tags:

The following loop is not good practice. Is it due to a String being the main condition of the for loop rather than an int variable, meaning the for loop is infinite? Also, is it due to there being no instance to enter 'end' to stop the loop?

Scanner in = new Scanner(System.in);
int i = 0;
for (String s = in.next(); !s.equals("end"); i++) 
{
    System.out.println("The value of i is: " + i + " and you entered " + s);
}

How can I rewrite it, so that it conforms to accepted style?

(This is a question in a past exam paper.)

like image 280
user3050340 Avatar asked Jan 13 '14 16:01

user3050340


People also ask

Is for loop a bad practice?

Nested loops are frequently (but not always) bad practice, because they're frequently (but not always) overkill for what you're trying to do. In many cases, there's a much faster and less wasteful way to accomplish the goal you're trying to achieve.

Why is a for I loop so bad?

for loops--and while loops-- are what's known as control statements, meaning they must be placed inside of a function and can not be used as standalones. This inherently increases the chance you'll end up manipulating variables outside of the loop's scope.

Why are break statements bad?

Overusing break statements is also bad practice because it introduces more exit points. We generally want to keep exit points to a minimum since multiple exit points complicate our logic and make it more difficult to comprehend code.

Is it bad practice to use continue in Java?

break and continue are not functional style programming. There is nothing about OOP which suggests break , continue or even goto within a method is a bad idea. IMHO using break and continue are discouraged in OOP languages as they can lead to complexity and confusion.


1 Answers

Well your string s is never changing, which can lead to an infinite loop. You probably wanted:

for (String s = in.next(); !s.equals("end"); s = in.next(), i++) {
    ...
}

Some (me included) might say that i++ shouldn't be in the increment section of this loop, since it's not directly relevant to the condition:

for (String s = in.next(); !s.equals("end"); s = in.next()) {
    ...
    i++;
}

Is it due to a string being the main condition of the for loop rather than an int variable, meaning the for loop is infinite?

The original loop was indeed infinite (at least, after an initial input is entered and assuming "end" wasn't the first input). However, it's not for the reason you state. For-loops are most commonly written using integral loop control variables, but it's not always the case. For example, a common idiom for iterating through a linked list is:

for (Node node = list.head; node != null; node = node.next) {
    ...
}

The problem with your loop is that the string s is never changed, so it will never equal "end" unless that's the first input.

like image 106
arshajii Avatar answered Sep 18 '22 19:09

arshajii