mikroC

making it simple...

Nested if statements

mikroC - C Compiler for Microchip PIC microcontrollers

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-expressionsfollowing 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-expressionsmust evaluate to integer. There cannot be two same constant-expressionsevaluating to same value.

Parantheses around expression are mandatory.

.

 

 

page

 

MikroElektronika: Development tools - Books - Compilers

117