Quote: Firstly, on page 70, it states: "An operator is a symbol that causes the compiler to take an action". The compiler, with the linker, convert source code to an executable program. Surely mathematical operators work within the executable? If the source code says "a = b + 2;" or "a = b - 2;", that happens when the executable runs, rather than having an effect on the compiler? Aren't the compiler and linker acting as an "interpreter" to translate (for instance) French to Russian?
|
I'm a bit confused by this. What are you asking exactly? I cant figure out which part of the above is a statement and which part you're confused about. If I go strictly by ? marks, then the answers would be something like this.
When you build your executable all that remains are 1's and 0's. There are no equal signs, subtraction signs, etc...The 1's and 0's represent instructions for the processor to do things, such as move a value into a registry, move a value from one place in memory to another, and to take two values in the current registries, add them together, and output the results to a third registry. This process of working with registries, and performing mathematical calculations is at the core of everything a program does.
Operators in your C++ code are usually converted directly in to "Opcodes" or processor instructions. 'Add' for example has a matching processor instruction which has a matching Assembly instruction ADD, which happens to have an opcode of somewhere between 00 and 08. Whenever your compiler/linker encounters the + operator, it compiles it into the appropriate opcodes to instruct your processor to add values together.
Compilers don’t translate French to Russian. They 'compile' C++ into binary instructions which your processor understands. This is more than just a strict copy/paste of values, it involves symbolic matching, creation of opcodes, and addressing of memory locations within the instructions.
Originally people programmed in 1's and 0's. By 1's and 0's we really mean opcodes. Values which the processor recognized as being symbolic for an instruction and arguments. After which we decided there wasn’t any good formatting and the opcodes were all difficult to remember. So assembly was created. Assembly instructions are really just aliases for opcodes and the assembler takes your .asm file and "assembles" it. There is still a conversion into binary, but its not as complicated as the leap from C++ to binary.
Finally, we created "high level" languages such as C++ to make the code more 'readable' by humans. If I didn’t answer your question, can you re-post with a clarification. I really am finding it difficult to determine what you're asking.
Quote: Secondly, an "if statement" is of the form:
if (expression) statement;
and if there are several lines in the statement, it is replaced by a block bounded by {}. I don't think that there should be a semi-colon after the } so which is correct, the example on line 40 of Listing 4.4 or at the end of the grey section on page 83? Are they both correct and, if so, under what circumstances is one or the other used?
|
Technically, an 'if statement' is either of the two formats:
if( expression) statement;
OR
if( expression ) { <statements> }
However, the ';' is the character identifying the end of a statement, so putting it in at the end of a {} block does not constitute a compiler error. That is, the following is perfectly acceptable, though non-standard, and not common syntax:
if( expression ) { <statements> };
In other words, the grey section on page 83 is probably a "typo," but is still legal syntax.
Cheers!