-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforEach.java
34 lines (32 loc) · 845 Bytes
/
forEach.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
package src.controlStatements.forStatements;
class forEach {
public static void main(String[] args) {
int sum = 0;
int num[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
for(int x: num) {
System.out.println("Value is: " + num);
sum += x;
if(x == 5) break;
}
System.out.println("Summation of 1st five elements: " + sum);
}
}
class search {
public static void main(String[] args) {
int nums[] = {4, 6, 9, 1, 5, 3, 8};
int val = 2;
boolean found = false;
for(int x: nums) {
if(x == val) {
found = true;
break;
}
}
if(found) {
System.out.println("Value found!");
}
else {
System.out.println("Value not found!");
}
}
}