Loops
Greg Ross
Posted on November 4, 2021
Computer science concepts in my lingo.
Iterate
What makes computers useful is the ability to perform repetitive tasks. Repeatable conditions to do time and time again while something is true.
int condition = 0;
do {
System.out.println(condition);
condition++;
}
while (condition < 5);
int condition = 0;
while (condition < 5) {
System.out.println(condition);
condition++;
}
Self perpetuating machine
Knowing what your looking for but do not know exactly how many times or possibilities exist while searching? No worries the collection length or size can realize the unknown to find what you are checking for.
int[] nums = {5, 10, 15, 20};
for (int iterator = 0; iterator < nums.length; iterator++) {
System.out.println(iterator);
}
String[] people = {"Roy", "Gee", "Biv", "Mazza"};
for (String eachPerson : people) {
System.out.println(eachPerson);
}
Practical Use Case
int[] trackGrades = {89, 93, 65, 68};
int sum = 0; // the sum of all the grades
for(int i = 0; i < trackGrades.length; i++) {
sum = sum + trackGrades[i]; // add each grade to sum
}
int average = sum / trackGrades.length;
System.out.println(average);
💖 💪 🙅 🚩
Greg Ross
Posted on November 4, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.