Advertisements
Advertisements
Question
Answer the following in brief. Give a reason/example wherever applicable.
Give a program example of a method call in a loop.
Give Reasons
Advertisements
Solution
Example of a method call in a loop:
class Example
{
static void display()
{
System.out.println("Hello Java");
}
public static void main(String[]args)
{
for(int i=1; i<=5; i++)
{
display();//Method call inside loop
}
}
}
Reason: The method display() is called repeatedly inside the for loop, so the same method executes multiple times with each iteration.
shaalaa.com
Is there an error in this question or solution?
