gregrossdev

Greg Ross

Posted on November 4, 2021

Loops

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);
Enter fullscreen mode Exit fullscreen mode
int condition = 0;
while (condition < 5) {
  System.out.println(condition);
  condition++;
}
Enter fullscreen mode Exit fullscreen mode

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);
}

Enter fullscreen mode Exit fullscreen mode
String[] people = {"Roy", "Gee", "Biv", "Mazza"};
for (String eachPerson : people) {
  System.out.println(eachPerson);
}
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
gregrossdev
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.

Related

Access modifiers
beginners Access modifiers

May 2, 2022

Naming matters
beginners Naming matters

March 11, 2022

OOP Principles
beginners OOP Principles

November 17, 2021

Collections
beginners Collections

November 10, 2021