Difficulties of rewriting the “for” loop

Difficulties of rewriting the “for” loop

Iteration/Loop:

Iteration means executing the same code block over and over, potentially many times. Now, a programming structure that implements iteration is called a loop. There are two types of iteration: Definite iteration, in which the number of repetitions is specified explicitly in advance e.g For loop. Indefinite iteration, in which the code block executes until some condition is met In Python, infinite iteration is performed with a while loop.

For loop:

To understand the for loop in python, let's suppose the following example: for in: <statement(s)> In which the is a collection of objects—for example, a list, tuple, or a range. The <statement(s)> in the loop body are denoted by indentation, as with all Python control structures, and are executed once for each item. The loop variable takes on the value of the next element each time through the loop. e.g.

for i in range(4): print (i)

Now, let's discuss some of the terminologies we used. Python Iterators: An iterator is an object that contains a countable number of values. Lists, tuples, dictionaries, and sets are all iterable objects. They are iterable containers which you can get an iterator from, while integer, float, and built-in functions are object types that are not iterable. iterable means an object can be used in iteration. Iteration is the process of looping through the objects or items in a collection.

a = ['foo', 'bar', 'baz'] for i in a: print(i)

Enumeration in for loop:

values = ["a", "b", "c"] count = 0 for value in values: print(count, value) count += 1

#the same for loop can be used as follow, easy and neat... values = ["a", "b", "c"] for count, value in enumerate(values): print(count, value)

While loop:

Now, To understand the while loop in python, let's suppose the following example: while: <statement(s)> <statement(s)> represents the block to be repeatedly executed, often referred to as the body of the loop. This is denoted with indentation, just as in an if statement. The controlling expression typically involves one or more variables that are initialized prior to starting the loop and then modified somewhere in the loop body. When a while loop is encountered, is first evaluated in a Boolean context. If it is true, the loop body is executed. Then is checked again, and if still true, the body is executed again. This continues until becomes false, at which point program execution proceeds to the first statement beyond the loop body.

n = 5 while n > 0: n -= 1 print(n)

break and continue work the same way with for loops as with while loops. break terminates the loop completely and proceeds to the first statement following the loop: continue terminates the current iteration and proceeds to the next iteration: there is another keyword used in python called Pass, this statement doesn’t do anything: it’s discarded during the byte-compile phase. But for a statement that does nothing, the Python pass statement is surprisingly useful.

n = 5 while n > 0: n -= 1 if n == 2: break print(n) print('Loop ended.')

n = 5 while n > 0: n -= 1 if n == 2: print('skipped') continue print(n) print('Loop ended.')

n = 5 while n > 0: n -= 1 if n == 2: print('passed') pass print(n) print('Loop ended.')

What are the difficulties writing the “for” loop statement to the “while” loop for the following code:

sequence =[1, 2, None, 4, None, 5]

total = 0 for value in sequence: if value is None: continue total += value # total = total + value print(total)

sequence = [1, 2, None, 4, None, 5] total=0 while total < len (sequence): if sequence[total] == None: total += 1 continue print('Current integer :' ,total, sequence[total]) total += 1

so, in this case, if I try to add the sequence [total] it will prompt us with a list out of range because the range starts from 0. so in this case we have the range from 0-5 but the actual list is 6 numbers i.e total=6 and since the list include non-integer values so we can’t add them directly. so, the only difficulty, encountered was adding the list values in the while loop…

total+=sequence[total]

Now, if we had an integer list including no None values, it's kind of easier to do….

# initialize a list of integers num_list = [1, 5, 4, 45, 12] # initialize variable to hold the sum of list elements

total = 0 # number of elements in list list_size = len(num_list) # variable to keep track of loop loop_counter = 0 # iterate over the list while loop_counter < list_size: # add current list element to the sum total = total + num_list[loop_counter] # increment loop index loop_counter = loop_counter + 1 print("Sum of elements of list is", total)

or, we could just use the sum method to do the job

# initialize a list of integers num_list = [1, 5, 4, 45, 12] # add list elements using sum function total = sum(num_list) print("Sum of list elements is",total)

REWRITING THE FOR LOOP - The solution of the program used

sequence = [1, 2, None, 4, None, 5]
total=0
loop_counter=-1
while loop_counter < len (sequence)-1:
loop_counter+=1
if sequence[loop_counter] is None:
continue
total+=sequence[loop_counter]
print(total)