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?

Postby gozron » May 07, 2011 21:33

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

Postby Kot » May 07, 2011 21:54

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?

Postby elsairon » May 08, 2011 0:05

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: 2958
Joined: Jan 15, 2007 20:44
Location: Australia

Postby Richard » May 08, 2011 4:38

Why make a decision when you can compute the inverse.

Code: Select all

Dim As String t, t1
t = "1111000" ' input test
t1 = t
For i as integer = 0 To 6 Step 2
    t1[i] = 1 Xor t1[i]
Next i
print t
print t1
Sleep
Here are three different ways of computing the inverse.

Code: Select all

Dim As String t = "0000000011111111"    ' input test string
Dim As String t1 = t, t2 = t, t3 = t
For 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 differ
Next i
Print t
Print t1
Print t2
Print t3
Sleep
Theunis Jansen
Posts: 248
Joined: Jul 01, 2010 9:35

Postby Theunis Jansen » May 08, 2011 10:25

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 t
t="10101010"           REM or whatever it is you want to change
t1=t                       REM make t1 the same as t
for 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 if
next i
    Rem the i isn't really necessary. it slows down the loop in QB4.5
bfuller
Posts: 328
Joined: Jun 02, 2007 12:35
Location: Sydney, Australia

Postby bfuller » May 09, 2011 0:41

@gozron

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

http://www.freebasic.net/wiki/wikka.php?wakka=DocToc for table of contents and

http://www.freebasic.net/wiki/wikka.php ... eyPgIfthen for IF....THEN

sorry if I'm being simplistic but you did post in the "beginners" forum and the WIKI is a huge help to me when I'm just trying to get something to run.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 3 guests