3.1 Our old program

Let us re-evaluate our sample program.


Listing 2:short_circuit_2
 
1i
2while (i<|a|)(a[i]kdo  
3  ii+1 
4end while

Without short circuited evaluation, this program won’t work anymore. Just work out the following scenario. a[0] = 2,a[1] = 4,a[2] = -2,a[3] = 7,k = 1. When i = 4, the left condition on line 2 returns false. However, that does not stop the evaluation of the right condition. Which means a[i]k will be a[4]k, which is an invalid operation!