In this chapter, you’ll learn how to use while and for loops to add repetition to your code. Repeating tasks without making errors is something that computers do well and people do poorly. Computers are often used to automate repetitive tasks, such as searching for text in documents. Ĭonsider the alternative: BufferedReader in =. Sometimes, while(true) is more readable than the alternatives. MyLoop() // start the loop When is a while(True) loop appropriate? Public static boolean displayResults(String searchUsername, String searchPassword, List titles) Scanner scanner = new Scanner(System.in) Result = displayResults(searchUsername, searchPassword, null) List titles = Files.lines(path).collect(Collectors.toList()) Path path = Paths.get("/Users/Coding/Desktop/myFile.txt").toAbsolutePath() You are looping only over the last step i.e. The idea is that You get username -> check -> display (These 3 steps happen in 1 big loop). You are looping the results of your display method. While-Loop repeating when program reaches else-statement If a is replaced with the constant value true, then a will never be false so the loop will never quit. This will keep repeating until a is not true. If their policy is while (!exit_flag), so be it. Of course, you'll have to square this with the people who will read that code in the end. There's no real technical difference between both it's just a matter of readability and expression of intent. Nothing says indefinitely better than while (true). However, if you are writing this loop to create a permanent daemon or event loop, then the point of this loop is to keep the program running indefinitely. something counting from 0 to 100 and then leaving the loop. while (!exit_flag) suggests that the loop has a natural exit condition which it will reach at some specific point by itself, e.g. My project leader doesn't allow this kind of loop in my code, he's arguing that it has to be replaced by while(!exit_flag) But look at this code, it basically does what is in the do, then checks the while portion to see if it should do it again. I 100% agree that while (true) is not a good idea because it makes it hard to maintain this code and the way you are escaping the loop is very goto esque which is considered bad practice.Ĭheck your individual language documentation for the exact syntax. I think what you are looking for is a do-while loop. The second will only "do something" when the statement evaluates to true. The first will execute the "do something" at least once every time even if the statement is never true. There is a discrepancy between the two examples. Is while (true) with break bad programming practice? because some input is null, or empty, or zero) I find it clearer to return that answer directly than to have a variable to store the result, then a whole block of other code, and finally a return statement. For example, if I can work out the result of a method within the first few lines (e.g. Note that this is exactly the same sort of argument that people have about multiple return statements in a method. The break statement communicates the intent more clearly, and lets the rest of the block get on with what it needs to do without worrying about earlier conditions. I view the latter as more complicated to read: it's got an extra else block, the actOnInput is more indented, and if you're trying to work out what happens when testCondition returns true, you need to look carefully through the rest of the block to check that there isn't something after the else block which would occur whether running has been set to false or not. Now let's force it to use a flag: boolean running = true Sometimes it can't be helped (or the alternative is to have a bool variable which does nothing meaningful except indicate the end of the loop, less clearly than a break statement) but it's worth at least trying.Īs an example of where it's clearer to use break than a flag, consider: while (true) In situations where it's the first thing I write, I almost always at least try to refactor it into something clearer. I wouldn't say it's bad - but equally I would normally at least look for an alternative.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |