NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » if statements...

October 27, 2010
by Quinnifir
Quinnifir's Avatar

somebody please help me with my if and else statements! this is what i put in:

while(2);

time = time + 1;

if (a1 != 1);

delay_ms(10);

  return 2;

else{

  lcd_write_int16(time);

and when i compile, it says there is an else without a previous if. thank you, from quinnifir.

October 27, 2010
by Quinnifir
Quinnifir's Avatar

oh sory about that ... new to these fourms

October 27, 2010
by bretm
bretm's Avatar

A couple of problems. First, in C, a semicolon marks the end of a statement, so when you do

while(2);

it will just spin there forever. You're saying "while 2 doesn't equal zero, do nothing". Nothing after the "while" statement is related to the while statement at all. Indenting means nothing in C, unlike some languages like Python.

Same problem with the semi-colon here:

if (a1 != 1);

That semicolon on the end marks the end of the "if" statement. Nothing after that is related to the "if" statement in any way. That means that "return 2" will always execute.

Next, the "while" and "if" statement only let you run a single statement if it evaluates to true. If you need to run multiple statements you need to use curly braces to combine them into a single compound statement (and get rid of the semi-colon):

while(2)      // get rid of semicolon
{             // begin compound statement for "while"
    if (a1 != 1)  // get rid of semicolon
    {             // start a compound statement
        delay_ms(10);
        return 2;
    }             // don't put a semicolon here

Now you can add an "else" clause, because the it's immediately preceded by an "if".

    else {        // you didn't have a matching close-brace in your example
      lcd_write_int16(time);  
    }             // matching close-brace from "else"
}                 // matching close-brace from "while"
November 02, 2010
by Quinnifir
Quinnifir's Avatar

so now if a1 DOES equal one then it will stop looping and show the time right?

November 02, 2010
by Quinnifir
Quinnifir's Avatar

ok. i tried to compile it but it still said there was an else without a previous if.

November 02, 2010
by Quinnifir
Quinnifir's Avatar

oops that was a different if statement. oops. 0:

but THANK YOU! i fied the other one. im so close to finishing the program i can smell it.

November 02, 2010
by bretm
bretm's Avatar

I was just helping with the syntax errors. I was leaving the logic errors for you. ^_^

If a1 equals 1 when the loop starts, the value of the time variable will be written to the LCD over and over forever. This is because the "else" clause will run, the loop will loop again, a1 will still be equal to 1, the "else" will run again, the loop will loop again, a1 will still be equal to 1, etc. There's nothing inside the loop that changes the value of a1. (Unless a1 is declared as volatile and you're changing its value in an interrupt servicing routine.)

If a1 doesn't equal 1 when the loop starts, it will delay 10ms and then exit whatever function this loop is a part of because of the "return" statement. The flow chart for this loop, as written above is:

+--------------------+
| Does 2 equal zero? |<-------------+
+--------------------+              |
          | No                      |
          V                         |
+----------------+                  |
| Increment time |                  |
+----------------+                  |
          |                         |
          V                         |
+--------------------+      +----------------+
| Does a1 equal one? |----->| Write the time |
+--------------------+ Yes  +----------------+
       | No
       V
+-------------+
| Delay 10 ms |
+-------------+
       |
       V
+----------------------+
| Return from function |
| with a value of 2    |
+----------------------+
December 25, 2010
by Iflyatwin
Iflyatwin's Avatar

Hi everyone, and Merry Christmas. I want to write two commands on an if statement and I can't seem to get it wright. I would like to write if (time is grater than 50 and lower than 60 then execute statement. Thanks in advance to anyone that responds

December 25, 2010
by Ralphxyz
Ralphxyz's Avatar

What have you tried?

I hope this is correct I really am not a programmer.

if (time >50 && time <60) { }

Someone will jump all over if it is not correct.

Merry Christmas

Ralph

December 25, 2010
by Iflyatwin
Iflyatwin's Avatar

Thanks Ralph I tried it and it works. I was trying the same but with one & instead of two, no wonder it didn't work. Thanks again this really helps me finish my project

December 25, 2010
by mrobbins
(NerdKits Staff)

mrobbins's Avatar

Hi Iflyatwin,

Glad to hear that Ralph fixed your issue.

The difference between "&" and "&&" is that the first one is a "bitwise AND", and the second is a "logical AND". But more generally than that, my primary suggestion for you is to use lots of parentheses to make the order of evaluation clearer to the compiler, like this:

if ((time > 50) && (time < 60)) { }

That way, it's clear that you want it to do the comparisons (greater than, less than) first, and only then see if both are true. Otherwise, in some situations where you're building more complex expressions, the compiler might interpret them in a way that's quite different from what you expect.

Mike

December 28, 2010
by Hexorg
Hexorg's Avatar

Yes, in support of mrobbins' statement, I want to add that I had compilation problems with parenthesis in one version of gcc, and had no problems with the same code in another. It's better to "force" the order of execution using parenthesis, because the order might change from one version of gcc to the other.

Post a Reply

Please log in to post a reply.

Did you know that NerdKits has been featured on Slashdot, Hack A Day, Hacked Gadgets, the MAKE blog, and other DIY-oriented websites? Learn more...