Im having tough time in understanding questions related to compile time and runtime error. Can you please shed light on this topic as how to proceed in this regard? i have two code listings shown below:
Code Listing 1 :
Code Listing 2:
the compiler gives error in Code Listing 2 as to variable j might not have been initialized. Can you please tell me what the compiler thinks when it encounters code listing 1 and 2. And also the generalized version of the problem relating to compile time error and runtime exception. ??
posted 3 years ago
Welcome to JR.
Well, there is a quick pointer. In code listing 1, the compiler will always execute the initialization for i before evaluating the condition.
So it compiles fine. In case-2, initialization of j is conditionally dependent on the outer loop. The compiler is not sure whether the outer
loop will even run or not, so it in such a scenario, it suspects as its dependent on outer loop, it is not necessary that j will initialize that's
why it will give the compile time error.