Use while-loop to avoid unnecessary break.

This commit is contained in:
Nicolás A. Ortega Froysa 2022-07-02 12:10:36 +02:00
parent b9c2ae82d6
commit aa5702a0a3

View File

@ -93,17 +93,16 @@ fn main()
}; };
// the next (first) box the prisoner is to open is the one with his number (`i`) // the next (first) box the prisoner is to open is the one with his number (`i`)
let mut next_box:usize = i as usize; let mut next_box:usize = i as usize;
// for every attempt the prisoner has (i.e. `size/2`) // while the prisoner still has attempts left AND he hasn't found his number
for _j in 0..(size/2) while prisoner_i.attempts < size/2 && !prisoner_i.found
{ {
// count attempt
prisoner_i.attempts += 1; prisoner_i.attempts += 1;
// if the number inside the box is his number // if the number inside the box is his number
if boxes[next_box] == i if boxes[next_box] == i
{ {
// assign `found` to true // assign `found` to true
prisoner_i.found = true; prisoner_i.found = true;
// stop looking through more boxes.
break;
} }
else else
{ {