######## DM2 ######## basics 1: loops ######## default loop from 0 to 7 print( "\nfor loop a" ) for i in range( 0, 8 ) : ## immutable sequence type, similar (!) to: initiate 0, as long as less than 8, add 1 (i=i+1) print i ## print variable i in the indented block ## instruction block ends with the end of the indent ######## loop from 2 to 7, add 2 at a time print( "\nfor loop b" ) for i in range( 2, 8, 2 ) : ## initiate 2, as long as less than 8, add 2 (i=i+2) print( i ) print( "done!" ) ## still part of the loop print( "final i-value: " + str(i) ) ## scope irrelevant, i is available everywhere ######## loop from 1 to 32, whereby the counter is doubled each time NOT WORKING print( "\nfor loop c" ); a = 1 for i in range( 1, 32, a ) : ## as immutable sequence, once initiated no changes possible print( i ) a *= 2 print( "final i-value: " + str(i) ) print( "final a-value: " + str(a) ) ######## further details see ######## https://docs.python.org/3/library/functions.html#func-range ######## loop from 1 to 32, whereby the counter is doubled each time print( "\nwhile loop d" ); a = 1 ## set variable a while ( a<32 ) : ## as long as less than 32 print( a ) a *= 2 ## double (a=a*2)