General question

I don't understand why some books and reliable sources state one thing about procedure and Arduino seems to follow its on rules. For example ; (for i = 1) && (j =1) {do something}. This will not compile in the recommended format. Using the same state; (for i = 1&&j = 1) {do something} does compile. What are the rules for such statements? For instance do the same layouts follow for the if and while and other conditional statements? Also in all conditional statements which you would think; if i <= 1); {do something} will not work unless you drop the ; after the test code. What code should use the dropped ; mark? if i >- 1){ do something} being proper.

stx38834:
I don't understand why some books and reliable sources state one thing about procedure and Arduino seems to follow its on rules. For example ; (for i = 1) && (j =1) {do something}. This will not compile in the recommended format. Using the same state; (for i = 1&&j = 1) {do something} does compile. What are the rules for such statements? For instance do the same layouts follow for the if and while and other conditional statements? Also in all conditional statements which you would think; if i <= 1); {do something} will not work unless you drop the ; after the test code. What code should use the dropped ; mark? if i >- 1){ do something} being proper.

OMG! So many mistakes and misunderstandings in so few sentences!

You'd better start with the basics of C programming, before you do other things in your books.

One thing is, that "=" means "assign value to" and NOT "is equal to"!

So if you want to do a comparison to find out if i equals 1, then it would read like:

if (i==1) ...

Next thing is, that a single command ends with a semicolon ';'.

After you have understood the difference between "=" and "==", the syntax is always, that after "if" there must be a logical expression included in round brackets. And after the logical expression, there may either follow one single instruction, or one instruction block. The instruction block is included in curly brackets.

So you could either do:

if (i==1) i=2;

Line breaks do not matter, so that is exactly the same:

if (i==1) 
  i=2;

Or you may have a code block:

if (i==1) 
{
  i=2;
}

Within a code block there may be several to many single commands.

So what is this:

if (i <= 1);

That means: if (i<=1) is true, then execute a single "do-nothing-command".
If you write nothing as a command and end this nothing with a semicolon, this is the command: Do nothing at all. So

if (i <= 1);

means, that if i<=1 then do nothing at all.
Of course, the next valid command will be executed.
If it is a valid command that can be compiled to valid code.

Yes I errowed. if(i ==1)&&(j == 1){ do something} . This is what I find in references however it does not compile. if(i == 1&&j == 1){do something} does compile. What is the correct syntax for the conditiols. Thanks for the info on the other part of the question.

stx38834:
Yes I errowed. if(i ==1)&&(j == 1){ do something} . This is what I find in references however it does not compile. if(i == 1&&j == 1){do something} does compile. What is the correct syntax for the conditiols. Thanks for the info on the other part of the question.

First syntax is wrong.
The correct syntax for if is:

if (condition) command;

So:

if(i ==1)&&(j == 1)
         ^
         |
         ERROR

Where the '&' is there should either be a command (which starts with a letter) or a command block (which starts with an opening curly bracket '{'). As Delta_G already noted, you'd have to put the whole expression into surrounding brackets to be a valid logical expression.

I assume then that the statement if((if i == 1)&&(if j == 1)) (do something} would be correct then. Leaving off the ; of the )). how about the code following If(( i == 1)&&(j == 1)){a == 1; j == 2; k++;}.. Is this correct?

I write more errors than good code. Lot of laughs. Coming from machine code and basic to this C++ makes my head hurt. I'll get it or burst. Yes I accidently mis-typed If.....if.. Are the multiple statements ok on the same line as long as I use the ; between statements? I assume its fine as per the info above in the thread about the ;.

Thanks everyone very much. Maybe I can help you someday. I hope the syntax follows these rules, if so I'm ok with it. Just confusing the first time on a nested if especially. Thanks again.

stx38834:
Are the multiple statements ok on the same line as long as I use the ; between statements? I assume its fine as per the info above in the thread about the ;.

Yes, you can write as many commands in one line as you like.
But remember the different if-syntax if there is following a curly bracket after the condition or not.

Such code like:

if (i==1) a=2;b=3;c=4;

will use the condition only to decide whether "a=2;" is to be executed or not.
b=3; and c=4; will always be executed.

If you want more than one statement to execute, you'll have to use curly brackets like:

if (i==1) {a=2;b=3;c=4;}

