Lesson 9
While, Do-while, For
Author: Brandon Fajardo (9/24/17). Updated (8/6/20)
Introduction
Review: In the last lesson, you learned how to utilize the special switch statement, including gaining an understanding of its components: case, break, and default.
Lesson topic: This lesson will move onto another category of incredibly useful statements: loops. These kinds of statements will continue to repeat until a certain condition is made false. We will cover while loops, do-while, and for loops.
Topic 1 - While Loop
Subtopic 1 - Definition
Currently, the statements we have written thus far only run once through and then stop. Wouldn’t it be nice to have code that repeats? That way we wouldn’t need to copy and paste it 100 times...
This is what a while loop provides.
While loop: a statement which repeatedly executes its body of code.
Notation:
The while loop will repeatedly execute so long as its condition is true. It will only stop if its condition becomes false (or you manually stop it from running).
Subtopic 2 - Outputting
A while loop is really good at quickly outputting many values.
EX:
*This prints out the integers from 1 to infinity.
*This prints out all the even numbers from 1 to infinity.
Note:
While loops are extremely fast. Simply incrementing by x++ may quickly reach into the 100,000s within milliseconds! This may cause many numbers to be skipped in the console due to the massive amount of output expected.
If the condition of a while loop is always true, nothing will stop the while loop from running. As a result, any code following such a while loop will not run. Properly using conditions with while looks will allow the code to continue on afterward.
Topic 2 - Do-While Loop
Do-while loop: a while loop where the body runs once first before the condition is considered.
Notation:
As described in the definition, do-while loops are a special case of a while loop where the body runs once first before the condition is checked. The body is preceded by “do” and a set of curly brackets. Finally, the condition has a semicolon (;) after it to mark the end.
Example:
In this example, number first starts at 0. The do body runs and increases the value of number by 1 and prints. Notice that the condition returns false for values of 0, but this is fine as by the time the condition is checked (after the do body is run), number is equal to 1.
Topic 3 - For Statement
Subtopic 1 - Definition
In programming, it is often useful to loop through a sequence of numbers. In fact, it is so useful, programming languages have a specific looping statement just for this situation.
for loop: a look through a sequence of numbers
Notation:
A for statement has a special kind of condition, unique from any other statements.
The components of its condition can be broken up into three parts:
initialization, declaration of any variables to be used within the loop.
termination, a traditional condition that determines when the loop should terminate.
increment, increment/decrement to the next number in the sequence after each iteration of the loop
A simple notation for this is:
Note:
The variables i and j are most often used in for statements, but any variable name is acceptable.
We can increment/decrement the variable we are using by any amount desired. *Consider the use of the Compound-Assignment Operators (+=, -=, *=, /=, %=) work.
Example:
Here, I want multiple entities to be rendered on screen. Using this for statement, I can render all 10 of them at once. *This is an example I used in a game I made.
Subtopic 2 - Nested For Statement
Oftentimes, programming takes on challenges with data that has multiple dimensions. For example, consider a table of information.
We need one for loop to go through each row, and within each row, we want a for loop to go through each column. We can solve these types of problems using nested for loops.
Nested (chained) for statements: a for statement inside of another.
EX:
Example:
As it turns out, nested for loops are quite important. They are often used for various things such as more complex algorithms There exist many types of algorithms in programming to accomplish different tasks. One type of algorithm is a sorting algorithm. These algorithms will take in a set of values (often numbers) and sort them from least to greatest. One such algorithm is called an insertion sort. This algorithm will look at each value in the set. If the value is misplaced, it will look for where that value belongs and place it in the correct spot. (As if it were inserting it where it belonged). As you might have predicted, the most common implementation of insertion sort uses a nested for loop:
Conclusion
Recap: In this lesson, you learned about loops in programming, including while looks, special do-while loops, and for loops.
Next: In the next lesson, you will learn about user input, one of the most unique things in these introductory lessons.
Last updated