# FOR (array sum)
a = [4, 3, 5, -1, 7, 0, 2]
print("sum of array:", a)
# for
s = 0
for e in a:
s += e
print(s)
# while
s = 0
i = 0
while i < len(a):
s += a[i]
i += 1
print(s)
# do_while
s = 0
i = 0
while True:
s += a[i]
i += 1
if i == len(a):
break
print(s)
print()
# WHILE (counting occuring value in array)
val = 9
b = [val, 2, 5, val, 0, val, 3, 1, val, 5, 8, 2, val]
print("counting occuring value", val, "in array:", b)
# while
ind = -1
count = 0
while val in b[ind+1:]:
count += 1
ind += b[ind+1:].index(val) + 1
print(count)
# do_while
ind = -1
if val in b[ind+1:]:
count = 1
while True:
ind += b[ind+1:].index(val) + 1
if val not in b[ind+1:]:
break
count += 1
else:
count = 0
print(count)
# for
count = 0
for e in b:
if e == val:
count += 1
print(count)
print()
# DO_WHILE (product of (firsts) positive inputs)
print("product of firsts positive inputs")
# do_while
prod = 1
while True:
e = int(input("Enter positive integer: "))
if e <= 0:
break
prod *= e
print(prod)
# for
prod = 1
for i in range(1000000000):
e = int(input("Enter positive integer: "))
if e <= 0:
break
prod *= e
print(prod)
# while
prod = 1
e = int(input("Enter positive integer: "))
while e > 0:
prod *= e
e = int(input("Enter positive integer: "))
print(prod)
print()
Firstly it should be noted that in python there are no do_while-loop, and
it have to be coded with while-loop by using if-break statement.
Secondly, we have to say, where we use each loop:
for - when we have to go through the iterated object
while and do_while - when we have to use check for some condition
(the only difference is that do_while-loop have to be executed at least
1 time, unlike while-loop can be executed 0 or more times)
In first example 'sum of array' it is preferred to use for-loop, as we can
directly go through array and make summation for every element.
Meanwhile in while-loop we have to check every time for boundary limit of
array and increase index.
And in do_while-loop we are assuming that while-loop is infinite and have
interrupt it with additional if-break statement. In this example it is
bad practise to use do_while-loop as if array is empty we will receive
an error taking a value of 0-index element (as loop is executed as least
one time).
In second example 'counting occuring value in array' it is preferred to
use while-loop, as it takes away 1 extra operation (checking if value is
in array, coding this we avoid Error of find function, in case there is
no val in b) which have to be done in do_while-loop.
And for-loop is bad coding, because it go through whole array (not using
slices) and check every element and takes O(n)-complexity.
In third example 'product of firsts positive inputs' it is preferred to
use do_while-loop, as the input have to be done at least one time. In this
case we can declare variable for input, and check our condition inside the
loop, unlike while-loop. The code also become more elegant.
And for-loop takes very big range of input which have to be stopped with
if-break statement.
Comments
Leave a comment