The XCSB for statement is a loop construct designed to repeat a sequence of statements. The general format of a for statement is:where:for init_expr while cond_expr step step_expr do statement_list doneinit_expr is the loop initialisation expression executed the first time through the loop. This is normally used to initialise the loop control variable.a concreat example would becond_expr is the expression that is executed each time through the loop and causes the loop to be terminated when it produces a 0 result. cond_expr is execute immediately after the init_expr and before the first statement in the statement_list, if cond_expr equates to 0 none of the statements in the statement_list are executed. cond_expr is normally used in conjunction with the loop control variable to limit the number of times the loop is executed
step_expr is the expression that is executed at the end of each pass through the loop. It is executed immediately after the last statement in the statement_list and before the cond_expr. step_expr is normally used to increment or decrement the loop control variable at the end of each pass through the loop.
statement_list is a list of statements to be executed. This list may consist of no statements at all, one statement on its own or multiple statements each on a seperate line. The statement_list starts after the for statement and finishes before the matching done statement.
This example executes the statement 'x = x + j' 10 times. The first time j is 1, the second time j is 2, the third time j is 3 and so on until j is 11 at which time the loop stops before executing the 'x = x + j' statement again. This results in the numbers 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10 being added together. Another example would bex = 0 for j=1 while j<=10 step j=j+1 do x = x + j donethis example is similar to the previous one except that it starts at 10 and works down to 1. It would effectively add the numbers 10, 9, 8, 7, 6, 5, 4, 3, 2 and 1 togetherx = 0 for j=10 while j>=1 step j=j-1 do x = x + j done