# __**Intro to Iteration**__
* When writing statements that might repeatedly appear in code, you can do 2 things.
* You can copy and paste the statement multiple times, and spend hours writing code.
* A more efficient way would be to use a conditional statement that is referred to as a loop.
* **Note:** On the AP Exam you will be expected to know what *while* and *for* loops are.
# __***While*** **Loop**__
* This loop cycles through again and again, while the condition is considered true.
* It’s similar to the *if* statement.
* Ex: Say that you have a paintbrush that is soaked in paint, and hopefully you don’t put away the paintbrush until it is clean. It’s entirely based on the cleanliness of the paintbrush. You will only put the brush away if it’s true that it is clean.
* If the condition is false then the loop will continue to run, until the condition returns true.
* However, if the condition is true then it will execute the statement, and it will exit the loop, and if there is another condition present then that will start running instead.
* However, sometimes loops aren’t well written, because sometimes there might not be any sort of statements that go through the loop to help change the value of the expression to be true.
* This would be considered an **infinite loop,** which, as its name suggests, means that the loop goes on forever.
* It has the similar feeling of TikTok freezing on you when you're on your daily Tiktok spiral.
* So an infinite loop isn’t desirable in all situations.
* **Note**: On the AP Exam, you need to be able to trace code in multiple-choice questions and detect infinite loops. In the free-response questions, you need to write code that is free of infinite loops.
* All loops are dependent on the conditions that are found within them.
* If there are multiple conditions found in the loop when can use boolean operators to simplify it. (View Chapter 5 for a review of boolean operators)
* Let’s look at our paintbrush example again.
* Ex: The paintbrush needs to be cleaned and dried for it to be put away, and if it isn’t then don’t put the paintbrush away.
* In this case, you would end up using the *&& (and)* boolean operator, because while the paintbrush is both wet and dirty, then only will the paintbrushes be put away.
* So if the paintbrush is dirty and wet then don’t put away the paintbrush, otherwise, put away the paintbrush.
# __**The**__ __***for*** **Statement**__
* This is just another type of loop and is just as effective as a *while* loop. The only difference is that one of the loops may make the code more simplified than the other.
* Since this loop needs more components, it can help to avoid an infinite loop in some situations.
* Here are some steps for how a *for* loop executes:
* The initializer will start first and it will start the loop off.
* The conditions will be checked to see if they are true or false.
* The statements will execute.
* The incrementer will increment the number, and in turn will change it.
* The process above starting from step 2 will continue to repeat until the value turns to false.
* Once the value turns false the loop ends.
* **Ex:**
```java
for(int num = 1; num