Conditional, Iteration and Loops

Conditional, Iteration and Loops
Loops are one of the building blocks that have a fundamental role in programming (Hall, & Stacey, 2009). There are two types of loops namely while loops and the other one is for-loops. In instant hacking, a programmer may use the loops to ask the computer to keep checking on a certain project that is in operation. While using an example of various types of food, the following illustrations will demonstrate how the two types of loops work after running in a Python IDLE Editor.
Example1.
While using the “for loops”, they are the simplest to run and offer a repetition command in the computer. In the instant hacking, the “for loops” are used in repeating certain set number of times according to the program.
for food in “meat”, “chips”, “eggs”
: print “I want”, to eat
The following example also show how the “for loops” runs
for number in range(1,100):
print “Hello, world!”
print “Just”, 100 – number, “more to go…”
print “Hello, world”
print “That was the last one

According to the above illustration, this means that the programmer ought to print for every element in the list that he/she want to eat. The block located in the loop will have to be executed. Every time the program assigns the current element to the variable food.
Most of the programmers use the while loop in order to get a reminder every time a program is in operation (Andrew, 2007). While using the above example of foods, the programmer may be required to check the meat many times that is a nice solution to make. In many of the case, the programmer may not know whether the food is hot or not. The best option to use in such a case is the while loop. The following illustration shows how to use the while loop for the above case.
In this example, it is advisable to note that there are some useful functions stored as modules and can be imported later in the program. In the case below, the programmer uses the function sleep from the module time that often comes with python. In most cases, “for loop” is the best to use in various counted loops.
# Spam-cooking program
# Fetch the function *sleep*
from time import sleep
print “Please start cooking the spam. (I’ll be back in 3 minutes.)”
# Wait for 3 minutes (that is, 3*60 seconds)…
sleep(180)
print “I’m back :)”
# How hot is hot enough?
hot_enough = 50
temperature = input(“How hot is the spam? “)
while temperature < hot_enough:
print “Not hot enough… Cook it a bit more…”
sleep(30)
temperature = input(“OK. How hot is it now? “)
print “It’s hot enough – You’re done!”

Summary
The programs associated with the while loops are very flexible. This means that a programmer may use the while loops to repeat the block of various codes in case the condition is true. However, it is advisable to consider that the both loops may are flexible but a programmer may encounter certain difficulties while in operation. A programmer may consider having more functionality although this option is not necessary. While applying the loops, the programmer may use the “else Clauses in Loops” that are break statements (Hall, & Stacey, 2009). A programmer may use these break statements in case something happens in between the operation of the program.
Reference
Andrew, H. (2007). Game programming: the L Line : the express line to learning. New York: John Wiley and Sons.
Hall,T & Stacey, J. P. (2009).Python 3 for Absolute Beginners. New York: Apress.

 

Latest Assignments