DEV Community

Cover image for Loops
Greg Ross
Greg Ross

Posted on • Edited on

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

Top comments (0)