At university we learned 2 ways to read integers from console, while specific integer is not given. First one is the "Loop and half":
int val = readInt(“Enter val:”);
while (val != SENTINEL)
val = readInt(“Enter val:”);
}
They say that it is a bad way because of code duplication val = readInt(“Enter val:”);
and they suggest second way to do it "The Repeat-Until-Sentinel Idiom" :
while (true) {
value = readInt(“Enter val:”);
if (value == sentinel) break;
}
I had teacher,who was more in computer science and he always sad that its better to avoid while(true)
and break
in programs and don't use them unless emergency, so i am little confused now.
Which way is considered as a better solution ?