File tree Expand file tree Collapse file tree 1 file changed +70
-0
lines changed Expand file tree Collapse file tree 1 file changed +70
-0
lines changed Original file line number Diff line number Diff line change
1
+ class loops {
2
+ public static void main (String [] a ){
3
+
4
+ // Different Loops in JAVA
5
+ // 1. For Loop
6
+ for (int i = 0 ;i < 11 ;i ++){
7
+ System .out .println ("Value of var is : " + i );
8
+ }
9
+
10
+ int arr [] = {12 ,34 ,56 ,77 };
11
+ // For-each Loop
12
+ for (int value : arr ){
13
+ System .out .println ("Value is : " + value );
14
+ }
15
+
16
+ // 2. While Loop
17
+ int i1 = 0 ;
18
+ while (i1 < 11 ){
19
+ System .out .println ("Value of var is : " + i1 );
20
+ i1 ++;
21
+ }
22
+
23
+ // 3. Do-while Loop
24
+ System .out .println ("Using Do-While Loop : " );
25
+ do {
26
+ System .out .println ("The Value of number is : " + i1 );
27
+ i1 --;
28
+ } while (i1 > 0 );
29
+
30
+ // Jump Statements
31
+ // 1. Break
32
+ for (int i = 0 ;i < arr .length ;i ++){
33
+ System .out .println ("Element is : " + arr [i ]);
34
+ // Skipping the second element
35
+ if (i == 1 ){
36
+ break ;
37
+ }
38
+ }
39
+
40
+ // 2. Continue
41
+ for (int i = 0 ;i < arr .length ;i ++){
42
+ if (i == 2 ){
43
+ continue ;
44
+ }
45
+ System .out .println ("Value is : " + arr [i ]);
46
+ }
47
+
48
+ // Labels
49
+ first_label : {
50
+ int i2 = 12 ;
51
+ if (i2 == 12 ){
52
+ System .out .println ("Local Variable : " + i2 );
53
+ break first_label ;
54
+ }
55
+ }
56
+
57
+ // Using the user-defined function to find the maximum value
58
+ System .out .println ("Maximum value from all the value : " + max1 (arr ));
59
+ }
60
+
61
+ public static int max1 (int [] num ){
62
+ int max_1 = num [0 ];
63
+ for (int val : num ){
64
+ if (val > max_1 ){
65
+ max_1 = val ;
66
+ }
67
+ }
68
+ return max_1 ; // use of return statements
69
+ }
70
+ }
You can’t perform that action at this time.
0 commit comments