Scripting Error with Nested 'If'

Things you’d like to miss in the future...
Forum rules
You might want to read this if you're looking for priority service

:info: Please Include The Following Information
1) Your XYplorer Version (e.g., v28.00.0801)
2) Your Windows Version (e.g., Win 11)
3) Your Screen Scaling Percentage (e.g., 125%).

:info: Use Your Profile Or Signature
We strongly recommend adding your Windows Version and Screen Scaling Percentage to the Location field in your Profile or to your Signature. That way, you only have to type them once, and we won't have to search for that vital information.

:info: How To Attach Images
When attaching an Image, please use the Attachment tab at the bottom of your post and click "Add files".
Post Reply
PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Scripting Error with Nested 'If'

Post by PeterH »

The result of the following code is wrong!
It's a simplified excerpt from real code, just to test/show the problem.
(Note: $b *could* be '', too.)

Code: Select all

  $a = '';
  $b = 'b';
  
  if ($a = '')
  { $a = $b;
    if ($a = '') {$a = 'cc';}
  }
  
  Msg $a; 
The first If correctly recognizes 'True' and executes it's conditional code: OK.
The second If correctly recognizes 'False', but nevertheless executes it's conditional code: that's wrong!

"Stepping" shows for the condition of the 2nd If
b = ''
but then the conditional
$a = 'cc'
is wrongly executed!

In the moment when stepping shows
$a = 'cc';
you can look for Variables: $a still contains 'b'! Then it's overwritten.

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Scripting Error with Nested 'If'

Post by Stefan »

Is this an typo just now
or did you really use
if ($a = '')

instead of
if ($a == '')


?


See Help > Advanced Topics > Scripting > If/ElseIf/Else Blocks
Examples:

Code: Select all

// shows "else", "elseif", "if"
 $i = 1;
 while ($i) {
   if ($i == 3) {
     echo "if";
     break 2;
   }
   elseif ($i == 2) {
     echo "elseif";
   }
   else {
     echo "else";
   }
   $i++;
 }

highend
Posts: 15016
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Scripting Error with Nested 'If'

Post by highend »

Code: Select all

  $a = '';
  $b = 'b';
  
  if ($a == '')
  { $a = $b;
    if ($a == '') {$a = 'cc';}
  }
  
  Msg $a;
Works as expected so you probably made the error of assigning a value in your if statements instead of comparing it with a variable.

Edit: Oh sorry, Stefan answered it already (had to do something else in the meantime while my answer was open in another browser tab).
One of my scripts helped you out? Please donate via Paypal

PeterH
Posts: 2830
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Scripting Error with Nested 'If'

Post by PeterH »

:oops: You are right...

Seems when you have programmed in one language you should never ever try to use another - you always run the risk of mixing up the syntax...
(...and it wasn't only one for me :roll: )
OK: one exception might be assembler 8) - no big risk to mix 'em up...

Trasd
Posts: 147
Joined: 19 Oct 2011 15:45

Re: Scripting Error with Nested 'If'

Post by Trasd »

PeterH wrote::oops: You are right...

Seems when you have programmed in one language you should never ever try to use another - you always run the risk of mixing up the syntax...
(...and it wasn't only one for me :roll: )
OK: one exception might be assembler 8) - no big risk to mix 'em up...
Don't feel bad, I've stared at code for days only to remember I was using another's syntax! It's all part of the fun...
Trasd

"Any sufficiently advanced technology is indistinguishable from magic." A.C.C.

"I'll tell you this, no eternal reward will forgive us now for wasting the dawn." J. M.

Post Reply