Respond to discussion Homework
Respond to discussion 150 words minimum
1. Explain when we would use pretest and posttest loops.
The differences from pretest and posttests loops are quite simple. Pre-test loops check the stopping condition before executing the loop and with Post-test loops the condition checking happens after executing the body of the loop. As for which to use and why, it usually depends on the preference of the programmer or the logic behind the problem because in most cases both the pre and post-test will come to the same conclusion.
If you would like the comparison to be performed at least once then you would go with a pre-test set-up. “A post-test loop is most useful when the code must be executed at least once (Scott, 2009).
2. How do you include a “loop” structure programming in Python?
If a programmer wants to put a loop into his program then they must first start one by creating a loop statement. A couple of the more common words for this would be “while” or “for”. An example of a loop would be:
while True:
print (“Good Job!”)
3. How do you control a loop?
To control a loop in programming it simply means providing a statement that controls the path that code execution takes. To control a loop the developer must stipulate the value of an integer and if the conditions are met the loops goes down a designated path. Some of the decision statements would be: “If” statements, “If, else”, and “Nested.” An example of this would be:
x = [2 , 5, 8]
for c in x:
Print c
References
Scott, M. L. (2009) Programming language pragmatics (3rd ed). San Fransisco, CA: Morgan Kaufmann. Retrieved from http://slaptijack.com/programming/decision-and-loop-structures/
-Jonathan
Respond to discussion 150 words minimum
Hello class,
Again sorry for posting so late but work and life got ahead of me again. Hope all is well.
So starting off with we will explain what a pretest and a posttest loop, I will talk about how I would include a loop structure program in python and finally I will discuss how to control a loop. So from the research ive done a pretest loop is the code that activates when a condition is be checked at the start of a loop. So if the condition is not met then the code would skip over the loop and move onto the next section of code. The Posttest loop is an action that occurs at the end of the loop. From my interpretation a posttest loop will check that a condition is met after the loop to make sure that a it preformed until the result is satisfied.
Moving on we will talk about controlling loops. Controlling a loop is to make sure that a loop starts and ends when needed. One way to do this is to use the while statement.
My example is
#the idea is until c is equal to 5 it will state "c is less than 5".
if c < 5:
print ('c is less than 5')
#the idea is that i start the count as 0 and then it would start adding 'a' or 1 to it.
count = 0
while count <c:
print ('this is loop #'),count
count =+ a
so in this example I am controlling the loop by setting the loop to end once the value of c==5. Or at least that is how I think I am controlling it. I realized the first week of this class that I will prob be wrong most of the time but that is ok because everything I do in this class is a learning experience.
Thank you for your time..