KnowledgeBoat Logo

Computer Applications

Predict the Output of the following Java program:

class dk3
{
public static void main(String args[])
{
int m=2,n=15; 
for(int i=1;i<=5;i++)
{
m++;--n;
System.out.println("m="+m);
System.out.println("n="+n);
}
}
}

Java

Java Iterative Stmts

ICSE 2010

67 Likes

Answer

m=3
n=14
m=4
n=13
m=5
n=12
m=6
n=11
m=7
n=10

Working

This table shows the change in values of m, n and i as the for loop iterates:

mniRemarks
215Initial values
31411st Iteration
41322nd Iteration
51233rd Iteration
61144th Iteration
71055th Iteration
7106Once i becomes 6, condition is false and loop stops iterating.

Answered By

34 Likes


Related Questions