You are on page 1of 14

UNIVERSIDAD POLITCNICA SALESIANA

NOMBRE: JUAN CARLOS TACO

FECHA: 16-06-16

MATERIA: ROBOTICA

NIVEL: OCTAVO ELECTRICA

TEMA: CONCEPTOS (IF, DO, FOR, WHILE)


=========================================================================

1. Uso del if.


coentario: el uso del if es como uno decir si es que algo es verdadero o falso y lo nico que
se le adiere a ello es una condicin que se ejecuta luego de saber si es verdadero o falso.

===============================================================
Function:
Depending on the result of and expression, a process is selected and executed.
Syntax:
IF <Expression> THEN <Process> [ELSE <Process>]
Parameters:
<Expression>

Describe a
expression.

numeric

operation

<Process>

Describe the command statements


used in MELFA-BASIC III (except
condition branch statements and repeat
control statements), line Nos. and label
names.

Explanations:
If the result of <Expression> is true, then the <Process> in the THEN statement is
executed, and if false, then the <Process> in the ELSE statement is executed.
Omit ELSE if not needed.
The following cannot be used for <Process>:

Type of the statement

MELFA-BASIC III-command

Condition branch statement:

ON ... GOTO

ON ... GOSUB
IF ... THEN ... ELSE
Repeat control statement:

FOR ... NEXT


WHILE ... WEND

Statements that cannot stand alone:

Commands with an asteriks (*), e. g.:


WTH, WTHIF, IN, PLT

Definition and declaration statements:

DIM, DEF, FN, ON COM GOSUB

Remark statements:

REM

Example:
100 IF MDATA > 10 THEN 1000
110 IF MDATA > 10 GOTO 1000 ELSE GOTO 2000
120 IF MDATA > 10 THEN GOSUB 1000 ELSE GOTO 2000
130 IF MDATA > 10 GOTO 1000 ELSE GOTO *WORK
Reference
ON GOSUB, ON GOTO
================================================================

1.1.

Uso del if<> then

================================================================================

Function:
A process is selected and executed according to the results of an expression. This format is
available for controller software version G1 or later:
Syntax:
IF <Expression> THEN <Process> [ELSE <Process>]
The BREAK command is available for controller software version J1 or later:
IF <Expression> THEN
<Process>

<Process>
BREAK
:
[ELSE]
<Process>
<Process>
BREAK
:
ENDIF
Parameters:
<Expression>

Describe the expression targeted for


comparison as a comparison operation
expression
or
logic
operation
expression.

<Process>

Describe the process following THEN


for when the comparison results are true,
and the process following ELSE for
when the comparison results are false.

Explanations:

The IF .. THEN .. ELSE .. statements should be contained in one line.


It is allowed to split an IF .. THEN .. ELSE .. ENDIF block over several lines.
ELSE can be omitted.
Make sure to include the ENDIF statement in the IF .. THEN .. ELSE .. ENDIF block.
If the GOTO instruction is used to jump out from inside an IF .. THEN .. ELSE .. ENDIF
block, an error will occur when the memory for control structure (stack memory) becomes
insufficient.
For IF .. THEN .. ELSE .. ENDIF, it is possible to describe IF .. THEN .. ELSE .. ENDIF
inside THEN or ELSE. (UP to eight levels of nesting is allowed.)
GOTO
following
THEN
or
ELSE
may
be
omitted.
Example:
IF M1 > 10 THEN 200 ELSE 300
Also, only when THEN is followed by GOTO, either one of THEN or GOTO may be
omitted.
ELSE
cannot
be
omitted.
Example:
IF M1 > 10 THEN GOTO 200 (The program at left can be rewritten as shown below:)
IF
M1
>
10
THEN
200
IF M1 > 10 GOTO 200
In the THEN or the ELSE, it can escape to the next line of ENDIF by BREAK. That is,
process of IF THEN ENDIF can be skipped.(Version J1 or later)

