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 |
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);
and when i compile, it says there is an else without a previous if. thank you, from quinnifir. |
---|---|
October 27, 2010 by Quinnifir |
oh sory about that ... new to these fourms |
October 27, 2010 by bretm |
A couple of problems. First, in C, a semicolon marks the end of a statement, so when you do
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:
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):
Now you can add an "else" clause, because the it's immediately preceded by an "if".
|
November 02, 2010 by Quinnifir |
so now if a1 DOES equal one then it will stop looping and show the time right? |
November 02, 2010 by Quinnifir |
ok. i tried to compile it but it still said there was an else without a previous if. |
November 02, 2010 by Quinnifir |
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 |
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:
|
December 25, 2010 by Iflyatwin |
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 |
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 |
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) |
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:
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 |
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. |
Please log in to post a reply.
Did you know that a flyback diode is important when driving a motor or any inductive load? Learn more...
|