1/35
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
loop
a program construct that repeatedly executes the loop's statements (known as the loop body) while the loop's expression is true; when the expression is false, execution proceeds past the loop.
iteration
Each time through a loop's statements
while loop
a program construct that repeatedly executes a list of sub-statements (known as the loop body) while the loop's expression evaluates to true. Once entering the loop body, execution continues to the body's end, even if the expression would become false midway through.
for loop
is a loop with three parts at the top: a loop variable initialization, a loop expression, and a loop variable update. A for loop describes iterating a specific number of times more naturally than a while loop.
for loop structure
for (initialExpression; conditionExpression; updateExpression) {
// Loop body
}
// Statements after the loopincrement operator, statement i = i + 1
++i
decrement operator, means i = i - 1
--i
for
Number of iterations is computable before the loop, like iterating N times.
while
Number of iterations is not (easily) computable before the loop, like iterating until the input is 'q'.
Iterating through a string with a for loop
A programmer commonly iterates through a string, examining each character.
Array
s an ordered list of items of a given data type. A special variable having one name, but storing a list of data items, with each item being directly accessible. Some languages use a construct similar to an array called a vector. Each item in an array is known as an element.
element
each item in an array.
Array declaration and allocation
dataType[] arrayName = new dataType[numElements];the declaration uses [] symbols after the data type to indicate that the variable is an array reference.
array reference
can refer to arrays of various sizes.
new keyword
creates space in memory to store the array with the specific number of elements.
array reference variable
is assigned to refer to that newly allocated array. Ex: int[] gameScores = new int[4]; declares an array reference variable gameScores, allocates an array of four integers, and assigns gameScores to refer to the allocated array.
array reference declaration and array allocation
A programmer can declare an array reference variable without allocating the array at that time and later assign the variable with an allocated array.
example of array reference declaration without allocation
int[] gameScores;good practice
If the size of an array is known, good practice is to combine the array reference variable declaration with the array allocation.
the index of an array is
an expression
An array’s index must be
an integer type
the array index can not be
a floating type.
Example: Declare and initialize an array named myVals that stores 10 elements of type int with default values.
int[] myVals = new int[10];
loops and arrays
are a key advantage, for example, store the values and then print them.
array’s length property
accessed by appending .length after the array's name, indicates the number of array elements.
example of an array’s length
userVals.length
array initialization
An array's elements are automatically initialized to default values when using the new keyword to initialize the array reference.
array initalization details
The default value for elements of integer and floating-point data types is zero, and the default value for boolean elements is false.
array specific initialization
non-default values by specifying the initial values in braces {} separated by commas. Ex: int[] myArray = {5, 7, 11}; creates an array of three integer elements with values 5, 7, and 11. Does not require the keyword new.
Iterating through an array using loops
commonplace and is an important programming skill to master.
Iterating through an array using loops example
// Iterating through myArray
for (i = 0; i < myArray.length; ++i) {
// Loop body accessing myArray[i]
}Note that index variable i is initialized to 0, and the loop expression is i < myArray.length rather than i <= myArray.length. If N were 5, the loop's iterations would set i to 0, 1, 2, 3, and 4, for a total of 5 iterations. The benefit of the loop structure is that each array element is accessed as myArray[i] rather than the more complex myArray[i - 1].
common error
try to access an array with an index that is out of the array's index range. Ex: Trying to access highScores[8] when highScores's valid indices are 0-7.
Swapping two variables
means to assign y's value to x, and x's value to y. If x is 33 and y is 55, then after swapping x is 55 and y is 33.
common swapping method
to use a temporary variable
temporary variable
is a variable used briefly to store a value. To understand the intuition of such temporary storage, consider a person holding a book in one hand and a phone in the other, wishing to swap the items. The person can temporarily place the phone on a table, move the book to the other hand, then pick up the phone.