Author Topic: IF statement  (Read 6816 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline 40wattstudio

  • Newbie
  • Posts: 82
    • View Profile
    • 40wattstudio
IF statement
« on: December 29, 2020, 08:36:10 pm »
Let's say you have an IF statement with multiple conditions . . .

IF Condition A = False AND Condition B and Condition C . . . AND Condition Z THEN
Do something
END IF

Now let's suppose that Condition A is True, will the IF statement continue to check all the other conditions or will it skip the rest and move on?

Asking because I have some IF statements with quite a few conditions and trying to see if it's necessary to reorganize things or just leave it as is.

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: IF statement
« Reply #1 on: December 29, 2020, 08:39:18 pm »
That first AND clause requires that condition A be false in order for the IF statement to be considered true. You'de probably have to break it down.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: IF statement
« Reply #2 on: December 29, 2020, 08:44:55 pm »
In my experience, break it down.  Especially if there’s anything more involved like an OR...

IF x AND y OR z THEN.....

Is that (x and y) or z, or is it x and (y or z)??

Its confusing with just 3 elements...  Now who the heck can decipher A to Z conditions?

Keep it simple so you (and others) can follow the logic in the future.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline 40wattstudio

  • Newbie
  • Posts: 82
    • View Profile
    • 40wattstudio
Re: IF statement
« Reply #3 on: December 29, 2020, 08:48:05 pm »
That first AND clause requires that condition A be false in order for the IF statement to be considered true.

Understood, but would it stop immediately after it sees that condition A isn't what is expected, or does it still have to process the rest of the IF statement?

Alternately, I could write it like this:

IF Condition A = False THEN
      IF Condition B AND Condition C . . . AND Condition Z THEN
      END IF
END IF

And that would isolate that one condition, but again, I'm not sure if that's even necessary.

Marked as best answer by 40wattstudio on December 29, 2020, 04:02:12 pm

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: IF statement
« Reply #4 on: December 29, 2020, 09:00:26 pm »
It processes them all.

As I was mentioning, imagine that last condition was OR Condition_Z...   It follows basic order of operations like my math evaluator does, until it gets down to a singular check of condition1 relation condition2.

You’re best served to break it down to eliminate checks, as your example above, if speed is a consideration .
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: IF statement
« Reply #5 on: December 29, 2020, 09:01:21 pm »
Yep, now you're getting it. Also, look into ELSEIF

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline 40wattstudio

  • Newbie
  • Posts: 82
    • View Profile
    • 40wattstudio
Re: IF statement
« Reply #6 on: December 29, 2020, 09:04:40 pm »
Perfect! Thanks OldMoses, SMcNeill and Pete! Very good stuff to know! Yes, speed is a consideration in the program I'm working on. I made a simple counter to see how many times the main loop ran in 1 second and noticed that when lots of stuff is going on the number of iterations would sometimes drop significantly. So hopefully this will help alleviate that somewhat.

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: IF statement
« Reply #7 on: December 29, 2020, 10:09:17 pm »
Side note: this behaviour (stop checking conditions once the final result is determined) is called short-circuit evaluation. Some other programming languages do this, QB64 doesn't.

Offline 40wattstudio

  • Newbie
  • Posts: 82
    • View Profile
    • 40wattstudio
Re: IF statement
« Reply #8 on: December 30, 2020, 12:32:28 pm »
Side note: this behaviour (stop checking conditions once the final result is determined) is called short-circuit evaluation. Some other programming languages do this, QB64 doesn't.

Awesome, thanks Luke! I was wondering if there was a special name for it. I'm not sure what all is involved with developing QB64, but it'd be cool if future versions had that behaviour built-in.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: IF statement
« Reply #9 on: December 30, 2020, 12:54:11 pm »
Quote
but it'd be cool if future versions had that behaviour built-in.

I think just nesting the IF's and optional ELSEIF's is way more transparent and easier to follow. Who the heck wants to use a LOGIC Table to predict how a little bit of decision making code is going to work. The -1 for True is bad enough ;-))

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: IF statement
« Reply #10 on: December 30, 2020, 12:56:29 pm »
Short circuit evaluation is a thing that's implemented behind the scenes (supposing we had it). The QB64 coder would be none the wiser if it was there, up to a very small speed boost at runtime.
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: IF statement
« Reply #11 on: December 30, 2020, 01:03:40 pm »
Well what I am saying is nest the IF's yourself (or use ELSEIF's or use SELECT CASE), get the speed boost today PLUS understand your code, specially 2 years, heck 2 weeks, down the line.
« Last Edit: December 30, 2020, 01:05:50 pm by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: IF statement
« Reply #12 on: December 30, 2020, 01:09:15 pm »
I prefer just nesting the conditional statements. If the first is false, it skips the others. It was more important back in the QBasic days of slower slower processors. I agree with Bill in that I don't see adding "short circuiting" to QB64 as important, unless it's very simple to implement. It's like putting aerodynamic air valve caps on a sports car. You could, but why bother?

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: IF statement
« Reply #13 on: December 30, 2020, 05:55:59 pm »
Unfortunately short-circuiting isn't a transparent optimisation. Imagine this code:
Code: [Select]
DIM SHARED S

IF S >= 0 OR F(2) THEN PRINT S

FUNCTION F(N)
    S = N
    F = N
END FUNCTION
In the current language that would print 2 because F(2) is always called, resulting in the shared variable being updated. But if we sort-circuited, S >= 0 is true so we'd never evaluate the F(2) and thus print 0.

Some languages (Ada comes to mind) have separate operators; "and" is like QB's AND, but the "and then" operator short-circuits. That'd be difficult to implement in QB64 though.

FellippeHeitor

  • Guest
Re: IF statement
« Reply #14 on: December 30, 2020, 06:00:04 pm »
In the spirit of QB64's essence, I don't even think it should.