Loops

Here is a summary of this page:

  • What is a While Loop?
  • What is a For Loop?
  • What is an Iteration?
  • What is a For Each Loop?
  • What is a Do-While Loop?

Loops are part of programming everywhere, in some sort of way. Also, they are used in a lot of applications and interview questions.

Often times in programming, we will want to repeat a particular groiup of statements multiple times. Loops give us a control structure to do so, without having to write out the steps multiple times.

What is a While Loop?

Basically, the logic for while loops is:

While(<boolean expression) then Execute

Just like the if-else statements that we previously went over, we can include multiple executable statments in a while loop:

while(<boolean expression>){
  statement1;
  statement2;
  statement3;
}
statement4;

Let’s outline what goes on above:

  1. Check if the boolean expression is true
  2. If boolean expression is true, execute statement1, statement2, and statement3.
    • After executing statement3, go back to step 1.
  3. If boolean expresison is false, skip to after the end of the while loop and execute statement4

Here’s an example of writing a program that adds two to a variable (0) until it’s over 100:

int main(){
  int val = 0;
  while(val < 100){
    val += 2;
  }
  return val;
}

What is a For Loop?

Many programming languages contain more than one type of loop. Another common loop is the For Loop. The common structure for a for loop is as follows:

for(<initial statement>;<boolean expression>;<increment statement>) { statement(s); }

As mentioned with the while loop, usually there is more than one statement in the body of a for loop, thus, we will usually have a block of statements:

for(<initial statement>;<boolean expression>;<increment statement>) {
  statement1;
  statement2;
  statement3;
}
statement4;

Let’s walk through the execution of this For Loop:

  1. Execute the initial statement
    • This is typically the initialization of a counter of some kind
  2. Evaluate the boolean expression
  3. If the boolean expression is true, execute statement1, statement2, and statement3
    • If the boolean expression is false, go to step 6
  4. Perform the increment statement
  5. Go back to step 2
  6. If the boolean expression is false, skip over the loop body and execute statement4

Let’s go through an actual example:

int sum = 0;
for(int count = 0; count <= 100; count++){
  sum += count;
}

So before reading below, try to understand what it’s doing first. It helps to try and teach yourself before just getting the answers.

  1. Declare and initialize the Integer variable sum to 0.
  2. Execute the initial statement of the For Loop
    1. Declare and initalize the Integer variable count to 0
  3. Check the boolean expression of “count <= 100”
    1. If count is greater than 100, then we proceed after the For Loop
  4. If count < 100, then add the counter’s value to the sum
  5. Perform the increment statement: increment count by 1
  6. Go back to Step 3

Do you know what this loop is actually doing?
It’s adding up every Integer up to 100, so basically:
sum = 0+1+2+3+4+5…+99+100

How to choose between a for and a while loop:
First, it should be noted that anything you can do with a For Loop, you can also do with a while loop. But, in certain situations, a for loop is easier to read than a while loop that would do the same thing.

In for loops, it’s easier to see the structure, to identify the counter, and to get an initial impression on how many times the loop might run without (usually) having to look inside of the loop to see what’s going on.

What is an Iteration?

An iteration in programming has to primarily do with loops. One “loop” in a loop, is called an Iteration, simple as that.
So when someone or some programmer asks you, “How many iterations until your program fails?” you’ll more of an idea of what they’re talking about.

What is a For Each Loop?

So I might be jumping a little ahead here, but only because the next page goes into Arrays.
Arrays are just groups of the same types of data. So we can have an array of Integers, such as int[] arrayOfNumbers = [1,2,3].

Now that you know exactly what an array is, a For Each loop is a tool that can be used to loop through an array:

int sum = 0;
forEach(int number : arrayOfNumbers){
  sum += number;
}

Can you guess what’s going on here? Basically, it’s taking each number from the arrayOfNumbers and adding it to sum:
sum = 1+2+3

Also, keep in mind that some languages format loops differently. A “For Each” might be expressed almost the same as a For Loop, such as this:

int sum = 0;
for(int number : arrayOfNumbers){
  sum += number;
}

This loop does the exact same thing as the structure outlined one, they’re just labeled differently.

What is a Do-While Loop?

A Do-While loop is very similar to a While Loop, except the first iteration is always performed. For example:

do
{
   statement1;
} while(<boolean expression>);
statement2;
  1. Execute statement1
  2. Evaluate boolean expression
    1. If boolean expression is TRUE, then go back to Step 1
    2. If boolean expression si FALSE, then proceed to statement2;

That concludes the initial creation of the Loops page. I’m sure there will be more information added later on or once I start getting feedback on the site.
Thanks for reading!