Example:
Software Versions previous to G1:
100 IF M1 > 10 THEN 1000
110 IF M1 > 10 THEN GOTO 200 ELSE GOTO 300
200 M1 = 10
210 MOV P1
220 GOTO 400
Software version G1 or later
100 IF M1 > 10 THEN
110 M1 = 10
120 MOV P1
130 ELSE
140 M1 = -10
150 MOV P2
160 ENDIF
For earlier than G1 edition it is also possible to use the following:
250 IF M2 = 0 THEN GOSUB *SUB1 ELSE GOSUB *SUB2
When a IF statement is described inside THEN or ELSE (G1 and later)
300 IF M1 > 10 THEN
310 IF M2 > 20 THEN
320 M1 = 10
330 M2 = 10
340 ELSE
350 M1 = 0
360 M2 = 0
370 ENDIF
380 ELSE
390 M1 = -10

400 M2 = -10
410 ENDIF
In the THEN or the ELSE, it can escape to the next line of ENDIF by BREAK.(Version
J1 or later)
300 IF M1 > 10 THEN
310 IF M2 > 20 THEN BREAK

' If the conditions are met,


continue on line 390

320 M1 = 10
330 M2 = 10
340 ELSE
350 M1 = -10
360 IF M2 > 20 THEN BREAK

'If the conditions are met,


continue on line 390

370 M2 = -10
380 ENDIF
390 IF M_BRKCQ = 1 THEN HLT
400 MOV P1

SynactStatement =
"SYNACT" "WHEN" Expression "DO"
( Assignment | PulseStatement ) [ "DELAY" Expression ].

The
SynactStatement
(SYNchronized ACTion)
serves for the fast and
exact reaction on an event

during the actual robot


movement. It must be
inserted just before the
MOVE
statement
to
activate the cyclic and
parallel monitoring of the
logic condition expressed
by the first Expression.
When the condition becomes true (<>0) the given Assignment or PulseStatement is
executed exactly once in parallel to the execution of the MOVE statement. After this or
at the end of the movement the monitoring of the condition stops.
The condition must be a simple logical expression without function calls or nested
expressions, respectively. It may contain predefined system variables refering to times or
distances. There are predefined variables refering to the start or to the end point of a
movement.
In addition the optional definition of a time delay of the event reaction is possible. The
second Expression is the positive or negative time delay given in seconds.
Multiple SynactStatements for one MOVE statement are allowed but any
SynactStatement controls only one MOVE statement.

Example:
VAR
OUTPUT BOOL: out_5 AT 6;
ROBTARGET: point_1;
...
BEGIN
...
SYNACT WHEN R_DISTANCE_F > 20.0 DO out_5 := TRUE;
MOVE LIN point_1;
...
ENDPROGRAM

Synchronized actions are currently not supported in CIROS and PCROB.


================================================================
2. Uso del bucle Do.

Comentario: este en mi opinin no hace nada solo pero si se lo conbian con el while es una
muy buena opcin de programacin.
=================================================================
ForStatement =
"FOR" Object ":=" Expression "TO" Expression
[ "STEP" Expression ]
StatementBlock "ENDFOR".
The first Expression is the limit to start the loop, the second Expression is the limit to end the
loop. The third Expression is the increment. The default increment is 1. Negative increments
are allowed.
All expressions must be arithmetical expressions. They and the Object must be of data type
INT. Before the StatementBlock of the ForStatement starts execution all three expressions
are evaluated.
In the case of positive increment the StatementBlock is executed if the value of Object which
represents the counter is less or equal to the second Expression. With negative increment the
counter has to be greater or equal for execution. In the case of zero increment the
StatementBlock is executed as long as it is not left by a GotoStatement.
After leaving the loop with a GotoStatement the counter keeps its current value. After normal
completion of the loop the counter is equal to the first value that does not match the condition
for the execution of the statement block.
Example:
FOR i := 1 TO 10
MOVE LIN palette [i];
ENDFOR;

===============================================================
3. Uso del bucle for.
Comentario: el uso del for podramos decir que es muy limitado ya que uno da el incio y un
fin y asi este programa cumple un determinado numero de acciones a la que ha sido
programada con una cantidad limite de pasos.

===============================================================

