I just want to clarify what's the difference between these syntax of for loop.
for (int ctr = 0; ctr < x.length; ctr++)
x[ctr] += 5;
for (int ctr = 0; ctr < x.length; ctr++) {
x[ctr] += 5;
}
I just want to clarify what's the difference between these syntax of for loop.
for (int ctr = 0; ctr < x.length; ctr++)
x[ctr] += 5;
for (int ctr = 0; ctr < x.length; ctr++) {
x[ctr] += 5;
}
No difference. When you have only one instruction in the loop body, you can ommit the brackets.
There is no difference in this situation , since you have only one instruction inside that for loop , you can skip the brackets.
But its a good practice to always put the brackets to help making your code easier to read so you wouldn't get confused in more complex algorithms.
There is no difference as far as the output is concerned. Java (and may other languages) allow you to skip the braces if only one statement is to be executed inside the loop.
However, it is recommended to add the braces for readability & maintenance purposes.