Monday, 2 May 2011

Looping and Iteration

Looping and Iteration

This chapter will look at C's mechanisms for controlling looping and iteration. Even though some of these mechanisms may look familiar and indeed will operate in standard fashion most of the time. NOTE: some non-standard features are available.

The for statement

The C for statement has the following form:


for (expression1; 2; expression3)
statement;
or {block of statements}

expression1 initialises; expression2 is the terminate test; expression3 is the modifier (which may be more than just simple increment);

NOTE: C basically treats for statements as while type loops

For example:

int x;
main()
{
for (x=3;x>0;x-)
{
printf("x=%d$\setminus$n",x);
}
}

...outputs:

x=3
x=2
x=1

...to the screen

All the following are legal for statements in C. The practical application of such statements is not important here, we are just trying to illustrate peculiar features of C for that may be useful:-

for (x=0;((x>3) && (x<9)); x++) for (x=0,y=4;((x>3) && (y<9)); x++,y+=2) for (x=0,y=4,z=4000;z; z/=10) The second example shows that multiple expressions can be separated a ,. In the third example the loop will continue to iterate until z becomes 0; The while statement

The while statement is similar to those used in other languages although more can be done with the expression statement -- a standard feature of C.

The while has the form:

while (expression)
statement

For example:

int x=3;
main()
{ while (x>0)
{ printf("x=%d$\setminus$n",x);
x-;
}
}

...outputs:

x=3
x=2
x=1

...to the screen.


Because the while loop can accept expressions, not just conditions, the following are all legal:-

while (x-);
while (x=x+1);
while (x+=5);

Using this type of expression, only when the result of x-, x=x+1, or x+=5, evaluates to 0 will the while condition fail and the loop be exited.

We can go further still and perform complete operations within the while expression:

while (i++ < 10); while ( (ch = getchar()) != `q') putchar(ch); The first example counts i up to 10. The second example uses C standard library functions (See Chapter 18) getchar() - reads a character from the keyboard - and putchar() - writes a given char to screen. The while loop will proceed to read from the keyboard and echo characters to the screen until a 'q' character is read. NOTE: This type of operation is used a lot in C and not just with character reading!! (See Exercises). The do-while statement

C's do-while statement has the form:

do
statement;
while (expression);

It is similar to PASCAL's repeat ... until except do while expression is true.

For example:

int x=3;

main()
{ do
{
printf("x=%d$\setminus$n",x-);
}
while (x>0);
}

..outputs:-


x=3
x=2
x=1

NOTE: The postfix x- operator which uses the current value of x while printing and then decrements x.

break and continue

C provides two commands to control how we loop:

* break -- exit form loop or switch.
* continue -- skip 1 iteration of loop.

Consider the following example where we read in integer values and process them according to the following conditions. If the value we have read is negative, we wish to print an error message and abandon the loop. If the value read is great than 100, we wish to ignore it and continue to the next value in the data. If the value is zero, we wish to terminate the loop.

while (scanf( ``%d'', &value ) == 1 && value != 0) {

if (value < 0) { printf(``Illegal value$\backslash$n''); break; /* Abandon the loop */ } if (value > 100) {
printf(``Invalid value$\backslash$n'');
continue;
/* Skip to start loop again */
}

/* Process the value read */
/* guaranteed between 1 and 100 */
....;

....;
} /* end while value != 0 */

No comments:

Post a Comment

VMware Cloud Learning Video's

Here is a nice summary list of all VMworld US 2018 Breakout session with the respective video playback & download URLs. Enjoy! Bra...