Responses on loop program discussion help
Respond to each post in 150 words.
1 of 2
This week we learned about using loops in programming. A loop is when you have a program that repeats. In a program a loop is like a circle and the program will continue to go around repeating the tasks until the loop has ended.
The two types of loops are for and while. You would use a for loop when you know how many times a task will need to be accomplished. For example if you are reading 50 pages a day in a book, you already know that you will stop at 50 pages. You would use a while statement when you don’t know how many times you will have to perform a task. For example if you are working at a video game store and your boss gives you a box of used games and tells you to check each game box to make sure the game and instructions are inside. You don’t know how many games are in the box, so you can’t say exactly when the task will be complete. The only thing you know for sure is that the task will be complete once the box of games is empty and all games have been checked.
An example for putting a loop in python is
letterNum = 1
for Letter in “Derrick”:
print(“Letter “, letterNum, ” is “, Letter)
You can control a loop using a break, continue, pass, or else statement. The break statement allows you to break out of a loop if a specific condition is met. The continue statement allows the program to continue processing the next element, skipping over and not showing the element that you wanted left out. The pass statement is just like the continue statement except it completes the code and shows the element that was in your if statement along with whatever you put in the print() for that element. Last, with the else statement you can execute a code even if there is nothing to process.
2 of 2
As we circle back to posting another forum, we will loop for eight weeks and while class < complete. If only that were two hundred and fifty words worth of exploration. Looping is a simple way to repeat the steps previs would take. Iteration is generally a repeat of the last procedure with something changed. All of the examples shown in the class material thus far take an integer and add a value in the next iteration of the procedure.
The while loop can be fun and useful. Using my example above. While technology continues to change I will strive for more education. In this use case, my while loop will run for the foreseeable future and possibly indefinitely. I can end this loop my adding a statement allowing for the constitution to no longer be true. While technology is needed to feed me, I will strive for more education. This allows for the sudden and hoped for winning lottery ticket.
Including a loop structure seems rather straight forward. All that is needed to start a loop is the loop statement or command â€œforâ€. For can be followed by in. Python will change the color letting you know you have typed a command. Donâ€™t forget that a loop needs some form of output. The print command has been shown in the examples so far.
I will add one more. Should you wish to loop through a list, you can create an array and then use â€œfor eachâ€ to run through the list held in your array.
I hope that my goofy little example at the begging was not too much of a distraction.