0% found this document useful (0 votes)
6 views2 pages

Snippet - of - Code - and - What - It - Does - PDF Öffnen

ghfcjvku

Uploaded by

ahmad.hawi197
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Snippet - of - Code - and - What - It - Does - PDF Öffnen

ghfcjvku

Uploaded by

ahmad.hawi197
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Sum of Array Elements Using For-Each Loop

Snippet of code What it does


int[] numbers = {4, 8, 15, 16, 23, 42};

int sum = 0;

for (int number : numbers) {

sum += number;

System.out.println("Sum: " + sum);

2. Finding the Maximum Element Using For-Each Loop

Snippet of code What it does


int[] numbers = {4, 8, 15, 16, 23, 42};

int max = numbers[0];

for (int number : numbers) {

if (number > max) {

max = number;

System.out.println("Maximum: " + max);

3. Counting Even Numbers Using For-Each Loop

Snippet of code What it does


int[] numbers = {4, 8, 15, 16, 23, 42};

int count = 0;

for (int number : numbers) {

if (number % 2 == 0) {

count++;

System.out.println("Even Count: " + count);


4. Printing Elements in Reverse Order Using Traditional For Loop

Snippet of code What it does


int[] numbers = {4, 8, 15, 16, 23, 42};

for (int i = numbers.length - 1; i >= 0; i--) {

System.out.println(numbers[i]);

5. Searching for a Specific Value Using For-Each Loop

Snippet of code What it does


int[] numbers = {4, 8, 15, 16, 23, 42};

int target = 15;

boolean found = false;

for (int number : numbers) {

if (number == target) {

found = true;

break;

if (found) {

System.out.println(target + " is in the array.");

} else {

System.out.println(target + " is not in the


array.");

You might also like