Iteration / Loops¶
Key Ideas¶
- Do Loop
- While Loop
- For Loop
- For Each Loop
- Loop Pattern for Menus / Programming Structure
- Loops for grids
Readings¶
https://books.trinket.io/thinkjava2/chapter6.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
Lecture¶
Definition
Iteration / Loops: the repetition of a sequence of computer instructions a specified number of times or until a condition is met. Merriam Webster
Java Loops¶
Iteration has several purposes:
- Reusability - loops allow us to repeat code as often as we wish in the form that we can write it once and run it many times.
- Loops allow us to traverse the items in a list, array, or data structure.
Types Of Loops¶
Java has four main types of loops
- Do Loop - This loop will always run once
- While Loop - This loop runs while a condition is true
- For Loop - This loop runs a specific amount of times
- For Each Loop - This loop is a form of the for-loop. It is set up to easily iteration of each item in an array, list, or data structure.
Example Basic Loops¶
/*
Loop Lecture Code
Programmer: James Goudy
*/
public class Loops {
public static void main(String[] args) {
int cntr = 0;
//Do Loop This loop will always run once
do
{
System.out.println(cntr + " do loop");
//We have to increment a counter
//otherwise our while statement will never
//go to false and we will be in a endless loop.
cntr++;
}while(cntr < 2000);
//while loop - This loop will always check for true before running
cntr = 0;
while (cntr < 2000)
{
System.out.println(cntr + " while loop");
cntr++;
//We have to increment a counter
//otherwise our while statement will never
//go to false and we will be in a endless loop.
}
//for statments are excellent if we have
//a known number of cycles to loop.
//Also we create a cntr, check for true and increment
//the counter all on one line.
for(int cntr2 = 0; cntr2 < 2000; cntr2++)
{
System.out.println(cntr2 + " for loop");
}
}
}
Loops For Printing A Grid¶
/*
Program: Loop Lecture Grid
Programmer: James Goudy
This program demonstrates how to write a grid
using loops. The important note is that the
counters in the loops act as coordinate to the row and columns
of the x's that are being printed out.
*/
package loops_lecture_grid;
public class Loops_Lecture_Grid {
public static void main(String[] args) {
//Variables
String xx = "x";
int Colcntr = 0; //This is our column counter
int ColStop = 5; //This is the actual number of
//columns for the grid
int Rowcntr = 0; //This is our row counter
int RowStop = 5; //This is the actual number of
//rows for the grid
//loop to control the number of rows
while (Rowcntr < RowStop) {
//loopt to control the number of columns
while (Colcntr < ColStop) {
//Note the print. If a println is used
//all of the starts will print down / "vertical"
System.out.print("x");
//Increment the column counter to the next column
Colcntr++;
}
//this represents the end of the row
//the cursor needs to be put on the next row
//so a println will be used.
System.out.println("");
//Next the Colcntr needs to be reset to zero
Colcntr = 0;
//Advance the rowcnter to the next row
Rowcntr++;
}
}
}
End Of Topic