Loops are important for calculations that require repeated steps and for processing input consisting of many data items.
The While loop
It has the form
while (condition)
{
As long as the condition remains true, the statements inside the while statement are executed. These statements are called the body of the while statement.
When you declare a variable inside the loop body, the variable is created for each iteration of the loop and removed after the end of each iteration.
Infinity loops
A very annoying loop error is an infinite loop: a loop that runs forever and can be stopped only by killing the program or restarting the computer.
A common reason for infinite loops is forgetting to update the variable that controls the loop.
The for loop
The for loop is used when a value runs from a starting point to an ending point with a constant increment or decrement.
{
System.out.println(counter);
}
For loop is often called count-controlled loop. In contrast, while loop can be called event-controlled loop because it executes until an event occurs;
Misc Notes:
The While loop
It has the form
while (condition)
{
statements
}
As long as the condition remains true, the statements inside the while statement are executed. These statements are called the body of the while statement.
When you declare a variable inside the loop body, the variable is created for each iteration of the loop and removed after the end of each iteration.
Infinity loops
A very annoying loop error is an infinite loop: a loop that runs forever and can be stopped only by killing the program or restarting the computer.
A common reason for infinite loops is forgetting to update the variable that controls the loop.
The for loop
The for loop is used when a value runs from a starting point to an ending point with a constant increment or decrement.
for (int counter = 1; counter <= 10; counter++)for( initialization; condition; update) { statements; }
{
System.out.println(counter);
}
For loop is often called count-controlled loop. In contrast, while loop can be called event-controlled loop because it executes until an event occurs;
- The initialization is executed once, before the loop is entered.
- The condition is checked before each iteration.
- The update is executed after each iteration.
Enhanced for loop:
Traditional example:
Enhanced for example:
The do loop
Traditional example:
for (int i = 0; i < myArray.length; i++) { System.out.println(myArray[i]); }
Enhanced for example:
for (int myValue : myArray) { System.out.println(myValue); }
The do loop
The do loop is appropriate when the loop body must be executed at least once.
The body of the do loop is executed first, then the condition is tested.do { statements } while (condition);
Misc Notes:
- To compute an average, keep a total and a count of all values.
- To count values that fulfill a condition, check all values and increment a counter for each match.
- If your goal is to find a match, exit the loop when the match is found.
- To find the largest value, update the largest value seen so far whenever you see a larger one.
- To compare adjacent inputs, store the preceding input in a variable.
- When the body of a loop contains another loop, the loops are nested. A typical use of nested loops is printing a table with rows and columns.
- You can introduce randomness by calling the random number generator.
No comments:
Post a Comment