PROGRAMMING PIC MICROCONTROLLERS IN C:PROGRAM FLOW CONTROL

PROGRAM FLOW CONTROL

The PICC Lite language supports the following flow control commands:

Programming PIC Microcontrollers in C-0123

The if statement can be used together with the else statement when it is required to execute alternative set of statements when a condition is not satisfied. The general format is:

Programming PIC Microcontrollers in C-0124

Switch–Case Statement

This is another form of flow control where statements are executed depending on a multi-way decision. The switch–case statement can only be used in certain cases where:

• only one variable is tested and all branches depend on the value of that variable;

• each possible value of the variable can control a single branch.

The general format of the switch–case statement is as follows. Here, variable number is tested. If number is equal to n1, statements between n1 and n2 are executed. If number is equal to n2, statements between n2 and n3 are executed, and so on. If number is not equal to any of the condi- tion then the statements after the default case are executed. Notice that each block of statement is terminated with a break statement so that the program jumps out of the switch–case block.

Programming PIC Microcontrollers in C-0125

For Statement

The for statement is used to create loops in programs. The for loop works well where the number of iterations of the loop is known before the loop is entered. The general format of the for statement is:

Programming PIC Microcontrollers in C-0127

While Statement

The while loop repeats a statement until the condition at the beginning of the statement becomes false. The general format of this statement is:

Programming PIC Microcontrollers in C-0128

Programming PIC Microcontrollers in C-0129

Continue Statement

The continue statement is similar to the break statement but is used less frequently. The continue statement causes a jump to the loop control statement. In a while loop, control jumps to the condition statement, and in a for loop, control jumps to the beginning of the loop.

Leave a comment

Your email address will not be published. Required fields are marked *