Author Topic: Pls bear with me and clear up my confusion over a simple if/else  (Read 2510 times)

0 Members and 1 Guest are viewing this topic.

Offline GrandewdTopic starter

  • Full Member
  • ***
  • Posts: 239
  • Activity:
    0%
  • Reputation Power: 3
  • Grandewd has no influence.
  • Respect: +44
  • Referrals: 0
    • View Profile
0
Clear this up for me, once and for all pls...

 If "if" is satisfied by a single condition are brackets required - (that may be the wrong syntax, so let me show you what I'm trying to say........

Can I do this:

Code: [Select]
If #findcnt = 0
     do this one thing   <<<< this doesn't have to be bracketed, right?
else
do this thing    <<<<<and do I need a bracket for these 3 here?
do this thing
do etc....

Or do I have to do this:

Code: [Select]
If #findcnt = 0
  {
   do this one thing
  }
else
  {
   do this thing   
   do this thing
   do etc....
 }

Offline dxrom

  • Master of the milestones!
  • Elite
  • *
  • *
  • Posts: 1080
  • Activity:
    0%
  • Reputation Power: 15
  • dxrom is working their way up.dxrom is working their way up.dxrom is working their way up.
  • KEYBOARD COWBOY, GREAT SAMURAI OF THE INTERNET.
  • Respect: +268
  • Referrals: 1
    • View Profile
Re: Pls bear with me and clear up my confusion over a simple if/else
« Reply #1 on: October 14, 2013, 09:19:49 PM »
0
Brackets are required for multiple statements... However they're not required when there will be only one statement to execute. I would personally prefer to utilize them just to keep code clean, neat and effectively helps when debugging code... I believe that excess lines though will hurt the overall runtime of EUO scripts as it executes literally line by line, so that's the draw back (If you're a stickler over fractions of milliseconds being wasted).

Example:
Code: [Select]
if #findcount = 0
  {
    do this one thing
  }

is the same as

Code: [Select]
if #findcount = 0
  do this one thing

Incorrect use would be:
Code: [Select]
if #findcount = 0
  do this thing
  also do this thing
  and this thing
This is incorrect because everything after "do this thing" (also do this thing, and this thing) will always execute regardless of your if statement. That is where brackets are required.

This carries over into all execution regions that would exceed a single line.

Example:

In this example, either "do this" OR "do this 2" will execute while "do this 3" and "do this 4" will always execute, because EUO doesn't understand that it's an entire region specific to only the if-else statement.
Code: [Select]
if #findcount = 0
  do this
else
  do this 2
  do this 3 ;will execute regardless of if-else statement
  do this 4 ;will execute regardless of if-else statement

So you would need brackets containing the else statement's execution region.

Code: [Select]
if #findcount = 0
  do this
else
  {
    do this 2
    do this 3
    do this 4
  }

Vice versa:
Code: [Select]
if #findcount = 0
  {
    do this
    do this also
  }
else
  do this 2

And ofcourse:
Code: [Select]
if #findcount = 0
  {
    do this
    do this also
  }
else
  {
    do this 2
    do this 3
    do this 4
  }



 ​_██​_
(ಠ​_ృ)
I do say, ol' Chap! Come play EVE Online! Why here is a 21 Day Free Trial!

Offline GrandewdTopic starter

  • Full Member
  • ***
  • Posts: 239
  • Activity:
    0%
  • Reputation Power: 3
  • Grandewd has no influence.
  • Respect: +44
  • Referrals: 0
    • View Profile
Re: Pls bear with me and clear up my confusion over a simple if/else
« Reply #2 on: October 15, 2013, 01:52:47 AM »
0
Excellent...

So brackets are NOT needed, either after if or after else - unless there is more than a single statement to execute!

I agree, that brackets help debug (especially if I haven't seen the script in a while) - but as long as I understand what is and isn't executed, there's no reason why I can't skip some annoying brackets...

I tend to write my scripts in bits and pieces and tie it together as I go along... I test everything before I move on....
« Last Edit: October 15, 2013, 01:54:25 AM by Grandewd »

Offline gimlet

  • Very Super Secret
  • Global Moderator
  • *
  • *
  • Posts: 6217
  • Activity:
    1.4%
  • Reputation Power: 72
  • gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!
  • Gender: Male
  • Respect: +624
  • Referrals: 3
    • View Profile
Re: Pls bear with me and clear up my confusion over a simple if/else
« Reply #3 on: October 15, 2013, 06:17:10 AM »
0
I always use {} myself - then it is easy to add stuff as I develop.

Offline Alpha

  • Hero Member
  • *
  • Posts: 583
  • Activity:
    0%
  • Reputation Power: 10
  • Alpha barely matters.Alpha barely matters.
  • Respect: +78
  • Referrals: 0
    • View Profile
Re: Pls bear with me and clear up my confusion over a simple if/else
« Reply #4 on: October 15, 2013, 07:12:50 AM »
0
I actually put both the brackets to define a given regions as the firs thing I do simply so that I don't forget that end closing bracket heh.

Offline dxrom

  • Master of the milestones!
  • Elite
  • *
  • *
  • Posts: 1080
  • Activity:
    0%
  • Reputation Power: 15
  • dxrom is working their way up.dxrom is working their way up.dxrom is working their way up.
  • KEYBOARD COWBOY, GREAT SAMURAI OF THE INTERNET.
  • Respect: +268
  • Referrals: 1
    • View Profile
Re: Pls bear with me and clear up my confusion over a simple if/else
« Reply #5 on: October 15, 2013, 09:21:41 AM »
0
It became a force of habit for me in Stealth. When I write an if-then or if-then-else statement I add the begin and ends (which are basically the same thing as brackets) before I add the execution statements.

IE:

Code: [Select]
if(  ) then
begin

end;

if(  ) then
begin

end
else begin

end;

while( Connected ) AND ( NOT Dead ) do
begin
  if(  ) then
  begin

  end;
end;

Then I fill in everything else, it basically allows me to start with a nice clean template.



 ​_██​_
(ಠ​_ృ)
I do say, ol' Chap! Come play EVE Online! Why here is a 21 Day Free Trial!

Tags: