@@ -3,23 +3,45 @@ class JavaLoops {
3
3
4
4
public static void main (String args []) {
5
5
6
- int a = 1 ; // Assign a starting value to variable 'a'
6
+ int a = 1 ; // Assign a starting value to variable 'a'
7
7
8
8
// Demonstrating Usage of While Loop in Java
9
+ // While loop is an entry controlled loop
9
10
System .out .print ("***** Using While Loop ****** " );
10
-
11
+ System . out . println ();
11
12
// While the value of Variable a < = 10 do the condition in the loop body { do this }
12
13
while ( a <= 10 ) {
13
14
System .out .println ("Value of a : " + a ); // While the loop condition is satisfied print value of 'a'
14
15
a ++; // Increment the value of 'a' for every iteration
15
16
}
16
17
17
18
// Demonstrating Usage of For Loop in Java
19
+ // for loop is an entry controlled loop
18
20
// Print the values that are in the range of the condition given in the loop.
19
21
System .out .print ("***** Using For Loop ****** " );
22
+ System .out .println ();
20
23
for ( a = 0 ; a <=10 ; a ++ ) {
21
24
System .out .println ("Value of a : " + a ); // While the loop condition is satisfied print value of 'a'
22
25
}
23
26
27
+ // adding do while loop
28
+ // exit controlled loop
29
+ System .out .print ("***** Using Do While Loop ****** " );
30
+ a =0 ;
31
+ do {
32
+ System .out .println ("Value of a : " + a );
33
+ a ++;
34
+ }while (a <=10 );
35
+
36
+ // for each loop example in java
37
+ // majorly used with Maps and Sets
38
+ System .out .print ("***** Output Using forEach Loop ****** " );
39
+ System .out .println ();
40
+ int inp [] = {1 ,2 ,3 ,4 ,5 };
41
+ // prints each element in the inp array
42
+ for (int i : inp ){
43
+ System .out .println (i +" " );
44
+ }
45
+
24
46
}
25
47
}
0 commit comments