Better range based IFs
#1

Did you know that instead of this:
Код:
for(new i = 0; i < 50; i++) {
    if(i >= 15 && i <= 30) {
        print("ugly :(");
    }
}
You can do this:
Код:
for(new i = 0; i < 50; i++) {
    if(15 <= i <= 30) {
        print("yay!");
    }
}
Which is much more compact and clear?
Reply
#2

personally number 1 for me is more clear to me, it makes more sense. Like loop through all players, if within the loop i is in betweem 15 and 30 it will print ugly. Correct me if im wrong.
Reply
#3

they both do the same things, but in situations like this i would go with the 2nd one
Reply
#4

Quote:
Originally Posted by kamzaf
Посмотреть сообщение
personally number 1 for me is more clear to me, it makes more sense. Like loop through all players, if within the loop i is in betweem 15 and 30 it will print ugly. Correct me if im wrong.
For you, if(i >= 15 && i <= 30) is more clear than if(15 <= i <= 30) ? o.O They both do the same..
Reply
#5

something didn't look good to me
Hmm.
1st one
pawn Код:
if(20 >= 15 && 20 <= 30)
meaning
if 20 is the same/bigger then 15
AND
if 20 is the same/lower then 30

2nd way
pawn Код:
if(15 <= 20 <= 30) {
meaning:
if 20 is same/smaller then 15
AND
if 20 is same/samller then 30

NOT the same
Reply
#6

I don't know if that's just an example, but I don't understand why you have so much bigger loop than what you actually need.

Код:
for(new i = 15; i <= 30; i++) 
{
     print("yay!");
}
Reply
#7

Quote:
Originally Posted by park4bmx
Посмотреть сообщение
something didn't look good to me
Hmm.
1st one
pawn Код:
if(20 >= 15 && 20 <= 30)
meaning
if 20 is the same/bigger then 15
AND
if 20 is the same/lower then 30

2nd way
pawn Код:
if(15 <= 20 <= 30) {
meaning:
if 20 is same/smaller then 15
AND
if 20 is same/samller then 30

NOT the same
They ARE the same.
if(15 <= i <= 30)
- i is same / bigger than 15
- i is less / same than 30

@Finn - It is only an example.
Reply
#8

a 2nd look yes,
i too the "<=" as a point of the i on its right side
but then again 15 does the check not i
Reply
#9

Well, if you are interested, then learn about Yoda conditionals (I learned it from symfony coding standards).
Instead
pawn Код:
if(variable == 5)
you do
pawn Код:
if(5 == variable)
Why? Sometimes people make typos. And we are people. So if there was
pawn Код:
if(variable = 5)
Then you'd get random assignemnt istead of comparison
pawn Код:
if(5 = variable)
Syntax error, assigning variable to constant.

Note:
In PAWN we have "warning 211: possibly unintended assignment;", but still this is useful if you operate with a number of languages
Reply
#10

Quote:
Originally Posted by Misiur
Посмотреть сообщение
Well, if you are interested, then learn about Yoda conditionals (I learned it from symfony coding standards).
Instead
pawn Код:
if(variable == 5)
you do
pawn Код:
if(5 == variable)
Why? Sometimes people make typos. And we are people. So if there was
pawn Код:
if(variable = 5)
Then you'd get random assignemnt istead of comparison
pawn Код:
if(5 = variable)
Syntax error, assigning variable to constant.

Note:
In PAWN we have "warning 211: possibly unintended assignment;", but still this is useful if you operate with a number of languages
I know about them, but I never made such a mistake and it really just makes less sense imo.
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)