Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / ELSEIF & THEN

Author
Message
Nieb
10
Years of Service
User Offline
Joined: 13th May 2014
Location: Lurking
Posted: 12th Jun 2014 04:10 Edited at: 13th Jun 2014 03:41
It seems

works.
However,

does not work.
I get
"COMPILER ERROR: Command out of place..."
Lucas Tiridath
AGK Developer
16
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 12th Jun 2014 10:30 Edited at: 12th Jun 2014 10:30
Yes that sounds right to me.


is a special syntax for one line if statements. elseif is part of the standard

structure.

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 12th Jun 2014 12:05
this is normal syntax
IF a = 1 THEN
b = 1
ELSEIF a = 2 THEN
b = 2
endif

and this want agk
IF a = 1
b = 1
ELSEIF a = 2
b = 2
endif

AGK 108 (B)19 : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265
Nieb
10
Years of Service
User Offline
Joined: 13th May 2014
Location: Lurking
Posted: 13th Jun 2014 03:44
It would help with making clean code.

This is clean and nice:


This is a bit messy/verbose:
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 13th Jun 2014 12:15 Edited at: 14th Jun 2014 02:19
maybe this work

1.


2.


AGK 108 (B)19 : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 13th Jun 2014 13:11
Case statement would be cleaner!

-- Jim - When is there going to be a release?
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 13th Jun 2014 15:57 Edited at: 13th Jun 2014 15:57
I agree Jim.

But seriously does it really make much difference? The point is if the logic works and it's possible to write code that is easy to read then great. If you want code that you can squeeze into as few lines as possible it won't be very readable most of the time.

Just for argument's sake though this is my smallest version of the above:

...and the cleanest in my opinion:
Nieb
10
Years of Service
User Offline
Joined: 13th May 2014
Location: Lurking
Posted: 13th Jun 2014 18:39
Quote: "
IF a = 1 : b = 1
ELSEIF a = 2 : b = 2
ELSEIF a = 3 : b = 3
ELSEIF a = 4 THEN b = 4

select a
case 1 : b=1 : endcase
case 2 : b=2 : endcase
case 3 : b=3 : endcase
case 4 : b=4 : endcase
endselect
"


Thanks for sharing, those are much nicer.

Question is ":" the same as ";" in C++?
SoftMotion3D
AGK Developer
19
Years of Service
User Offline
Joined: 24th Aug 2005
Location: Calgary,Alberta
Posted: 13th Jun 2014 22:50 Edited at: 13th Jun 2014 22:51
and your code doesnt nessesarly need to be super tidy if your the only one reading it. just leave plenty of comments within your code. Optimising this tiny code posted above is not gunna gain you [if any] significant processing time so it doesnt matter how you want to write it.

I like bax's cleanest code snippet...but for my self id probably type it in as




1 liner...lol and same as markus

Nieb
10
Years of Service
User Offline
Joined: 13th May 2014
Location: Lurking
Posted: 16th Jun 2014 03:21
Would CASE work with something like this?


Also, is ":" similar to ";" in C++?
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 16th Jun 2014 09:11
Easy in Pascal:



If possible I always use case statements instead of cascading if/else statements, which are hard to read when they get big.

-- Jim - When is there going to be a release?
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 16th Jun 2014 09:39
yes, : is similar, in basic it is the linebreak too.
i believe v1 agk compiler can not understand complex conditions at case.

AGK 108 (B)19 : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 16th Jun 2014 11:13
Quote: "i believe v1 agk compiler can not understand complex conditions at case."


Use Select Time / 100
Case 0 : Score = 1000

etc

-- Jim - When is there going to be a release?
Naphier
14
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 16th Jun 2014 11:42
I've always found case to be fairly worthless because I typically need conditionals. Also,
Quote: "if a = 1
elseif a = 2
endif"


is neater than
Quote: "select a
case 1: //do this :endcase
case 2: //do that :endcase
endselect
"

in my opinion.

And it is very handy to do
Quote: "if a <= 1
elseif a = 2
endif"

instead of having an extra case statement for defaults.

JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 16th Jun 2014 12:33
Case statements can be optimised, whereas elsifs have to descend the entire tree and are therefore degenerative in performance terms.

-- Jim - When is there going to be a release?
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 16th Jun 2014 13:42
It's crazy to say "I always do this" because there will occasionally be cases where there is a better way to do it. It's not always about how easy your code is to read.

I would rather put an additional check in before the select than put a whole ream of elseif's:


I'm sure we can come up with a reason why our way is better than doing it another way but the fact is you should use the best system for each case depending on what is required.

As Jim says case statements are easily optimised so if you can use a case statement it is well worth it.

As it happens I don't think I have EVER used elseif in AGK. Not particularly through choice, I just haven't found a situation where I needed it over another method.

I actually prefer to nest if - else - endif under most circumstances and find it very easy to read using proper indentation. Elseif just feels a little longwinded to me, which is totally illogical.

It's worth knowing as many different ways of writing the same code as possible though. It's easier to work around a problem if you know all the paths.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 16th Jun 2014 13:50
@baxslash
i want this will work in agk ...



AGK 108 (B)19 : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 16th Jun 2014 18:10
Back in the dawn of time when I started programming we used to count instruction cycles in assembler. It could make a big performance difference on an 8-bit processor like a 6502.

We've all been spoiled by desktop super-computers and very clever optimising compilers.

But:

(a) Mobile phones are not super-computers
(b) AppGameKit Basic is probably not very optimised

The burden is back on the programmer to write the most efficient code possible. For example - using "if-else" constructs - arrange the order in the most probable way at the top, even if it's not immediately obvious.

Sometimes you may want to step outside the given language path. In the example above there are only 4 possible states, so that's trivial. What if there were 50 or a hundred? If-else becomes ridiculous, and case statements become verbose. If-else is highly inefficient in all non-trivial situations because the test has to be done repeatedly until it's satisfied. If you're unlucky enough that the select value is mostly last you'll be hitting the processor pointlessly.

So how do old men like me do it for large spreads? Make an array with the results and then pick the appropriate one. We used to do this with sin-tables, because it was slow to calculate the sin(x) values and fast to do a table look-up.

-- Jim - When is there going to be a release?
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 16th Jun 2014 18:40
OT
@JimHawkins
he he i used sin tables too in past but today i think sin/cos
is nonsense, it is just a value from -1 to 1, means for
sprite movement/direction i can split into x,y,z i do not need a angle.

(b) i agree, but i believe its also other open source behind what is slow.

(a) in comparison with C64 its a super-super-super-super-super-super-computer

AGK 108 (B)19 : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265
Naphier
14
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 16th Jun 2014 20:11
@Markus
Cases only work with constant integers. You need IF commands to use conditionals.

I'd love to see proof that there is a performance difference between elseif and case on today's processors. Not that it matters to me because I almost always find myself needing conditionals to test my various cases. The only time I've used case is with my keyboard code that has to check all of the keycodes.

JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 16th Jun 2014 23:51
@Naphier - As baxslash said earlier - it's a case (!) of using the appropriate code for the problem. In the example above, it was indeed a simple integer constant, and the compiler can make a jump-table. That will be efficient for all the constant cases.

Suppose you have 100 possible values to switch on, and use if-else-if, you will have 100 ifs and 100 else-ifs. On any processor, if your value is the last or out of range it will take 100 comparisons. Is that as fast as an inherent jump-table?

-- Jim - When is there going to be a release?
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 17th Jun 2014 00:08
@Naphier
Quote: "Cases only work with constant integers."

in agk (for no reason), true ,where is my freedom?

AGK 108 (B)19 : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265
Naphier
14
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 17th Jun 2014 00:14
I understand the concept. I just don't think that for most cases (!) there will be a significant performance difference. Show me a benchmark on a comparison between a 100 switches vs. 100 elseif.

@Markus - They may also work with literal strings. I've not tried. Are there other languages where switch statements allow conditionals? PHP doesn't. I don't think java does. If it did it would be an elseif statement.

Naphier
14
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 17th Jun 2014 01:07
OK, so I couldn't help myself. I had to see a benchmark NOW apparently (this kind of talk always gets me very interested).

Here's my simple benchmark code:


Results are interesting!
The test is 1,000,000 iterations through a switch and then through an elseif.
Windows 7 (AMD Phenom II)
switch time = 2.743884 seconds
elseif time = 3.135091 seconds (thought I was going to magically get some pie)
dTime = 0.391207 (remember that's the time difference for one million iterations for a 100 case switch/elseif)

Nexus 5 (Qualcomm Snapdragon 800 MSM8974AA, Quad Core 2.26 GHz processor)
switch time = 14.520952 seconds (holy slowness, Batman)
elseif time = 14.786129 seconds
dTime = 0.265177 (ok then...)

So I'll let you all be the judge. Do you want to use a switch and save a whopping 0.265177 to 0.391207 microseconds per frame? I guess... if you have 2,000 of these happening in a single frame then you're approaching a millisecond difference , but otherwise...

I do find it strange and interesting that the dTime is smaller on the Nexus 5. It's also pretty sad that the Nexus 5 is 5 times slower overall.

Also you have to think about the lines of code you have. With AppGameKit T1 every line turns out to be about a kilobyte in bytecode (hopefully not with the new compiler). Elseif "switches" require 2/3 of the lines. For a 100 case switch that would be a savings of 100kb (also a pretty minor deal in today's numbers of gigs and megs). The bigger that bytecode is the slower your game loads and the greater the memory footprint.

Knowing is half the battle! Go Joe!

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 17th Jun 2014 01:17
@Naphier
yes, i know ms languages that can handle this.
the result of much conditions is only one value (true or false),
means select (select true & case conditions) can compare this two values.
i think its logical.
it not prevent me of using constants too at case if i want.

AGK 108 (B)19 : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265
Naphier
14
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 17th Jun 2014 01:34
All conditionals evaluate to true or false.
Switches typically just do a direct comparison (which also evaluates to t/f) without evaluation of any other statements. Just does A = B?
That's the way AppGameKit works. And there's no significant difference in performance. You could always use both together


Login to post a reply

Server time is: 2024-11-25 06:41:42
Your offset time is: 2024-11-25 06:41:42