Function:
This command will continually repeat part of the program between the FOR statement and
NEXT statement until the end conditions are satisfied.
Syntax:
FOR <Counter> = <Default value> TO <End value> [STEP <Increment>]
...
NEXT [<Counter 1> [, <Counter2>] ... ]
Parameters:
<Counter>

Describes the numeric value data that


represents the counter for the number of
repetitions.
<Counter 1> <Counter 2> can also be
used.

<Default Value>

Sets default value of the counter for the


number of repetitions, in terms of a
numeric operation expression.

<End Value>

Sets the end value of the counter for the


number of repeats, in terms of a numeric
operation expression.

<Increment>

Sets the value of the increments for the


counter for the number of repetitions, in
terms of a numeric operation
expression.

Explanations:

o
o

The order of execution will be as shown below:


If the repeating process is not executed even once, one of the following situations has
occurred:
The counter's <Default Value> is greater than <End Value> and <Increment> is a positive
number.
The counter's <Default Value> is smaller than <End Value>, and <Increment> is a negative
number.
If the FOR statement and NEXT statement contradict each other, an error will occur.
Note that when a FOR-NEXT exists in a nesting structure and it has the same end value, it
is possible to use only 1 NEXT statement. For example, line 50 and 60 in the example can
be combined to be written "NEXT MY, MX" in one line.

o
o
o

When the NEXT statement corresponds to the closest FOR statement, the variable name in
the NEXT statement can be omitted. In the example, "MY" in line 50 and "MX" in line 60
can be omitted.
Program
depth:
In this system, it is possible for the control structure to have up to 16 levels within a
program. Exceeding 16 levels will cause an error during execution.
The FOR-NEXT process allows the control structure to go deep within the program.
It is possible to use the FOR-NEXT statements between other FOR-NEXT statements.
With each FOR-NEXT process, the control structure of the program becomes one level
deeper.

Example:
A program that adds the numbers 1 to 10:
10 MSUM=0

'Set the total, MSUM, to its default


value.

20 FOR MDATA=1 TO 10 STEP 1

'Set the numeric variable MDATA's


default value, end value, and increment.
If MDATA's value exceeds 10, go to
line 50.

30 MSUN=MSUN+MDATA

'Add MDATA to the numeric variable


MSUM.

40 NEXT MDATA

'Go to line 20.

50 END

'End the program.

A program that puts the result of a product of two numbers into a 2-dimensional array
variable:
10 DIM MBOX(10,10)

'Reserve space for a 10x10 array.

20 FOR MX=1 TO 10 STEP 1

'Increase the counter by 1 from 1 to 10


for the numeric variable MX. If 10 is
exceeded, go to line 70. Step 1 can be
omitted.

30 FOR MY=1 TO 10 STEP1

'Increase the counter by 1 from 1 to 10


for the numeric variable MX. If 10 is
exceeded, go to line 60. Step 1 can be
omitted.

40 MBOX(MX,MY)=MX*MY

'Substitute the value of MX*MY for the


array variable MBOX (MX, MY).

50 NEXT MY

'Return to line 30.

60 NEXT MX

'Return to line 20.

70 END

'End the program.

Function:
Repeatedly executes the program between the FOR statement and NEXT statement until the
end conditions are satisfied.
Syntax:
FOR <Counter> = <Default value> TO <End value> [STEP <Increment>]
...
NEXT [<Counter 1> [, <Counter2>] ... ]
Parameters:
<Counter>

Describes the numeric value data that


represents the counter for the number of
repetitions.
Same for <Counter 1> and <Counter 2>.

<Default Value>

Sets default value of the counter for the


number of repetitions as a numeric
operation expression.

<End Value>

Set the end value of the counter for the


number of repeats as a numeric
operation expression.

<Increment>

Sets the value of the increments for the


counter for the number of repetitions as
a numeric operation expression.

Explanations:

