do while (true); // Getting rid of warnings / Alternative?
#1

pawn Код:
public FindSkin(gender, skin, price){
    new skinid;
    do
    {
        skinid = random(300);
        if(  SkinDB[skinid][Gender] == gender && SkinDB[skinid][Skin] == skin && SkinDB[skinid][Price] == price ) break;
    }
    while( true ); // warning 206: redundant test: constant expression is non-zero
    return skinid; // warning 225: unreachable code
}
Now, this code functions like it should, and does what needed. However, it puts out two warnings, stated in the code as comments. What is the best way to get rid of them. Or what would be a better, more optimal way to do this?
Reply
#2

Код:
public FindSkin(gender, skin, price){
    new skinid;
    for(;;)
    {
        skinid = random(300);
        if(  SkinDB[skinid][Gender] == gender && SkinDB[skinid][Skin] == skin && SkinDB[skinid][Price] == price ) break;
    }
    return skinid; // warning 225: unreachable code
}
Reply
#3

Heh, didn't know this would work. Thanks
Reply
#4

Must add: Pawn- language has not the do-while- function like Java does
Reply
#5

Why would you do do-while? Since while doesn't have a statement for which it shouldn't run there is no need to "do" something first right?

pawn Код:
while(1)
{
        ...
}
Does exactly the same as

pawn Код:
do
{
        ...
} while(1)
Reply
#6

You could do:
pawn Код:
public FindSkin(gender, skin, price)
{
    new skinid;

loop:
   
    skinid = random( 300 );

    if ( SkinDB[skinid][Gender] == gender && SkinDB[skinid][Skin] == skin && SkinDB[skinid][Price] == price )
        return skinid;
   
    goto loop;
}
Reply
#7

Well, at first I did use do-while loop with a valid condition, kinda like ****** posted. Exact code was ( I think ):
pawn Код:
do
{
    skinid = random(300);
}
while (SkinDB[skinid][Gender] != gender && SkinDB[skinid][Skin] != skin && SkinDB[skinid][Price] != price);
But this didn't work like its supposed to. It have me a wrong skinID. I figured it did a loop one more time, and returned the next random value instead. Maybe, there was some other mistake..
The truth is, I'm a bit ill today, and it's kind of hard to focus. So some stupid little mistakes are keen to come. Still, I wish to get something done despite it.

The code will be used only once, when the player registers. All of the gender/skin combinations are there, and the price variable is there for another reason, for later. For now, the function will only search for skins with the price of -1. So there shouldn't be a condition where the loop can't break.

For now I will stick with the infinite for loop, as it's something new for me, and it works I doubt there's much room for optimization anyway. ( I hope someone proves me wrong here :P )
Reply
#8

Ah, I see. Thanks for the help, everyone
Reply
#9

omfg ***** ur really smart
you are pawn's (andwhatnototherlangs) einstein with a little touch of aristotle and some pythagoras topping with as desert some galilei-gelly(lol those look like eachother)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)