1

I am doing an assignment for school and messing around with some different types of loops, namely for loops... I am curious as to whether the items it accepts are considered as parameters or arguments... and what is the actual difference between the two?

for (initialization; condition; increment/decrement) { Statement(s) }

  • Your courseware should cover the difference between the two. If it does not, please ask your teacher to explain this in class because _everyone in your class_ should be told what the difference is. Not just you. – Mike 'Pomax' Kamermans Feb 09 '20 at 19:04
  • 5
    They are neither parameters nor arguments because `for` is not a function/method. They are expressions. – jarmod Feb 09 '20 at 19:04
  • @jarmod they're not all expressions. Only the `Expression` is an expression; `ForInit` and `ForUpdate` are statements. – Andy Turner Feb 09 '20 at 19:09
  • I understand how the idea can come up, but note, that `for(a;b;c){...}` uses `;` rather than `,`. Long ago I heard my profs talking about formal and actual parameters, may be a variation of the theme. – Curiosa Globunznik Feb 09 '20 at 19:11
  • @AndyTurner OK, technically ForInit can be a list of statement expressions or a local variable declaration, and ForUpdate can be a list of statement expressions. Though the Java tutorial on `for` loosely calls all 3 expressions (per https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html). – jarmod Feb 09 '20 at 19:41

2 Answers2

2

As mentioned in Java Docs:

Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration's parameters in type and order.

E.g. moveCircle() method has 3 parameters in declaration like: circle, deltaX, deltaY.

public void moveCircle(Circle circle, int deltaX, int deltaY) {
//...
}

E.g. moveCircle() method is invoked and has 3 arguments:

moveCircle(myCircle, 23, 56)

If we speak about loops, then we have the general form of the for statement like:

for (initialization; termination;
     increment) {
    statement(s)
}

Where is:

  • The initialization expression initializes the loop; it's executed once, as the loop begins.
  • When the termination expression evaluates to false, the loop terminates.
  • The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.
invzbl3
  • 5,872
  • 9
  • 36
  • 76
0

A parameter is a variable in a method definition. When a method is called, the arguments are the data you pass into the method's parameters.

Please refer here to check further discussion.

kgkmeekg
  • 524
  • 2
  • 8
  • 17