for loops in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

12th Aug 2019, 3:51 PM
PVSV PW
PVSV PW - avatar
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.
12th Aug 2019, 6:46 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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
12th Aug 2019, 5:55 PM
D_Stark
D_Stark - avatar
+ 3
Why not find out from the Java tutorial?
12th Aug 2019, 11:52 PM
Sonic
Sonic - avatar
+ 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.
13th Aug 2019, 1:05 AM
Lifelong Learner
Lifelong Learner - avatar