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