In that case the condition decides whether the whole code block is to be executed.
But normally you use different formatting. Some programmers would write:

if (i==1) {
  a=2;
  b=3;
  c=4;
}

Others might prefer:

if (i==1) 
{
  a=2;
  b=3;
  c=4;
}

Typically C-Programmers do not write more than one command in one line.
But you can do, if you like.

Mr. Gammon has an excellent reference here, take a close look at it:

My thanks to Mr Gammon for the list of do's and dont's. Very necessary info. I wonder if theres a list of machine used function, declarations, etc. so that you don't pick a name for a variable that interferes with a used code name? Looks like that would be helpful also.

stx38834:
My thanks to Mr Gammon for the list of do's and dont's. Very necessary info. I wonder if theres a list of machine used function, declarations, etc. so that you don't pick a name for a variable that interferes with a used code name? Looks like that would be helpful also.

I don't know, probably somewhere. I have found that by mostly following best practices for naming, I have almost never (and I mean only a few times) ever had this happen to me.

Search your Arduino folders for "keyword", that's what you're after.

Search your Arduino folders for "keyword", that's what you're after.

stx38834:
I don't understand why some books and reliable sources state one thing about procedure and Arduino seems to follow its on rules. For example ; (for i = 1) && (j =1) {do something}. This will not compile in the recommended format. Using the same state; (for i = 1&&j = 1) {do something} does compile. What are the rules for such statements? For instance do the same layouts follow for the if and while and other conditional statements? Also in all conditional statements which you would think; if i <= 1); {do something} will not work unless you drop the ; after the test code. What code should use the dropped ; mark? if i >- 1){ do something} being proper.

In general, the problem is that people aren't learning to program in C++ - they are just cutting-and-pasting code. They haven't learned about lexical tokens, statements, expressions, block structure: all that basic gear. So the put semicolons after their for() statements and then wonder why they don't' work. It's because they don't know what the semicolons actually do.

The annoying thing is that these are not stupid people, and they would never suppose that you could just throw transistors at a breadboard and expect the thing to go. But C++ doesn't seem to have a single go-to place except the ANSI standard which is sixty goddamn dollars.

Speaking of which: "the rules" for C++ are here The Standard : Standard C++. The problem is that they are extremely technical and theres a huge amount of C++ that you just don't need on the arduino.

Another related problem is that there are features of C++ that are awful - destructor semantics for virtual inheritance is a black art. You don't need that stuff for arduino programming, but any complete description of the language must discuss it.

Personally, I learned C out of the K&R blue book back in '85, and learned about the new-fangled stuff in C++ when it came out. C by itself is mostly all you need for Arduino programming and I really recommend the old blue book. Mine is badly coffee-stained and you can't have it.

The equivalent for C++ is this one: Stroustrup: The C++ Programming Language (4th Edition) by the dude who actually invented C++.

Thank you guys for the good suggestions. I am mostly electronic oriented with a degree but learned machine language on my on starting in 1978. I quit doing programming in 1982 and missed a lot since then and had an urge to start again. I am used to or was used to the at2313 atmel and have an stkk500 which I did many projects with back in the 90's but only for a short while. I did eventually start programming the atmel in Bascom which was pretty nice and I wish could be implemented with the arduino's. I now find myself lost with c and c++ as I have never tackled it because of the syntax depth but I'm, thanks to you guys and a lot of looking at the reference, learning the code but its a slow process at age 62. I am not one to cut and paste, I have to understand whats going on rather than short cuts. It makes you prouder to create something that you actually wrote than a cut and paste. Also their just aren't examples of everything. I get quite confused when I do type in an example from the reference sketches and they don't work. It is hard to work with something that is not backwards compatible but fun non the less. If I can ever help you guys with an electronic problem maybe I can be of some help to you as well. I hate to post questions but sometimes you just cant learn something when the backwards compatibility is not there and you have no idea what theĀ  code was changed to. I worked for 24 hours because of one of those ; one day on a 4 line program. Its hard for someone just starting out to implement things of that nature when they follow their on syntax. I cant be pretty irritating I guess asking questions but I cant learn without doing so. Please bear with me and again thanks

Did you do that on purpose ?

@stx38834
All of this can be quite a bit of fun but I urge you to start slow, learn the basics, go through the examples that come with the IDE, if you have problems with them, ask for help here.