You are on page 1of 20

Programming mikroBasic using Control Structure

Part 2 Iterative and Jump Statements


CpELECIA: Embedded System Final Lecture 1 Engr. Ricrey E. Marquez, CpE, MSCS

Introduction to Iterative Statement

Iterative or Looping Statements

These control structure allows a repeating one or more instructions for a number of times

Conducting expression determines the number of iterations loop will go through.

Three are three types of looping statements in mikroBasic:


for loop while loop do-while loop

Introduction to Iterative Statement

It be can use the statements break and continue to control the flow of a loop statement.

break terminates the statement in which it occurs continue begins executing the next iteration of the sequence.

Iterative Statement FOR loop Statement


Also known as repeating of a program segment An iterative statement that requires to specify the number of iterations of the loop to go through.

Every statement between for and next will be executed once per iteration.

General syntax for FOR loop:


FOR counter = initialValue TO finalValue [STEP step_value] statement_1 statement_2 ... statement_N NEXT counter

Iterative Statement FOR loop Statement


counter is a variable initialValue and finalValue are expressions compatible with counter statement/s is any statement that does not change the value of counter step_value is value that is added to the counter in each iteration.

step_value is optional, and defaults to 1 if not stated otherwise. Be careful when using large values for step_value, as overflow may occur The parameter step_value may be negative, allowing to create a countdown In FOR statement, the results in an endless loop if the finalValue equals or exceeds the range of counters type.

Iterative Statement FOR loop Statement

Code Sample 1: This sample code turn on and off the LEDs connected to port B of PIC16F84A ten times.
PROGRAM forLoop1 DIM i AS BYTE MAIN: TRISB=0 PORTB=0 FOR i = 1 TO 10 PORTB=0 DELAY_MS(500) PORTB=255 DELAY_MS(500) NEXT i END.

Iterative Statement FOR loop Statement

Code Sample 2: This sample code turn on and off the LEDs connected to port B of PIC16F84A ten times.
PROGRAM forLoop2 DIM i AS BYTE MAIN: TRISB=0 PORTB=0 FOR i = 10 TO 1 STEP -1 PORTB=0 DELAY_MS(500) PORTB=255 DELAY_MS(500) NEXT i END.

Iterative Statement WHILE loop Statement


An iterative statement that will loop while condition is fulfilled It is similar to do..loop, except the check is performed at the beginning of the loop.

If expression returns FALSE upon first test, statements will not be executed.

expression is tested first.


If it returns TRUE, all the following statements enclosed by while and wend will be executed (or only one statement, alternatively). It will keep on executing statements until the expression returns FALSE. As expression returns FALSE, while will be terminated without executing statements.

Iterative Statement WHILE loop Statement

General Syntax of WHILE loop:


WHILE expression statement_0 statement_1 ... statement_N WEND

Iterative Statement WHILE loop Statement

Code Sample 3: This sample code turn on and off the LEDs connected to port B of PIC16F84A ten times.
PROGRAM whileLoop2 DIM i AS BYTE MAIN: TRISB=0 PORTB=0 i=1 WHILE (i<=10) PORTB=0 DELAY_MS(500) PORTB=255 DELAY_MS(500) i=i+1 WEND END.

Iterative Statement WHILE loop Statement

Code Sample 4: This sample code turn on and off the LEDs connected to port B of PIC16F84A ten times.
PROGRAM whileLoop2 DIM i AS BYTE MAIN: TRISB=0 PORTB=0 i=10 WHILE (i>=10) PORTB=0 DELAY_MS(500) PORTB=255 DELAY_MS(500) i=i-1 WEND END.

Iterative Statement DO..LOOP UNTIL Statement


An iterative that will loop until condition is fulfilled It is an statement executes until the condition becomes TRUE. General syntax for DO..LOOP UNTIL loop:
DO statement_1 ... statement_N LOOP UNTIL expression

Iterative Statement DO.. LOOP UNTIL Statement

expression returns a TRUE or FALSE value.

Executes statement_1; ...; statement_N continually, checking the expression after each iteration. When expression returns TRUE, the do..loop statement terminates.

Sequence is executed at least once

Iterative Statement DO..LOOP UNTIL Statement

Code Sample 5: This sample code turn on and off the LEDs connected to port B of PIC16F84A ten times.
PROGRAM dowhileLoop1 DIM i AS BYTE MAIN: TRISB=0 PORTB=0 i=0 DO i=i+1 PORTB=0 DELAY_MS(500) PORTB=255 DELAY_MS(500) LOOP UNTIL (i>=10) END.

Iterative Statement DO..LOOP UNTIL Statement

Code Sample 6: This sample code turn on and off the LEDs connected to port B of PIC16F84A ten times.
PROGRAM dowhileLoop2 DIM i AS BYTE MAIN: TRISB=0 PORTB=0 i=10 DO i=i-1 PORTB=0 DELAY_MS(500) PORTB=255 DELAY_MS(500) LOOP UNTIL (i<=0) END.

Introduction to Jump Statement

Jump Statements

These are statement that when executed, it transfers control unconditionally. There are four such statements in mikroBasic:

break statement continue statement goto statement gosub statement

Jump Statement BREAK Statement


It is use within loops to pass control to the first statement following the innermost loop (for, while, and do). If loop need to be stop from within its body. Sample Code 6: This program will detect card then exit from the loop if card is inserted.
PROGRAM breakinLCD MAIN:

' Wait for CF card to be plugged; refresh every second WHILE TRUE Lcd_Out(1,1,"No card inserted") IF Cf_Detect() = 1 THEN BREAK END IF DELAY_MS(1000) WEND
' Now we can work with CF card ... Lcd_Out(1,1,"Card detected ") END.

Jump Statement CONTINUE Statement

It is a statement use within loops to skip the cycle

In FOR loop moves program counter to the line with keyword FOR

It does not change the loop counter

In WHILE loop moves program counter to the line with loop condition (top of the loop) In DO..LOOP UNTIL loop moves program counter to the line with loop condition (bottom of the loop).

Jump Statement GOTO Statement

This transfer control to the location of a local label specified by label_name. Syntax of GOTO statement:
GOTO label_name

GOTO line can come before or after the label.

It is not possible to jump into or out of routine.

It is use to break out from any level of nested control structures.


Never jump into a loop or other structured statement, since this can have unpredictable effects. Use of GOTO statement is generally discouraged as practically every algorithm can be realized without it, resulting in legible structured programs.

One possible application of GOTO statement is breaking out from deeply nested control structures.

Jump Statement GOSUB Statement

This will transfer control to the location of a local label specified by label_name and calling point is remembered.

Upon encountering a return statement, program execution will continue with the next statement (line) after the GOSUB. GOSUB line can come before or after the label.

Syntax of GOSUB statement: GOSUB label_name ... label_name: ...

It is not possible to jump into or out of routine by means of GOSUB.

Never jump into a loop or other structured statement, since this can have unpredictable effects.

You might also like