Skip to main content

Newbie Question. Flow of this program?

1 reply [Last post]
aigbellyup
Offline
Joined: 2008-09-17
Points: 0

Hello, I'm new to JAVA. How come this program runs three times.
What calls which lines. I put in line numbers at the end of lines to make the explanation easier (if needed).

Thank you.

//: c03: ifelse2.java
public class ifelse2 { //01
static int test (int testval, int target) { //02
int result = 0; //03
System.out.println("hello"); //04
System.out.println(testval); //05
System.out.println(target); //06
if(testval > target) //07
result = result + 1; //08
else if(testval < target) //09
result = result - 1; //10
else //11
result = 0; //match //12
return result; //13
} //14
public static void main (String[] args) { //15
System.out.println(test(10, 5)); //16
System.out.println(test(5, 10)); //17
System.out.println(test(5, 5)); //18
} //19
} ///:~ //20

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
aigbellyup
Offline
Joined: 2008-09-17
Points: 0

Why doesn't this forum accept my line # 9 of my program.

result = result - 1; //10
else //11
result = 0; //match //12
return result; //13
} //14
public static void main (String[] args) { //15
System.out.println(test(10, 5)); //16
System.out.println(test(5, 10)); //17
System.out.println(test(5, 5)); //18
} //19
} ///:~ //20