Professional Documents
Culture Documents
These control structure allows a repeating one or more instructions for a number of times
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
Executes statement_1; ...; statement_N continually, checking the expression after each iteration. When expression returns TRUE, the do..loop statement terminates.
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.
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.
Jump Statements
These are statement that when executed, it transfers control unconditionally. There are four such statements in mikroBasic:
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.
In FOR loop moves program counter to the line with keyword FOR
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).
This transfer control to the location of a local label specified by label_name. Syntax of GOTO statement:
GOTO label_name
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.
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.
Never jump into a loop or other structured statement, since this can have unpredictable effects.