Getting Started

(lily) #1

Chapter 5: C Control Flow


this be evaluated sequentially looking for the first
non-zero (true) expression and if they all equal 0 (false) we do statement 4. You
can om nt to do nothing if all the expressions
are 0 (f xample of this construction later when we write an
exampl rogr ng the joystick interrupts:


KEY_PLUS)PORTD= ~0x01;
put == KEY_NEXT)PORTD = ~0x02;
else if(input == KEY_PREV)PORTD = ~0x04;

ht and LED, this statement lights
e LED). If the first line is true then the rest of the statements are skipped. If the

ons that are either true or false. If we
ant to make decisions using expressions that can have any numeric result we use

if (expression1)
statement1
else if (expression2)
statement2
else if (expression3)
statement3
else
statement4


In case each expression will


it the final else statement if you wa
alse). We will use an e
e p am for usi

if(input
else if(in

==

else if(input == KEY_MINUS)PORTD = ~0x08;
else if(input == KEY_ENTER)PORTD = ~0x10;

Which may be read as: if the input is equal to KEY_PLUS then set port D equal
to the inverse of a byte equal to 1 (a byte of 1 is binary 00000001, the inverse is
11111110 and since we output a 0 to a pin to lig
th
first line isn’t true, then each line is evaluated sequentially until a true expression
is found or it drops out the bottom and does nothing.


Switch............................................................................................................


The ‘if else’ construction limits us to expressi
w
the switch statement that selects an expression with results equal to a specified
constant.

Free download pdf