Group members: Marcus, Hao, Ameer, Kayden
What is Iteration? In coding, "iteration" means to repetitively perform a set of actions or operations, typically with the purpose of processing or examining a collection of items, such as a list, array, or other data structures. It is a fundamental concept in programming and is often used when you need to perform a specific task multiple times or when you want to process a collection of data, such as a list or an array. Below is an example of an iteration, and its uses. ```python for number in range(5): print(number) ``` 0 1 2 3 4
You can also create an iteration using words or a list. Examples below.
```python
fruits = ["apple", "banana", "cherry", "date", "fig"]
for fruit in fruits:
for number in range(3):
print(fruit)
```
apple
apple
apple
banana
banana
banana
cherry
cherry
cherry
date
date
date
fig
fig
fig
In this example, the input was a list of fruits, which is then put into the code where the fruits will be printed and repeated 3 times, because of the iteration of "for number in range(3)" keyword "range(3)"
Popcorn Hack #1
now create your own iteration, wheter that be a list of numbers or words, its up to you. (3 minutes)
```python
groupMates = ["Will", "Jayden", "Howie", "Nihar"]
for groupMate in groupMates:
for number in range(3):
print(groupMate)
```
Will
Will
Will
Jayden
Jayden
Jayden
Howie
Howie
Howie
Nihar
Nihar
Nihar
Indefinite Iteration
Indefinite iteration, also known as conditional or while-loop iteration, involves repeating a block of code as long as a specified condition remains true. Unlike definite iteration, where you know the exact number of iterations in advance, with indefinite iteration, the loop continues as long as the condition is met. If the condition becomes false at any point, the loop terminates. Below is an example of the layout
```python
while condition:
# Loop body
```
Indefinite Iteration Indefinite iteration, also known as conditional or while-loop iteration, involves repeating a block of code as long as a specified condition remains true. Unlike definite iteration, where you know the exact number of iterations in advance, with indefinite iteration, the loop continues as long as the condition is met. If the condition becomes false at any point, the loop terminates. Below is an example of the layout ```python while condition: # Loop body ```
Below is an example code of indefinite iteration: ```python while True: user_input = input("Enter something (type 'exit' to quit): ") if user_input == "exit": break print("You entered:", user_input) ```
</br>
</br>
</br>
</br>
Loop Control Statements Loop control statements are used to manage the flow of loops and determine when to exit, skip, or repeat iterations within a loop. There are three primary loop control statements:
</br> break is used to exit a loop when a specific condition is met. Example: Exit a loop when the number 5 is encountered. ```python for i in range(10): if i == 5: break print(i) ``` 0 1 2 3 4
</br> continue is used to skip the current iteration of a loop and move to the next. Example: Skip printing the number 2 in a loop. ```python for i in range(5): if i == 2: continue print(i) ``` 0 1 3 4
</br> In functions, return is used to exit the function and any loop within it. Example: Exit a function and loop when a target value is found. ```python def calculate_rectangle_area(length, width): area = length * width return area length = 5 width = 3 area = calculate_rectangle_area(length, width) print("The area of the rectangle is:", area) ``` The area of the rectangle is: 15 Above is more of a complex iteration, but we use the return statement to specify that the result of the function is the value stored in the (area) variable. # Homework! create a code for each of the different scenarios below:
</br>
</br>