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

Loops and Arrays

The document discusses loops and arrays in Java. It defines loops as control structures that repeatedly execute parts of a program if a condition is true. There are four main types of loops: for, for-each, while, and do-while. Arrays allow storing multiple values in a single variable rather than separate variables. Arrays can be single-dimensional or multi-dimensional. Elements in an array are accessed using indexes and the length of an array can be determined. Several problems demonstrate using loops and accessing, changing, and determining lengths of arrays.

Uploaded by

callmeazu25
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)
41 views

Loops and Arrays

The document discusses loops and arrays in Java. It defines loops as control structures that repeatedly execute parts of a program if a condition is true. There are four main types of loops: for, for-each, while, and do-while. Arrays allow storing multiple values in a single variable rather than separate variables. Arrays can be single-dimensional or multi-dimensional. Elements in an array are accessed using indexes and the length of an array can be determined. Several problems demonstrate using loops and accessing, changing, and determining lengths of arrays.

Uploaded by

callmeazu25
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/ 23

LOOPS

LOOPS
•A control structure that is used to execute a
particular part of the program repeatedly if a
given condition evaluates to be true.
•Comes into three types:
• For loop
• For-each loop
• While loop
• Do-While loop
Elements required to perform counter-
controlled repetition
1. a control variable (or loop counter)
2. the initial value of the control variable
3. the increment/decrement by which the control
variable is modified each time through the loop
(also known as each iteration of the loop)
4. the loop-continuation condition that determines
if looping should continue.
PROBLEM 1:
int n1=0, n2=1 ,n3 ,i , count=10;
System.out.print(n1+" "+n2);

for(i=2;i<count;++i) {
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
PROBLEM 2

int count = 0;
do {
System.out.print (count +" ");
count++ ;
} while ( count < 6 );
PROBLEM 3
int x = 0;
while ( x < 20 )
{
System.out.print( x + " " );
x = x + 5;
}
PROBLEM 4
int x = 0;
while ( x < 20 )
{
x = x + 5;
}
System.out.print(x);
}
PROBLEM 5

for(int i = 0; true; i++) {


System.out.println("The world is breaking apart!");
break;
}
PROBLEM 6
int i, fact=1;
int number=6;
for(i=1;i<number; i++){
fact*=i;
}
System.out.println(fact);
}
PROBLEM 7
int count = 0;
int x = 0;
while (count < 10){
if (x % 2 == 0){
System.out.println("Hephep");
} else {
System.out.println("Hooray");
}
count++;
x++;
}
PROBLEM 8
int total = 0;
for(int s=1;s<=20; s++){
if(s % 2 != 0){
System.out.print(s+” ”);
total += s;
}
System.out.println();
System.out.println(“Total is: ”+total);
}
ARRAYS
ARRAYS
• are used to store multiple values in a single variable,
instead of declaring separate variables for each
value.
• Types of arrays:
• single-dimensional array
represents a linear collection of data

• multi-dimensional array
 are used to store data as a tabular form, like a table with
rows and columns.
ACCESSING THE ELEMENTS OF AN ARRAY
• To access an element, refer to the index number.
Example:
//For 1D array
String[] motors = {“Suzuki", “Yamaha", “Honda", “Kawasaki"};
System.out.println(motors[3]);
//For 2D array
int[][] numbers = {{0,2,4}, {1,3,5}};
System.out.println(numbers[1][2]);
CHANGING THE ELEMENTS OF AN ARRAY
• To change the value of a specific element, refer to the index number:
Example:
//For 1D array
String[] motors = {"Suzuki", "Yamaha", "Honda", "Kawasaki"};
motors[3] = "Duke";
for (int i = 0; i < motors.length; i++) {
String motor = motors[i];
System.out.println(motor+" ");
}
CHANGING THE ELEMENTS OF AN ARRAY
• To change the value of a specific element, refer to the index number:
Example:
//For 2D array
int[][] numbers = {{0,2,4}, {1,3,5}};
numbers [1][1] = 7;
for (int i = 0; i < numbers.length; ++i) {
for(int a = 0; a < numbers[i].length; ++a) {
System.out.println(numbers[i][a]);
}
}
DETERMINING THE LENGTH OF AN ARRAY
• To access an element, refer to the index number.
Example:
//For 1D Array
String[] motors = {“Suzuki", “Yamaha", “Honda", “Kawasaki"};
System.out.println(motors.length);
//For 2D Array
int[][] numbers = {{0,2,4}, {1,3,5}};
System.out.println(numbers.length);
DETERMINING THE LENGTH OF AN ARRAY
• To access an element, refer to the index number.
Example:
//For 1D Array
String[] motors = {“Suzuki", “Yamaha", “Honda", “Kawasaki"};
System.out.println(motors.length);
//For 2D Array
int[][] numbers = {{0,2,4}, {1,3,5}};
System.out.println(numbers.length);
PROBLEM 1
int array_length = 0;
double[][] av = { {1.2, 9.0}, {9.2, 0.5, 0.0},
{7.3, 7.9, 1.2, 3.9} } ;
array_length = av.length;
System.out.println(array_length);
PROBLEM 2
int[][] a = { {10, 11, 31, 14}, {24, 32, 29, 20, 72 },
{33, 82} } ;

for( int row=0; row < a.length; row++ ) {


for( int col=0; col < a[row].length; col++ ) {
System.out.print( a[row][col] + " ");
}
}
PROBLEM 3
double[][] cb = { {1.2, 9.0, 3.2}, {9.2, 0.5,
1.5, 1.2}, {7.3, 7.9, 4.8}, {1.2, 9.0, 3.2} } ;
System.out.println(cb[2][2]);
PROBLEM 4
double[] myList = {1.5, 2.5, 3.5, 4.5};
double total = 0;
for (int i = 0; i < 3; i++) {
total += myList[i];
}
System.out.println(total);
PROBLEM 5
int[][] num = {{3,4,0,1,2},{0,4,3,2,1},{0,3,1,2,4},{2,1,4,0,3}};
int []a = num[0];
int []b = num[1];
int []c = num[2];
int []d = num[3];
System.out.println(a[b[c[d[4]]]]);

You might also like