1
In a hypothetical situation: Is it good programming practice to write a while that way? So that it continues to run endlessly?
while(true){
System.out.println("Por favor, Escolha: ");
System.out.println("1: Adicionar Cliente");
System.out.println("2: Depositar Dinheiro");
System.out.println("3: Retirar Dinheiro");
System.out.println("4: Checar Conta");
System.out.println("5: Calcular Valor");
System.out.println("6: Sair");
}
Ta missing a read (user input) ai :P
– rray
The question is whether a
while (true)
has a valid case or if this case is exactly valid?– Gabriel Katakura
Yeah, I’d generally like to know if at any point, I need to do a while that way, if it’s wrong?
– Marcielli Oliveira
Just looking at this code becomes difficult, with the user input and if any of the methods called by the numbers kill the application or call the menu again is up to reasonable
– rray
I edited the question for a hypothetical situation, I had not specified before. Thank you.
– Marcielli Oliveira
I get it, I’ll post the whole code. Thank you.
– Marcielli Oliveira
For this situation there does not make sense, the loop will be infinite with nothing to change this condition.
– user28595
For the situation of your question, the while would make sense if it were
while (escolha != 6)
, for example. The use of while/do-while does not hurt any best Practice. You should use it whenever you want something to run at least once, and not know the number of times it should repeat.– Eduardo Silva
This is one of the most typical cases where good practice is of no use. In this example shown it is clear that you should not do this. In another example, it’s the right thing to do.
– Maniero
I understand, guys, thank you so much. I’m still adapting, trying to understand as a whole for later when I need it, I really make sure I can use it or not. ;)
– Marcielli Oliveira
I can do while that way as long as I have some way that it stops at some point... It’s because I’ve never seen a while being done with just (true) and got in doubt.
– Marcielli Oliveira
As others have cited above, while(true) is not a bad practice if there is a condition to break this loop at some point, games for example use while(true) in a thread to work the movement of objects and other information...
– Furflez
If you can avoid it, it’s better because the code gets harder to read. Not in this example, because it is very simple, now imagine more complex codes, with some conditions of stops using break inside the repetition, if not very well structured it is difficult to give maintenance.
– Guilherme Guini
I recommend caution with the term "good practice", as this is usually used to propagate ideas "canned" from those who do not master what they are doing, or who do not want to take the trouble to explain (usually the first case, see many teachers who have never used programming for anything serious that was not only theoretical, and sell these ready-made recipes), so I recommend that you do not buy this idea when they try to apply this terminology to you, and charge a more serious explanation. Everything in programming has to have a technical reason to be done, and "good practice" is not a true reason
– Bacco
Related subject http://answall.com/questions/104376/70
– Bacco
So it’s not good to use the term good practice because in programming a good practice would be a code well done? Be it, the way you do it... There’s no recipe... That would be it?
– Marcielli Oliveira