13.07.2015 Views

Analysis of Structures - Baustatik-Info-Server

Analysis of Structures - Baustatik-Info-Server

Analysis of Structures - Baustatik-Info-Server

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2.7. LOOP FOR REPETITIONS Page 23n = 10# factorial inputp = 1 # result variable must be initalized by 1for i in range(2,n+1): # the counter runs from 2 up to np *= i# here we perform the productprint "%3d! = %10d" % (n,p) # write the result into the console>>>... console window ...10! = 3628800The second loop type, the while loop is working implicit with a boolean expression, which controls thebreak condition. If we want to implement the factorial using the while loop we get the following code.n = 10# factorial inputp = 1 # result variable must be initalized by 1i = 2# the counter runs from 2 up to nwhile i >>... console window ...10! = 3628800The next example shows a nested loop. Very important is the correct code block indent.for i in range(0,4):# outer loopfor j in range(0,2):# inner loopprint "i:%2d, j:%2d" % (i,j) # print counter variables>>>... console window ...i: 0, j: 0i: 0, j: 1i: 1, j: 0i: 1, j: 1i: 2, j: 0i: 2, j: 1i: 3, j: 0i: 3, j: 1For the detailed controlling <strong>of</strong> the loops cycles two statements are available.• continueIf the continue statement is used a cycle is immediately stopped and the next cycle is started.• breakIf the break statement is used a cycle is immediately stopped and the loop is exited.The next example shows an application <strong>of</strong> the continue statement. A loop is performed with the values0 · · · 4. The cycle with the counter 2 is prematurely canceld.2.12.2011

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!