Author Topic: ElseIF v's Or  (Read 5530 times)

0 Members and 1 Guest are viewing this topic.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
ElseIF v's Or
« on: April 06, 2022, 04:54:00 pm »
Ok, time for another dumb questions on "Why can't I...?"

Now I realize there is such a thing as ELSEIF. I do use it a lot. But I often find myself wondering why an IF statement can not follow an OR.

To give you an example.. Suppose you have an Array (1 to 10) and you want to search the array for duplicate, or triplicate, or quadruplicate, or Quintuplicate, or Sextuplicate etc values

For x = 1 to 10
  For y = 1 to 10
     If Array(x) = Array(y) then Duplicate = Array(x)
     or IF Array(x) = Array(y) and Array(x)=Array(y+1) then Tripiplicate = Array(x)

etc etc


Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: ElseIF v's Or
« Reply #1 on: April 06, 2022, 05:38:33 pm »
From your example, it'd just be a complication of values.

For x = 1 to 10
  For y = 1 to 10
     If Array(x) = Array(y) then
         Duplicate = Array(x)
         IF Array(x)=Array(y+1) then Tripiplicate = Array(x)
     END IF
etc etc

Isn't the above much easier to understand and follow than what you had?
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: ElseIF v's Or
« Reply #2 on: April 06, 2022, 05:41:40 pm »
In your example would you get a triplicate without the AND? The mix of logical operators with If statements I suppose is the problem.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: ElseIF v's Or
« Reply #3 on: April 06, 2022, 05:55:54 pm »
Ok, time for another dumb questions on "Why can't I...?"

Now I realize there is such a thing as ELSEIF. I do use it a lot. But I often find myself wondering why an IF statement can not follow an OR.

To give you an example.. Suppose you have an Array (1 to 10) and you want to search the array for duplicate, or triplicate, or quadruplicate, or Quintuplicate, or Sextuplicate etc values

For x = 1 to 10
  For y = 1 to 10
     If Array(x) = Array(y) then Duplicate = Array(x)
     or IF Array(x) = Array(y) and Array(x)=Array(y+1) then Tripiplicate = Array(x)

etc etc



I predict 10 duplicates even if all the values of Array() are different!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: ElseIF v's Or
« Reply #4 on: April 06, 2022, 06:35:46 pm »
In your example would you get a triplicate without the AND?

You would.

     If Array(x) = Array(y) then  <--- This line checks to see if we have a duplicate first.
         Duplicate = Array(x)
         IF Array(x)=Array(y+1) then Tripiplicate = Array(x)  <--- And only if we have a duplicate can we check to see if it's a triplicate.
     END IF

Look at your code for a brief moment:

     If Array(x) = Array(y) then Duplicate = Array(x)
     or IF Array(x) = Array(y) and Array(x)=Array(y+1) then Tripiplicate = Array(x)

Notice how the conditions are *exactly* the same for the portions I colored?  Those lines can be simplified so that a single IF checks for their condition, with sub-conditions coming inside the main if.

Think of it as:

IF x >0 AND x < 4 THEN PRINT "I"
IF x > 0 AND x < 3 THEN PRINT "I"
IF X > 0 and X < 2 THEN PRINT "I"

The above is a simple Roman Numeral printer for values from 1 to 3, but it's got a total of 6 IF checks to pass before it goes on past this segment of code.  The exact same thing can be simplified by nestled IF structuring:

IF x > 0 Then 'Every condition needs x to be greater than 0
   IF x < 4 then Print "I"
   IF x < 3 then Print "I"
   IF x < 2 then PRINT "I"
END IF
 
Now there's only a total of 4 IF checks to pass, and the results will be exactly the same.  ;)

When you have segments that contain the exact same conditions to operate, it's often simplest to merge them together under one blanket nestled condition.


https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: ElseIF v's Or
« Reply #5 on: April 07, 2022, 08:38:08 am »
I see it Steve , but my conundrum is a decisional statement following a logical operator. Mostly the decisional statement is an IF statement (I haven't tried it with a Select Case statement). But it seems logical to me to pose the formula

 if A=B or IF A=C then ...

if A=B and if A=C then ..

Brackets don't work either ..(if A=B) or (if A=C) then ..

I suppose Elseif could be considered a substitute for OR or AND but when it comes right down to it, Elseif is conditional and not logical. I do realize you can use logical operators within an IF statement ..If A=B or A=C then ... works fine but no go between two if statements.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: ElseIF v's Or
« Reply #6 on: April 07, 2022, 08:52:56 am »
I see it Steve , but my conundrum is a decisional statement following a logical operator. Mostly the decisional statement is an IF statement (I haven't tried it with a Select Case statement). But it seems logical to me to pose the formula

 if A=B or IF A=C then ...

if A=B and if A=C then ..

Brackets don't work either ..(if A=B) or (if A=C) then ..

I suppose Elseif could be considered a substitute for OR or AND but when it comes right down to it, Elseif is conditional and not logical. I do realize you can use logical operators within an IF statement ..If A=B or A=C then ... works fine but no go between two if statements.

In this case, you just use the and/or without the IF.

IF A = B OR A = C Then...
IF A = B AND A = C Then...

Why would you need that extra If statement after the And/Or?  Tell me how exactly adding the word IF in there after that And/Or would change performance at all.   

IF A = B OR IF A = C THEN PRINT "Hello World"

What happens if A = B?  Do we evaluate true and say hello to the world?  What if A = C instead?  What if it doesn't equal either?  And how would it be any different if we took that IF out of the statement after that OR?

What am I not seeing here??
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: ElseIF v's Or
« Reply #7 on: April 07, 2022, 09:37:14 am »
The main difference is the logical comparison between two conditional statements v's the logical comparison between the value in two variables. It's also a more natural way in which logic is processed while theorizing. If you know, in the run of your program, that A "will" reach 101 then you can write code to deal with that condition. But if you have no idea if it will generate a value of 101 then you write code to "IF" it reaches 101. The code to deal with that condition could be the same but if 101 is a complete surprise then you may want to do something more.

I can see from your perspective that a logical operator between two concrete values can cover a lot of scenarios where the IF becomes redundant. After all, whether the 101 is expected or a surprise code can be developed to deal with the result ... So I guess we chalk this up to another one of those dumb questions.