COSC-1315 2807 12B Introduction to Computer
Programming
For this discussion, you saw in the chapter that the while loop is
a pretest loop. What is a post test loop and what example can you
come up with? Lastly, what explanation can you find as to why
Python does not have a post test loop?
(1.)
Post Test loop, where the condition comes after the body. Using a while loop first checks the statement within the loop and then goes to check the condition to determine if the statement will run again or not.
Example: using c++
#include <iostream>
using namespace std;
int main()
{
int i = 0;
do {
cout << i << endl;
i++;
} while(i<5);
return 0;
}
(2.)
In Python, there is no structure made for do-while loop or post test loop, because if the post-test loop, it will give you value like an infinite loop and will never close the loop. That's why we use break to stop the while post-test loop.
Basically there is no logic built for post test loop.
In Python, we use while loop like this
i = 0
while True:
print(i)
if i >= 5:
break
i = i+1
Thumbs Up Please !!!
Get Answers For Free
Most questions answered within 1 hours.