Help in understanding Return Value and Control Flow

I was going through the editorial at ZCO16002 - Editorial and I have some doubts regarding some pieces of code given there.

Note:- The following are just some parts of the code given in the editorial.

return dp[i][j] = 2;

	if (i < 0 || j < 0 || i >= n || j >= n)
		return 0;
	if (dp[i][j] != -1)
		return dp[i][j];
	else if (i == 0)
		return dp[i][j] = 2;
	else 

As you can see the function returns dp[i][j] = 2, but what I have learnt till now is that a function returns a value according to its type whereas here the function is assigning a value to another variable, how is this possible?
Then in the second part, I saw an ‘if’ followed by another ‘if’ and then an ‘else if’, so my doubt is that which ‘if’ statement serves as the basis for the ‘else if’ and ‘else’ statement? And is it alright to use an ‘if’ followed by some more ‘if’ and then using ‘else if’?

return dp[i][j]=2 means dp[i][j] is set to 2 and it will return 2.
As for if, you can have as many ifs and else ifs

if(a){
     if(b){
     //code if a and b are true
     }
     else if(c){
      //code if a is true, b is false, and c is true
      }
      else{
       //code if a is true, b is false, and c is false
      }
      if(d){
      //code if a is true and d is true
      }
}

else if always corresponds to the deepest if.

2 Likes

Thanks :slightly_smiling_face: