The Return Type: returns the greater one. What happens if I enter 2 equal values? Why its not error? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

The Return Type: returns the greater one. What happens if I enter 2 equal values? Why its not error?

Why if I enter two equal values the program does not give an error? class Program { public static void main(String[ ] args) { int res = max(7, 7); System.out.println(res); } static int max(int a, int b) { if(a > b) { return a; } else { return b; } } }

2nd Jun 2017, 3:10 PM
Alex Ihnatsiuk
Alex Ihnatsiuk - avatar
2 Answers
+ 4
If the if statement is false the else is executed. if(a > b) Or in other words. is 7 > 7? No, let's go to the else statement. else{ return b; } Ok, b was 7. return 7; If they are equal it doesn't really matter which one you return anyway.
2nd Jun 2017, 3:16 PM
Rrestoring faith
Rrestoring faith - avatar
0
Great. Now I understand. Thanks
2nd Jun 2017, 3:18 PM
Alex Ihnatsiuk
Alex Ihnatsiuk - avatar