PDA

View Full Version : IF Then Question



penelopepug
- 16th July 2012, 15:48
Hi!

Can someone please tell me if the following code is equivelent?

If B0 <> 10 Then
B0 = B0 + 1
B1 = B1 - 1
Endif


Is the above the same as this below?


If B0 <> 10 Then B0 = B0 + 1 :B1 = B1 - 1

I am trying to determine whether (in the second example) "B1 = B1 + 1" would be executed only if B0 <> 10.

Thanks.

aratti
- 16th July 2012, 16:34
The two if/then instructions are doing the same thing.

Surely you ment B1= B1 - 1

Cheers

Al.

penelopepug
- 16th July 2012, 17:10
Thanks for your help. And yes, I meant B1 = B1 -1.

I saw conflicting informaition on posts that suggested in a single line statement, consecutive stements on the same line, when separatred by ":" are treated as a statement on a new line and is not part of the If Then.

So just to confirm, as long as I am not using "else", I have a choice to use mulitple lines blocked with "endif" as the last line or I can have a single line with mulitple statments separated by ":" and no endif? Each will be treated the same?

aratti
- 16th July 2012, 21:56
I gave you an uncorrected answer! If you need to use multiple lines command then use the if then/Endif statement. Using the ":" is a new line statement.

To stay on the same line your code should be:

If B0<>10 then B0 = B0 + 1 and B1 = B1 - 1

Al.

penelopepug
- 16th July 2012, 22:51
Thanks Al!

mackrackit
- 16th July 2012, 23:44
From the good ole manual


IF..THEN can operate in 2 manners. In one form, the THEN in an IF..THEN is essentially a GOTO. If the condition is true, the program will GOTO the label after the THEN. If the condition evaluates to false, the program will continue at the next line after the IF..THEN. Another statement may not be placed after the THEN, it must be a label.

If Pin0 = 0 Then pushd ' If button connected to Pin0 is pushed (0), jump to label pushd
If B0 >= 40 Then old ' If the value in variable B0 is greater than or equal to 40, jump to old
If PORTB.0 Then itson ' If PORTB, pin 0 is high (1), jump to itson
If (B0 = 10) AND (B1 = 20) Then loop
In the second form, IF..THEN can conditionally execute a group of Statements following the THEN. The Statements must be followed by an ELSE or ENDIF to complete the structure.

If B0 <> 10 Then
B0 = B0 + 1
B1 = B1 - 1
Endif

If B0 = 20 Then
led = 1
Else
led = 0
Endif

penelopepug
- 17th July 2012, 00:37
Thanks Dave.

The PBP3 manual itself left me confused as per the examples below;

More Examples:
IF B0 <> 10 THEN
B0 = B0 + 1
B1 = B1 - 1
ENDIF
IF B0 = 20 THEN
led = 1
ELSE
led = 0
ENDIF
IF B0 >= 40 THEN old
IF PORTB.0 THEN itson
IF (B0 = 10) AND (B1 = 20) THEN mainloop
IF B0 <> 10 THEN B0 = B0 + 1: B1 = B1 - 1

mackrackit
- 17th July 2012, 03:04
It is only a little bit out dated, but for the most part, IMHO, this is still the best manual.
http://melabs.com/resources/pbpmanual/
It is always open on a tab in my browser... for those special moments...

penelopepug
- 17th July 2012, 05:04
I agree Dave!

Thanks.