This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/MSP430F5438A: If and else statements

Part Number: MSP430F5438A


Tool/software: Code Composer Studio

I've only recently started programming in my course and I'm having trouble understanding the way the code is written.

First question I have: when i write an if statement, does it always need an else for it to work or can i just leave it out?

second question: i've seen things written like:

if (P2IN & BIT6)

{

P1OUT | = BIT0

}

my question is, what are they braces doing? are they necessary and if i don't have them in code like that, will it not function properly?

thanks

                                                                         

  • You do not need an "else" branch.
    The braces are necessary only when they enclose multiple statements.

    This question is about the C language and has nothing to do with microcontrollers. I'd recommend learning C before also trying to learn about this hardware.
  • There are some great resources online for learning C. As Clemens suggested I would start with some of those first. Sounds like you have an MSP430 board that you can run things on. Ideally it is easier to just learn running some programs on your PC but there is nothing wrong with running them on the board but there are some things to be aware of. Embedded devices like the MSP430 have a limited amount of memory, so something as simple as a printf() statement can eat up a lot of that memory. These devices are designed to run for years off a small cell battery so power management is very important. Thus starting off learning the language on a PC and then moving to the embedded device is easier. Learning the basics of C is pretty easy, especially if you have coded something else before. Just run through some lessons online and then pick up your MSP430 board and start working with the example programs.

    Regards,
    John