Mikroc – ABL electronic PIC Microcontrollers PIC16 User Manual

Page 125

Advertising
background image

Nested if statements

Nested

if

statements require additional attention. General rule is that the nested

conditionals are parsed starting from the innermost conditional, with each

else

bound to the nearest available

if

on its left:

if

(

expression1

) statement1

else if

(

expression2

)

if

(

expression3

)

statement2

else

statement3

/* this belongs to: if (expression3) */

else

statement4

/* this belongs to: if (expression2) */

Note: The

#if

and

#else

preprocessor statements (directives) look similar to the

if

and

else

statements, but have very different effects. They control which

source file lines are compiled and which are ignored. See Preprocessor for more
information.

Switch Statement

Use the switch statement to pass control to a specific program branch, based on a
certain condition. Syntax of switch statement is:

switch

(

expression

) {

case

constant-expression_1

:

statement_1

;

.

.

.

case

constant-expression_n

:

statement_n

;

[default :

statement

;]

}

First, the

expression

(condition) is evaluated. The

switch

statement then

compares it to all the available

constant-expressions

following the keyword

case

. If the match is found,

switch

passes control to that matching

case

, at

which point the

statement

following the match evaluates. Note that

constant-expressions

must evaluate to integer. There cannot be two same

constant-expressions

evaluating to same value.

Parantheses around

expression

are mandatory.

.

MikroElektronika: Development tools - Books - Compilers

117

page

mikroC - C Compiler for Microchip PIC microcontrollers

mikroC

making it simple...

Advertising
This manual is related to the following products: