Java development for beginners 9. For statement and arrays

Another way to interrupt the flow from top to bottom is by using loops. A programming loop is one that forces the programme to go back up again. If it is forced back up again you can execute lines of code repeatedly.

 

 

Let´s start with the "for loop".

The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows:

for (initialization; termination; increment) {
    statement(s)
}

 

When using this version of the for statement, keep in mind that:

-The initialization expression initializes the loop; it's executed once, as the loop begins.

-When the termination expression evaluates to false, the loop terminates.

-The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.

Arrays

Another programming concept you just have to get used to if you're to code effectively is the array.

An array is a way to hold more than one value at a time. It's like a list of items. Think of an array as the columns in a spreadsheet. Like a spreadsheet, arrays have a position number for each row. The positions in an array start at 0 and go up sequentially. Each position in the array can then hold a value.

Array syntax;

// declares an array of integers
int[] anArray;

Like declarations for variables of other types, an array declaration has two components: the array's type and the array's name.

An array's type is written as type[], where type is the data type of the contained elements; the square brackets are special symbols indicating that this variable holds an array. The size of the array is not part of its type (which is why the brackets are empty).

An array's name can be anything you want, provided that it follows the rules and conventions as previously discussed in the naming section. As with variables of other types, the declaration does not actually create an array — it simply tells the compiler that this variable will hold an array of the specified type.

But this just tells Java that you want to set up an integer array. It doesn't say how many positions the array should hold. To do that, you have to set up a new array object:

anArray = new int[6];

You start with your array name, followed by the equals sign. After the equals sign, you need the Java keyword new, and then your data type again. After the data type come a pair of square brackets. In between the square brackets you need the size of the array. The size is how many positions the array should hold.

In the example of the video tutorial we learn how to use the statement for to loop through an array:

package com.edu4java.Tutorial9;

public class Tutorial9 {
	public static void main(String[] args) {
		int[] array = { 2, 3, 5, 7, 1, 4, 7, 3, 0, 5 };

		for (int i = 0; i < array.length; i++) {
			System.out.print(array[i] + ",");
		}
		System.out.println("we add one");
		
		for (int i = 0; i < array.length; i++) {
			array[i]++;
		}
		for (int i = 0; i < array.length; i++) {
			System.out.print(array[i] + ",");
		}
	}
}
<< Previous Next >>