Click to See Complete Forum and Search --> : If / Else if logic


Motabobo
06-18-2003, 08:07 AM
Hi !

I'm a little bit confused as which use...if/then/else or if/else if.

Could someone tell me if there is a more efficient way to organize this code ? :


if MyArray2(0) = "MOZILLA" then
if Instr(ua, "MSIE") then
MyArray5 = Split(varleft4, " ")
varleft5 = MyArray5(1)
msie = left(varleft5,1)
os = MyArray4(2)
if msie < 4 then
Response.Redirect("error.asp")
end if
elseif Instr(ua, "NETSCAPE") then
os = MyArray4(2)
MyArray5 = Split(varight3,"/")
varight5 = MyArray5(2)
ns = Left(varight5,1)
elseif Instr(ua, "OPERA") then
if Instr(ua, "MSIE") then
MyArray5 = Split(varleft4, " ")
varleft5 = MyArray5(1)
msie = left(varleft5,1)
os = MyArray4(2)
if msie < 5 then
Response.Redirect("error.asp")
end if
else
if mozilla < 5 then
os = MyArray4(0)
Response.Redirect("error.asp")
end if
end if
else
if mozilla < 5 then
Response.Redirect("error.asp")
end if
end if
elseif MyArray2(0) = "OPERA" then
if mozilla < 6 then
os = MyArray4(0)
Response.Redirect("error.asp")
end if
else
Response.Redirect("error.asp")
end if


Thanks !

cmelnick
06-18-2003, 09:07 AM
There's not a whole lot you can do to make this more efficient. There were a couple places where you had some compounded if statements that I fixed in bold.


if MyArray2(0) = "MOZILLA" then
if Instr(ua, "MSIE") then
MyArray5 = Split(varleft4, " ")
varleft5 = MyArray5(1)
msie = left(varleft5,1)
os = MyArray4(2)
if msie < 4 then
Response.Redirect("error.asp")
end if
elseif Instr(ua, "NETSCAPE") then
os = MyArray4(2)
MyArray5 = Split(varight3,"/")
varight5 = MyArray5(2)
ns = Left(varight5,1)
elseif Instr(ua, "OPERA") then
if Instr(ua, "MSIE") then
MyArray5 = Split(varleft4, " ")
varleft5 = MyArray5(1)
msie = left(varleft5,1)
os = MyArray4(2)
if msie < 5 then
Response.Redirect("error.asp")
end if
elseif mozilla < 5 then
os = MyArray4(0)
Response.Redirect("error.asp")
end if
elseif mozilla < 5 then
Response.Redirect("error.asp")
end if
elseif MyArray2(0) = "OPERA" And mozilla < 6 then
os = MyArray4(0)
Response.Redirect("error.asp")
else
Response.Redirect("error.asp")
end if


You could go as far as the following, but this isn't any more efficient, just a little easier to read, perhaps (and you can get carried away)...

if MyArray2(0) = "MOZILLA" And Instr(us, "MSIE") then
MyArray5 = Split(varleft4, " ")
varleft5 = MyArray5(1)
msie = left(varleft5,1)
os = MyArray4(2)
if msie < 4 then
Response.Redirect("error.asp")
end if
elseif MyArray2(0) = "MOZILLA" And Instr(ua, "NETSCAPE") then
os = MyArray4(2)
MyArray5 = Split(varight3,"/")
varight5 = MyArray5(2)
ns = Left(varight5,1)
elseif MyArray2(0) = "MOZILLA" And Instr(ua, "OPERA") then
if Instr(ua, "MSIE") then
MyArray5 = Split(varleft4, " ")
varleft5 = MyArray5(1)
msie = left(varleft5,1)
os = MyArray4(2)
if msie < 5 then
Response.Redirect("error.asp")
end if
elseif mozilla < 5 then
os = MyArray4(0)
Response.Redirect("error.asp")
end if
elseif MyArray2(0) = "MOZILLA" and mozilla < 5 then
Response.Redirect("error.asp")
elseif MyArray2(0) = "OPERA" and mozilla < 6 then
os = MyArray4(0)
Response.Redirect("error.asp")
else
Response.Redirect("error.asp")
end if


I am also not sure why you are setting os = MyArray4(x) before you redirect, seeing as the redirect immediatly stops the script and redirects, and thus those variables won't be usable...Hope this helps!

Motabobo
06-18-2003, 10:28 AM
Thanks for your help :D

The reason i set os is because i'm going to add it in the response redirect link (ie: error.asp?error=1) so it goes on the querystring so i know what error to "response.write" onload of error.asp

Thanks again !

cmelnick
06-18-2003, 02:05 PM
You're welcome. I figured it was something like that, but just wanted to make sure you knew that on a redirect, the script stops.