The counting for Statement A most versatile loop























- Slides: 23
The counting for Statement A most versatile loop Copyright © 1999 -2010 Curt Hill
The for • The for is the most popular of the C family of language loops • This is because the initialization, test and advancement are all obvious as part of the header of the statement • Many other languages have a similar statement but this one has very few restrictions on the pieces • It is a leading decision loop Copyright © 1999 -2010 Curt Hill
Simple Example int i; for(i=1; i<= 10; i++){ System. out. println(i); } This will write the integers 1 -10 on separate lines of the console. Copyright © 1999 -2010 Curt Hill
How example executes • First, it does the initialization – i=1 – Just an assignment • Second, it does the test – i<=10 • Third, it does the body – System. out. println • Fourth, it does the increment • It repeats the test/body/increment until the test fails Copyright © 1999 -2010 Curt Hill
The generalized for • The general form is: for(initialization; test; advancement) one statement • The for is a reserved word • The parentheses are required • The three items are quite flexible – Must be separated by a semicolon • The one statement is the body of the loop Copyright © 1999 -2010 Curt Hill
The Initialization • Always executed and always prior to first execution of the test • Usually is a simple assignment but may be more complicated – The same restrictions as on any assignment statement for (c = Math. abs(3*i); c< … • The variable may also be declared there Copyright © 1999 -2010 Curt Hill
Control Variable • A typical for has the same variable referenced in: – The initialization – The test – The advancement for(i=0; i<n; i++)… • Such a variable is known as the control variable – Its value controls the iteration of the for Copyright © 1999 -2010 Curt Hill
Variable Declaration in a For • The variable in the initialization may be declared at that time: for(int j = 0; j < k; j++) • It does not have to be a pre-existing variable • Thus the for and the compound statement allow declarations within them • This is the only location that allows a declaration within the for Copyright © 1999 -2010 Curt Hill
Scope • So where is the variable known? • The variable declared in a for is known only in the for header and the one statement that follows it – This is the body of the loop • The variable is created by the for • It is destroyed when the for is exited • Consider the next example Copyright © 1999 -2010 Curt Hill
Scope of a for control variable // larger scope int j = 5, k=Integer. parse. Int(ed. get. Text()); int sum = 0; for(int m=2; m<k; m++){ sum = sum*m – k/2; // scope of m k -= 2; } int val = 1; for(int m = 1; m<j; m++) val = val*m; // second scope of m Copyright © 1999 -2010 Curt Hill
Final words on initialization • The control variable does not have to be any particular type • It may also be left out • Suppose that the variable that you want as a control variable is already properly set – Leave out the initialization – Retain the semicolon int a= k*2, b=2; for(; b<k; b++)… Copyright © 1999 -2010 Curt Hill
The test • The test is evaluated before every execution of the body of the loop • If it is true then the body is executed – Otherwise the loop is exited • The test is any boolean expression • Anything that produces a boolean is acceptable • Including things we will not see in this presentation Copyright © 1999 -2010 Curt Hill
A Condition • • • Any expression of type boolean Usually a comparison May include boolean methods May include boolean operators Unlike C/C++ must be a real boolean Copyright © 1999 -2010 Curt Hill
Comparisons • A comparison has the form: expr op expr • Expr may be any expression – Involves constants, variables, operators – Produces a value • The comparison operators include: –< > >= <= == != • Do not confuse = with == • The precedence of comparisons is lower than any arithmetic Copyright © 1999 -2010 Curt Hill
Example comparisons: • OK: a<b // Simple a>b*2 a*b<c-4*d // Not so simple • Not so good: a // Only legal if a is boolean • Maybe x. y(z) // A boolean method Copyright © 1999 -2010 Curt Hill
Mixed mode again • The same mixed mode rules apply to comparisons as arithmetic • Comparing an integer to a double automatically casts the integer as a double for the purposes of the comparisons • The arithmetic may generate casts • The comparisons may generate additional ones Copyright © 1999 -2010 Curt Hill
More on the test • It is usually some test on the control variable • Not required to be related to either the initialization or advancement but usually is k = sc. next. Int(); for(int i = 1; k>0; i++){ k /= 2; } Copyright © 1999 -2010 Curt Hill
Advancement • Third piece of the for header • Only executed after the body of the loop • Always before the test on the second and subsequent times through the loop • May be any expression – Usually a ++ or – applied to control variable Copyright © 1999 -2010 Curt Hill
Advancement examples • The usual is count by one: for(i=0; i<j; i++) … for(i=0; i<j; i--) … • The count by n: for(i=0; i<j; i+=n) … • Power of 2 for(i=1; i<j; i*=2) … • Something else: for(d=1. 0; d<m; d+=Math. log(d)) Copyright © 1999 -2010 Curt Hill
The Body of the Loop is one statement • • Compound statement Assignment Method call Empty statement • Considered in another presentation • Another for • Also considered in another presentation • Any other statement Copyright © 1999 -2010 Curt Hill
For flow chart Suppose the following loop: for(a; b; c) d; a c b true false Copyright © 1999 -2010 Curt Hill d
Order of execution • Suppose the following loop: for(a; b; c) d; • The order of execution is: a, b, (d, c, b)* • Thus, these are possible: a, b, d, c, b , d, c, b … Copyright © 1999 -2010 Curt Hill
Example: Turtle Star • Consider the following method: static void star(Turtle t, int length){ for(int i = 0; i<5; i++){ t. forward(length); t. turn(144); } // end of for } // end of star • This is easier than five copies of forward and turn Copyright © 1999 -2010 Curt Hill