Computer Science Canada

Is there an easy way to make these if statements be one?

Author:  joe333 [ Tue Apr 13, 2004 7:16 pm ]
Post subject:  Is there an easy way to make these if statements be one?

Is there an easy way to make these smaller? Like in a loop or something? i'm making a hex to dec convertor.

if input1 = "A" then
input2 := intstr (A)
end if
if input1 = "B" then
input2 := intstr (B)
end if
if input1 = "C" then
input2 := intstr (C)
end if
if input1 = "D" then
input2 := intstr (D)
end if
if input1 = "E" then
input2 := intstr (E)
end if
if input1 = "F" then
input2 := intstr (F)
end if

Author:  omni [ Tue Apr 13, 2004 7:42 pm ]
Post subject: 

Right off the top of my head, you can make it like

if input1="A" then
'command'
elsif input1="B" then
...
...
so on
end if
That means ONLY ONE of those conditions can be true.
Not sure if intstr(A) will work, that converts an integer to a string. Unless
if 'A' is a variable of integer type, don't think that will work

Author:  EDEN-E [ Tue Apr 13, 2004 7:57 pm ]
Post subject: 

If you dont want to use 'IF' statement, you can use "CASE"

case input1 of
label "A" : input2 := intstr(A)
label "B" : input2 := intstr(B)
label "C" : input2 := intstr(C)
label "D" : input2 := intstr(D)
label "E" : input2 := intstr(E)
label "F" : input2 := intstr(F)
end case

Author:  Raugrist [ Tue Apr 13, 2004 8:07 pm ]
Post subject: 

I don't know why you're doing intstr, but whatever...

code:

if ord (input1) >= 65 and ord (input2) <= 70 then
    input2 := intstr (ord (input1) - 65 + 10)
end if


From the looks of your post, that should do it quite nicely. Basically, it checks if input1 is a letter between A and F, and if it is, it gets the number value for that letter (you are converting from Hex, so A = 11, B = 12, ..., F = 15), converting the value to a string and putting it in input2.


: