gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > C if-else statement doesn't work. So simple... Argh!

#16098 - AndOrAFK - Sun Feb 08, 2004 10:29 am

I have two years of VB experience under my belt, most recently programming an iTunes lookalike for the PC (before Apple decided to release their own, official version... cuppertino is always one step ahead of everyone, including yours truly). I wanted to start programming the GBA recently as a hobby, so I decided I'd pick up C. However, I can't seem to do even the simplest things. An example:
Code:
unsigned long foo; //used to detect if [A] is pressed
...
        foo = 2; //set to 2, rather than Get_Input();, to test the if-else statement.

        if (foo=1)
        {
            ScreenBuffer[0] = RGB16_BGR(31,31,31);
        }
        else
        {
            ScreenBuffer[0] = RGB16_BGR(0,31,0);
        }

The funny thing is... the first statement *always* executes. Which is impossible, since foo is set to 2, not one! Argh! What's going on here?

#16099 - qw3rty - Sun Feb 08, 2004 10:41 am

the equal comparsion is "==" and NOT "="...

#16100 - AndOrAFK - Sun Feb 08, 2004 10:44 am

-_-* Well, how the heck was I supposed to know that the "=" operator did double-duty (in more ways than one. heh.)

C'mon over here, I've got some scissors for that snip in your voice. =P j/k

Thanks. =)

#16101 - tom - Sun Feb 08, 2004 12:27 pm

AndOrAFK wrote:
-_-* Well, how the heck was I supposed to know that the "=" operator ...


by reading a... gasp...book about c ?

#16123 - AndOrAFK - Mon Feb 09, 2004 2:14 am

Heh. Okay, okay, enough with the sarcasm. I admit that I made a mistake. On the other hand, last night I finished my first working C program - a scrolling 24x40 tile background. Perhaps it's not much, but on the other hand, I didn't know any C whatsoever until about 8 hours before I started.

Get it here! [uses gba.h by Eloist and keypad.h by Nokturn]

Now, let's see if I can figure out how to work in an animated sprite. Ah CowBiteSpec, how I adore thee.

#16141 - niltsair - Mon Feb 09, 2004 8:20 pm

Something of note with C, it's optimized to stop as soon as something is known to be true or false, unlike VB that evaluate the whole expression.
Code:

int a = 0;
int b = 3;
if( (a != 0) && (b / a == 3) )
{
     //Do something
}
else
{
    //Do something else
}
In Vb this would have caused an error because 'a' is equal to 0, thus 'b / a' fail. In c, as soon as the expression can't possibly be true it stop and won't evaluate the rest. This is something that alway annoyed me in Vb, how you need to create another 'if' statement to prevent error like that.

Note:
!= means 'different'
&& means 'and'
|| means 'or'
_________________
-Inside every large program is a small program struggling to get out. (Hoare's Law of Large Programs)
-The man who can smile when things go wrong has thought of someone he can blame it on. (Nixon's Theorem)

#16142 - animension - Mon Feb 09, 2004 8:26 pm

The GBA isn't exactly the best place to learn C and put it into practice. C is complicated enough as it is without the hardware limitations of a platform like GBA, not to mention the fact that there is no operating system on the platform meaning you have to write your own hardware drivers.

If you don't know anything at all about C, I strongly recommend you first pick up the book called "The Absolute Beginner's Guide to C" (I'm not being sarcastic here, this book really rocks!) and first learn how to program on a platform that has an operating system. Once you feel comfortable with C, then come back to the GBA. Working directly with the hardware like you have to on the GBA really is *not* the ideal learning environment, nor is it the mainstream method in which C is programmed.
_________________
"Beer is proof that God loves us and wants us to be happy."
-- Benjamin Franklin

#16153 - dagamer34 - Mon Feb 09, 2004 11:54 pm

You learn quickly...
_________________
Little kids and Playstation 2's don't mix. :(

#16210 - AndOrAFK - Tue Feb 10, 2004 5:35 pm

Well, I worked on my program again this morning for an hour, and I got sprites working fairly quickly (it wouldn't have taken so long, except that initially, I was DMA-ing the OAMData (0x06010000) into my sprites, instead of the other way. Silly rabbit, trix are for kids. =P

@niltsair: ooh, now that's cool - although I wouldn't think anything of simply nesting if statements - I'm a neat freak when I'm coding, and nesting statements helps keep me organized. Thanks for the and\or\not-equal shorthand, btw - one of the things I love about C (so far) is that I can very easily and\or bits into bytes - I love the ability to have direct control over my memory! Oh, and pointers are awesome (VB has pointers, but I used them only for APIs). =)

@animension: Thanks for the advice, but I've been known to bite off more than I can chew before. =) Although I lament the loss of directsound, I think I can get by without all the other win32 crutches I use. And oddly enough, I find the limitations of the GBA comforting - it has a very specific way it wants to work (put backgrounds HERE, put your sprite arrays HERE), and thus, I'm never at a loss as to where I might store data.

I'll check out that book, btw. Thanks for the recomendation.

@dagamer34: heheh. I try. Those years of VB6 certainly help.

#16222 - niltsair - Tue Feb 10, 2004 9:01 pm

Here's the bits operators :
Val >> X : Move bits of Val X bits to the right (divide)
Val << X : Move bits of Val X bits to the Left (multiply)
& : And operator for bits
| : Or operator for bits
^ : Xor (exclusive or)
~ : Bits complement
_________________
-Inside every large program is a small program struggling to get out. (Hoare's Law of Large Programs)
-The man who can smile when things go wrong has thought of someone he can blame it on. (Nixon's Theorem)

#16311 - sgeos - Thu Feb 12, 2004 5:56 am

I learned C from a book. I recommend the book I learned from:

Practical C Programming by Steve Oualline.
Published by O'Reilly.
ISBN 1-56592-306-5

It talks about = vs == on pages 10 and 91.

-Brendan