Cases in which the repeated control is not executed are shown below.
The counter's <Default Value> is greater than <End Value> and <Increment> is a positive
number.
The counter's <Default Value> is smaller than <End Value>, and <Increment> is a negative
number.
If the FOR statement and NEXT statement contradict each other, an error will occur.
Note that when the FOR and NEXT statements are in a nesting structure and have the same
end value, the statement can be described with one NEXT statement. For example, line 50
and 60 in the example can be combined to be written "NEXT M2, M1" in one line.

When the NEXT statement corresponds to the closest FOR statement, the variable name in
the NEXT statement can be omitted. In the example, "M2" in line 50 and "M1" in line 60
can be omitted.
Program depth:
It is possible to describe FOR-NEXT statements between other FOR-NEXT statements.
With each FOR-NEXT process, the control structure of the program becomes one level
deeper.
In this system, it is possible for the control structure to have up to 16 levels within a
program. Exceeding 16 levels will cause an error during execution.
Example:
A program that adds the numbers 1 to 10:
10 MSUM=0

'Initialize the total MSUM.

20 FOR M1=1 TO 10 STEP 1

'Increase the counter by 1 from 1 to 10


for the numeric variable M1. If 10 is
exceeded, go to line 50. Step 1 can be
omitted.

30 MSUM=MSUM+M1

'Add M1 value to numeric variable


MSUM.

40 NEXT M1

'Return to line 20.

50 END

'End the program.

A program that puts the result of a product of two numbers into a 2-dimensional array
variable
(Using FOR-NEXT as a nesting structure)
10 DIM MBOX(10,10)

'Reserve space for a 10 x 10 array.

20 FOR M1=1 TO 10 STEP 1

'Increase the counter by 1 from 1 to 10


for the numeric variable M1. If 10 is
exceeded, transfer control to line 70.
Step 1 can be omitted.

30 FOR M2=1 TO 10 STEP 1

'Increase the counter by 1 from 1 to 10


for the numeric variable M2. If 10 is
exceeded, transfer control to line 60.
Step 1 can be omitted.

40 MBOX(M1,M2)=M1*M2

'Substitute the value of M1*M2 for the


array variable MBOX (M1, M2).

50 NEXT M2

'Return to line 30.

60 NEXT M1

'Return to line 20.

70 END

'End the program.

Reference
GOSUB, WHILE, END
================================================================================

4. Uso del bucle while.


Comentario: el while es una opcin muy buena para hacer repeticiones de un sistema para
que se cumpla o se siga ejecutando hasta que se cumpla.
================================================================================

WhileStatement =
"WHILE" Expression StatementBlock "ENDWHILE".
The statement block will be repetitively executed as long as the evaluation of the logical
Expression returns true (<>0). If the first interpretation of Expression returns false (=0) the
statement block will not be executed at all.
Every WHILE needs an ENDWHILE.
Example:
i := 1;
WHILE i<=10
MOVE LIN palette[i];
i := i+1;
ENDWHILE;
=================================================================

4.1.

Uso del bucle while and wend

=======================================================
Function:
The program between the WHILE statement and WEND statement is repeated until the
loop conditions are satisfied.

Syntax:
WHILE<Loop condition>
...
WEND
Parameters:
<Loop Condition>

Describe a numeric operation


expression. (Refer to the syntax
diagram)

Explanations:

The program between the WHILE statement and WEND statement is repeated.
If the result of <Expression> is true (not 0), then the control moves to the line following
the WHILE statement and the process is repeated.
If the result of <Expression> is false (is 0), then the control moves to the line following
the WEND statement.
If a GOTO instruction forces the program to jump out from between a WHILE statement
and a WEND state-ment, the free memory available for control structure (stack memory)
decreases. Thus, if a program is executed continuously, an error will eventually occur.
Write a program in such a way that the loop exits when the condition of the WHILE
statement is met.
Example:
20 WHILE (M1>=-5) AND (M1<=5)

30 M1=-(M1+1)
40 PRINT# 1, M1
50 WEND
60 END

'Repeat the process while the numeric


variable M1 value is between -5 and
+5, and transfer control to line after
WEND statement if range is
exceeded.
'Add 1 to M1, and reverse the sign
'Output the M1 value
'Return to the WHILE statement (line
20)
'End the program

You might also like