+ 1

Java loops

Is there any particular difference in java in looping statements?

3rd Sep 2017, 5:28 PM
Mahadevan S K
5 Réponses
+ 3
you can either use a for loop that iterates a defined number of times. for (int i = 0 ; i < 10 ; i++) { instructions } will iterate 10 times a while that iterates as long as your condition is true while(condition) { instructions } will iterate as long as condition==true a do-while, that is a while, but it iterates at least once do{ instructions } while(condition) will iterate as long as condition==true, but will anyway iterate at least once and a for each, which is a particular type of for, that does you operation for every element in a certain array/collection Integer[] MyArr = new Arr[10] for ( Integer i : MyArr ) { instructions } will execute the instructions on every element of MyArr
3rd Sep 2017, 5:35 PM
Michael Vigato
Michael Vigato - avatar
+ 4
do{ ... }while(...) will be executed at least once unlike while(...){ ... } and with a for loop, you can don't need to initialize the variables and conditions before
3rd Sep 2017, 5:33 PM
Dapper Mink
Dapper Mink - avatar
+ 2
do you mean differences between Java and other languages or between different cycles in Java?
3rd Sep 2017, 5:33 PM
Michael Vigato
Michael Vigato - avatar
+ 1
yes
3rd Sep 2017, 5:34 PM
Mahadevan S K
0
FOR LOOP:we use for loop when we know the starting and end condition for eg :we print even numbers from 0 to 10 for(i=0;i,<=10;i=i+2) { System.out.println(i); } WHILE LOOP: we use while loop we don't know the exactly how many times the loop executed it stop its execution when condition is false.if the condition is false it execute the statements below while loop example: int x=50; while(x>100) { System.out.println(x); x++; } System.out.println("hi"); }
3rd Jul 2020, 9:13 AM
Ashwini
Ashwini - avatar