+ 3
for loops in java
please let me know more about for loops... I know the increment/decrement types loops, want to know more uses
4 Answers
+ 11
⢠Skipping parts ;)
Any part of 'for' can be skipped.
For example, we can omit 'begin' if we donât need to do anything at the loop start.
int i = 0; // we have i already declared and assigned
for ( ; i < 3; i++) { // no need for "begin"
System.out.println( i ); // 0, 1, 2
}
We can also remove the 'step' part:
int i = 0;
for ( ; i < 3; ) {
System.out.println( i++ );
}
The loop became identical to while (i < 3).
We can actually remove everything, thus creating an infinite loop:
for ( ; ; ) {
// repeats without limits
}
Please note that the two 'for' semicolons ; must be present, otherwise it would be a syntax error.
+ 6
They just loop what ever is inside the {braces} if theres are inner loops they always complete before going back to there outer loops
outer loop Ă1 /1
nested Ă3 /1 /2 /3
nestedĂ2 /12 /12 /12
⤴ď¸back to outer outer loop
+ 3
Why not find out from the Java tutorial?
+ 2
If you have a list, array or other container to store a lot of values and you want to find a specific value, then you can use a for loop for example.
If the for loop find these specific values, you can specify in the for loop what you want to do with that value. Maybe you want to assign this value to a variable to work with it later in the code or you want to calculate something with it or something else.
It depends on what you have to do and how you can realize it with a for loop or another loop.