top of page

Coding 101: The While Loop

No matter what programming language you start to code in, it is important that you understand the basics to start and build from there. One of the core functionalities in programming that is used throughout every object-oriented programming language is the “while” loop.

This article will:

  • Define what a while loop is

  • Breakdown the structure of a while loop

  • Show the different comparisons that can be used in a while loop

  • Show how it is used in code




What is a while statement?

A while statement is a control flow statement in code that tells your program to repeat a section of code until a certain criterion is met. In English, sometimes you may want to repeat a certain action until the desired result is met. An example of this could be you want to walk a mile a day until you lose 10 pounds. Once you lose 10 pounds you no longer want to walk a mile every day. You will keep repeating the action of walking until the criteria is met of you losing 10 pounds.




While loop structure

Criteria

There are several different parts of the while loop. First criteria must be defined to check against. If the criteria is met you don't perform the action in the while loop. If it is not met, you do perform the action in the while loop. The criteria in the diagram above is you losing 10 pounds. You weigh yourself every day to check for this criteria.

Action

Once inside the while loop, an action should take place. In the diagram above the action is you being excited to work out.

Alter

While still inside the loop before checking the initial criteria again we need an altering action so that we become closer to meeting our criteria. In the diagram above this is you going for a walk for a mile today. This will theoretically bring you closer to losing 10 pounds. After this, you go check to see if the criteria is met.






What are the different comparisons that can be used in a while loop criteria check?

  • Equal to(==) -code executes if object a is equal to object b. For example, if 5 is equal to 5.

  • Greater than(>)-code executes if object a is greater than object b. For example, if 5 is greater than 4.

  • Less than(<)-code executes if object a is less than object b. For example, if 5 is less than 6.

  • Greater than or equal to(>=)-code executes if object a is greater than OR equal to object b. For example, if 5 is greater than 5.

  • Less than or equal to(<=)-code executes if object a is less than or equal to object b. For example, if 5 is less than 5.

  • Not equal to(!=)-code executes if object a is not equal to object b. For example, if 5 is not equal to 6





How is this actually used in code?

If you are new to code anything marked with # is considered a comment and is not run with the code. It is usually used for note-taking. I will place it above each line of code to explain what the line of code is doing. Also please note the programming language I am using is Python.



As you can see this program relates to our previous example. We start out with 0 pounds lost. We then check the criteria to see if pounds lost is 10 or greater. While pounds lost are not equal to 10 then we execute the code in the loop. Inside the while of pounds lost being loop we first print to the output “ I am excited to workout, I have lost this amount of pounds” and then the value of pounds lost. We then alter the value of pounds lost by adding 2 to it. We then repeat this cycle until we reach pounds lost equal to 10. Once the criteria is met we no longer execute the code in the while loop. We then print to the output “I will congratulate myself”.

8 views0 comments

Recent Posts

See All
bottom of page