The compiler will check whether the local variable is initialized before it is used.
So, if you use the "IF statement"(only) and you are initializing the local variable inside the block.
The compiler does not know whether the block will run or not. So, the compiler is uncertain about the initialization of the local var and it will not compile.
But , if you use "IF ELSE statement" and you are initializing the local variable inside
both the block(If and Else).
Then the compiler realises that either one of the statement will run for sure and the var will be initialized before its gonna be used.