## How to structure If/Else?

New to FreeBASIC? Post your questions here.
gozron
Posts: 2
Joined: May 07, 2011 15:37

### How to structure If/Else?

Search on this forum is confusing. I searched for "else" or "if" (with and without quotes) with all topics selected and returned no results.

Anyway, here's my dilemma. (spell checked and doesn't look right) :P

I'm trying to go through a string of bits flipping every other one. Like so:

Code: Select all

`t1=""for i=1 to 7 step 2   if mid(t,i,1)="0" then t1=t1+"1"   if mid(t,i,1)="1" then t1=t1+"0"   t1=t1+mid(t,i+1,1)next i`

Of course you see the problem. If the first condition is true then both lines get executed accomplishing nothing. I went through help looking for if/else formats trying every combination I can think of and keep getting "Else without If" errors. Can someone point me in the right direction? It's been several years and starting to enjoy this stuff again.

Regards
Kot
Posts: 336
Joined: Dec 28, 2006 10:34
Since you can have only two values - 1 or 0, the second line is useless. Can't you do it like this:

if mid(t,i,1)="0" then t1=t1+"1" else t1=t1+"0"
elsairon
Posts: 207
Joined: Jul 02, 2005 14:51

### Re: How to structure If/Else?

gozron wrote:Of course you see the problem. If the first condition is true then both lines get executed accomplishing nothing. I went through help looking for if/else formats trying every combination I can think of and keep getting "Else without If" errors. Can someone point me in the right direction? It's been several years and starting to enjoy this stuff again.

Regards

You could add an elseif to the code, or use a select case, although that might be a bit much for only 2 choices.

Code: Select all

`t1=""for i=1 to 7 step 2   if mid(t,i,1)="0" then        t1=t1+"1"   elseif mid(t,i,1)="1" then        t1=t1+"0"   end if   t1=t1+mid(t,i+1,1)next i`
Richard
Posts: 3026
Joined: Jan 15, 2007 20:44
Location: Australia
Why make a decision when you can compute the inverse.

Code: Select all

`Dim As String t, t1t = "1111000" ' input testt1 = tFor i as integer = 0 To 6 Step 2    t1[i] = 1 Xor t1[i]Next iprint tprint t1Sleep`
Here are three different ways of computing the inverse.

Code: Select all

`Dim As String t = "0000000011111111"    ' input test stringDim As String t1 = t, t2 = t, t3 = tFor i As Integer = 0 To Len(t1)-1 Step 2    t1[i] = Asc("0")  +  Asc("1")  -  t1[i] ' subtract, the other remains    t2[i] = Asc("0")  *  Asc("1")  /  t2[i] ' one cancels, the other remains    t3[i] = Asc("0") Xor Asc("1") Xor t3[i] ' flip only the bits that differNext iPrint tPrint t1Print t2Print t3Sleep`
Theunis Jansen
Posts: 248
Joined: Jul 01, 2010 9:35
On a pure QB4.5 basis -

I would suggest the following because it is more structured then one line coding and is easier to read. I tested it with -lang fblite and there are no problems.
If you use Fbide then there are some examples on the structure of IF then Else ElseIf. I prefer to use Select Case when more than one decision is involved. You can code precisely what you want in a more readable manner.
If we process t as indicated t1 will become t1="00000000"

Code: Select all

`DEFSTR tt="10101010"           REM or whatever it is you want to changet1=t                       REM make t1 the same as tfor i=1 to 7 step 2   REM process t but change t1   if mid(t,i,1)="0" then        mid(t1,i,1)="1"     else       mid(t1,i,1) = "0"         Rem since we have checked for 0 we don't have to check for 1   end ifnext i    Rem the i isn't really necessary. it slows down the loop in QB4.5`
bfuller
Posts: 338
Joined: Jun 02, 2007 12:35
Location: Sydney, Australia
@gozron

I see you are new on the forum, with only a couple of posts. Have you found the WIKI yet?