0% found this document useful (0 votes)
12 views

Looping and Arrays Slides

The document discusses different types of loops in Java including while, do-while, for, and for-each loops. Examples are provided of each loop type to calculate factorials, sum arrays, and iterate through values.

Uploaded by

Gayathri
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)
12 views

Looping and Arrays Slides

The document discusses different types of loops in Java including while, do-while, for, and for-each loops. Examples are provided of each loop type to calculate factorials, sum arrays, and iterate through values.

Uploaded by

Gayathri
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/ 18

while ( condition )

statement ;
someValue factorial
int someValue = 4;

int factorial = 1; 4 1
while(someValue > 1) {

factorial *= someValue;

someValue--;

System.out.println(factorial); // displays 24
do
statement ;
while ( condition )
int iVal = 5;

do {

System.out.print(iVal); 5 * 2 = 10

System.out.print(“ * 2 = “); 10 * 2 = 20

iVal *= 2; 20 * 2 = 40

System.out.println(iVal);

} while(iVal < 25);


int iVal = 80;

do {

System.out.print(iVal);

System.out.print(“ * 2 = “); 80 * 2 = 160

iVal *= 2;

System.out.println(iVal);

} while(iVal < 25);


for (initialize; condition; update)
statement ;
int i = 1;

while(i < 100) { for(int i = 1; i < 100; i *= 2)


System.out.println(i); System.out.println(i);
i *= 2;

}
float[] theVals = new float[3];

theVals[0] = 10.0f;
theVals 10.0f 20.0f 15.0f
theVals[1] = 20.0f;
0 1 2
theVals[2] = 15.0f;

-
-
-
float[] theVals = new float[3];

theVals[0] = 10.0f;

theVals[1] = 20.0f;

theVals[2] = 15.0f;

float sum = 0.0f;

for(int index = 0; index < theVals.length; index++)

sum += theVals[index];

System.out.println(sum); // displays 45
float[] theVals = new float[3];

theVals[0] = 10.0f;

theVals[1] = 20.0f;

theVals[2] = 15.0f;

float sum = 0.0f;

for(int index = 0; index < theVals.length; index++)

sum += theVals[index];

System.out.println(sum); // displays 45
float[] theVals = new float[3];

theVals[0] = 10.0f;

theVals[1] = 20.0f;

theVals[2] = 15.0f;

float sum = 0.0f;

for(int index = 0; index < theVals.length; index++)

sum += theVals[index];

System.out.println(sum); // displays 45
float[] theVals = {10.0f, 20.0f, 15.0f };

float sum = 0.0f;

for(int index = 0; index < theVals.length; index++)

sum += theVals[index];

System.out.println(sum); // displays 45
float[] theVals = { 10.0f, 20.0f, 15.0f };

float sum = 0.0f;

for(float currentVal : theVals)

sum += currentVal;

System.out.println(sum); // displays 45

for (loop-variable : array)


statement ;
-
-

-
-

-
-
-
-
-

-
-

You might also like