Getting Started

(lily) #1

Chapter 5: C Control Flow


statement). Th
K&R) ember to use it when you add a
stateme


Loops – While, For and Do-while.................................................................


// Do stuff while expression is true

The co






oop_ (3000 );

This do p in our first example program:


{
PORTD = i;

}


e break after default: isn’t even necessary, but is recommended (by
as a good practice to help you rem
nt to the end of the list.

We’ve been using while for a while (har!).


while(expression)
{

}

While will repeat the associated statement or block as long as the expression is
true.


de fragment:

xint i;
while( i <= 1
{
PORTD = i;
_delay_l 2 0
i = i*2;
}

es exactly the same thing as the for loo

for(int i = 1; i <= 128; i = i*2)

_delay_loop_2(30000);
}

The for loop is constructed as follows:


for(expresson1; expression2; expresson3)
{
// Do stuff

Free download pdf