Newbie question. Flow of this program?

Joined
Sep 18, 2008
Messages
1
Reaction score
0
Hello, I'm a newbie.
How come when I run this program, this runs like 3 times.
Can someone explain the flow of the program please?
I placed line numbers at the end of lines.

Thank you.


//: c03: ifelse.java
public class ifelse { //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
 
Joined
Sep 18, 2008
Messages
3
Reaction score
0
Normally a stand alone java pgm begins its execution with main() method.ur pgm has a method test(). This method runs everytime it is called.
If u see ur main(), it calls test() thrice in line 16,17,18. Hence u get 3 values printed on ur standard output( monitor).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,117
Latest member
Matilda564
Top