problem with IF

Talk about making games.

problem with IF

Postby will97 » Thu Nov 16, 2006 2:09 am

i'm not an expert (some of you will thought that i am stupide but...)

i need help with the IF thing

can somebody here can tell me how to use it

(i tryed a lot of thing and they all dont work)

Code: Select all
if ()
{
   
}
User avatar
will97
 
Posts: 11
Joined: Sun Nov 12, 2006 3:40 pm
Score: 0 Give a positive score

Postby Fuzzy » Thu Nov 16, 2006 5:05 am

an example..

if (something is tested)
{
do something here if the test is true
}

is how it works. now as for what to fill in..

if (Jumping==1)
{
Here is code to make him fall
}

see the double ==? that means compare. if you only use one = then it means assign.

so...

x = 100;

means make x contain the value 100

x == 100

means compare the value of x to the value 100

if you use a single = in an if sign, it assigns that value, and the if statement acts true, causing the instuctions within the {} brackets to be executed. Its a common error.

What if you wanted to see if something is not true? use this

if (jumping != 1)
{
allow character to jump code
}

! means not, so != means not-equal

if (x > 100)

means check to see if x is greater than 100.

if (x < 100) means check to see if it is less than 100.

if (x <= 100) is greater than or equal.. and the opposite is >=

you may also combine two tests in one if...
if ((x > 100) && (Jumping == 1))

&& means 'and'

|| means 'or', so if either test passes, do the code.

you may also occasionally use

if ((test == 1) ! (jumping == 1))

which means if the first, but NOT the second is true, execute the code.

You may also see

if (test)
{

}

which presumes that the variable is true, or positive.

and...
if (!test)

which means its not true..


hope this helps. if you have a defective If statement, post it here, and we will examine it for errors.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Postby sonicfire » Thu Nov 16, 2006 10:17 am

and dont forget the else statement:

Code: Select all
this = 0; (this is false)


if (this)
{
    // do something
} else {   // since "this" is 0
    // do something else
}


you can also add more if-else-statements if you like:
Code: Select all
this = 0; (this is false)
that = 1; (that is true)

if (this)
{
    // do something
} else {   // since "this" is 0
    if (that == 1) // or just write "if (that)" (if that is true)
    {
         // do that
     }
}
sonicfire
 
Posts: 425
Joined: Wed Nov 01, 2006 9:34 pm
Location: berlin germany
Score: 16 Give a positive score

Postby Game A Gogo » Thu Nov 16, 2006 10:39 pm

and also the else if command, it is use when you want to check something like this:
Code: Select all
if(var==1)
{
//do something
}
if(var==2)
{
//do something else
}
if(var==3)
{
//do something
}
if(var>=4)
{
//do something
}

You could turn the code like this:
Code: Select all
if(var==1)
{
//do something
}
else if(var==2)
{
//do something else
}
else if(var==3)
{
//do something
}
else if(var>=4)
{
//do something
}

that could augment a little the frame rate when the action is executated, this is useful in the draw actor.
but remember, you still can do this:
Code: Select all
if(var==1)
{
//do something
}
else if(var==2)
{
//do something else
}
if(var2!=1 && var==3)
{
//do something
}
else if(var2>var)
{
//do something else
}


And if you just have ONE line of code, you still can do this
Code: Select all
if(var<=10)//put the action here


instead of
Code: Select all
if(var<=10)
{
//action here
}


hope this helps also!
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Postby will97 » Sun Dec 03, 2006 1:52 pm

:?: I try this and it work :?:

Code: Select all
if (somthing)
the action
User avatar
will97
 
Posts: 11
Joined: Sun Nov 12, 2006 3:40 pm
Score: 0 Give a positive score

Postby Game A Gogo » Sun Dec 03, 2006 3:16 pm

it because there isn't a line break at the end of the IF statement i think.
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest