Chapter 3 Types, Operators, and Expressions; While and For Reference: Brooks, Chapter 3 2 3)



Download 283.95 Kb.
Page3/3
Date07.08.2017
Size283.95 Kb.
1   2   3




  • A common programming mistake when one uses a while construct is to either forget the initialization of the variables or to forget the increment of the loop index

  • The for construct puts both of those things at the top

  • Programmers are less likely to forget these things

  • A for loop is simply a more “general” while loop




Example - Fibonacci Numbers


  • Here is the version which uses a while loop

prev1 = prev2 = 1; /*first two numbers*/

sum = 2; /*sum of the first two numbers*/

count = 2; /*calculate 3rd number on*/

while (count++ < N)

{

curr = prev1 + prev2;



sum += curr;

prev1 = prev2; /*advance prev1*/

prev2 = curr; /*advance prev2*/

}

prev1 = prev2 = 1; /*first two numbers*/

for (sum = 2, count = 2; count < N; count++)

{

curr = prev1 + prev2;



sum += curr;

prev1 = prev2; /*advance prev1*/

prev2 = curr; /*advance prev2*/

}


  • In this example, we use the comma operator to form a longer, more complex initialization expression

  • We could also use it for create longer next expressions

  • The logic of the for loop is slightly different here, but we also got rid of rather tricky code that the while loop was using

  • The while version post-incremented a count whose initial value was 2

  • The number of iterations is N-2

  • The for version starts with count=3 and continues until it is N, thus iteration (N-3)+1 times, i.e. N+2 times

The FOR statement (2)
· The for statement, like the while statement, is used to execute code iteratively.

· The for statement in C is designed to be a lot more general (and potentially more powerful) than Algol-like languages.



· The following table gives syntax of various for-like constructs and the equivalent while construct.


for statement syntax

equivalent while syntax


for (expr1; expr2; expr3)

statement

next statement

expr1 ;

while (expr2) {

statement

expr3 ;

}

next statement


for ( ;expr2; expr3 )

statement

next statement

while (expr2) {

statement

expr3 ;

}

next statement

for ( ;expr2; )

statement

next statement

while (expr2)

statement

next statement


for ( ;;expr3 )

statement

next statement

while (1) {

statement

expr3 ;

}

next statement

for (;;)

statement

next statement

while (1)

statement

next statement

The FOR statement (3)
More generally, the syntax is a follows:


for_statement

::=

for (expr1 ; expr2 ; expr3) statement

expr1

::=

initialization-expression




|




expr2

::=

test-relation-expression




|




expr3

::=

next-expression




|



· The first optional expression within the for loop is the initialization

· It is done before the loop starts

· The second optional expression is the test condition

· It is performed at the beginning of each iteration

· If it is nonzero (true), the body of the loop is performed

· If it is zero (false), the for loop terminates

· The third optional expression is an expression which will be performed as the last statement in the loop

· In this context, the semicolons are expression separators

The FOR statement (4)
example -- infinite loop


for ( ;; ) /* while (1) */

{

statements

}



example


for construct

equivalent while construct

int i, n, sum;
...
sum = 0;

for (i=1; i<=n; i++)

sum = sum + i;


int i, n, sum;

...


sum = 0;

i = 1;


while (i <= n)

{

sum = sum + i;



i++;

}

expr1: i = 1

expr2: i <= n

expr3: i++
The FOR statement (5)
· The for statement is similar to the Pascal for loop or the Fortran DO statement

· However, in C, both the index and limit can be altered within the loop


example


int getlimit (int limit);

int getindex (int index);


void ugly (void)

{

int i = 0, limit = 100;


for (i = 0; i < limit; i++) {

i = getindex (i);

limit = getlimit (limit);

printf ("Hmmm: i=%d and limit=%d\n",

i, limit);

}

}


· This type of thing is not a good idea…

· The loop index and limit are changed by an outsider

· The behavior can be very unpredictable



Copyright (c) 1999 by Robert C. Carden IV, Ph.D.

8/6/2017


Download 283.95 Kb.

Share with your friends:
1   2   3




The database is protected by copyright ©ininet.org 2020
send message

    Main page