Skip to main content

Posts

lvalue vs. rvalue in C++

In many situation we have to make a difference between expressions defining a  value that can be addressed (and changed), which are called lvalue and value  that can be only read, called rvalue. For example, the assignment operator  expect a lvalue on the left and a rvalue on the right. So far the only lvalue we have seen are variables. #include <iostream> int main(int argc, char **argv) { int i; i+3 = 5; // does not compile 45 = i; // does not compile } leads to the following compilation errors : /tmp/something.cc: In function ‘int main()’: /tmp/something.cc:4: non-lvalue in assignment /tmp/something.cc:5: non-lvalue in assignment

How to install Code Block 12.11 for C++ programs

Are you upgrading your code block for 10 to 12.11 here is a full setup video on how to setup code block 12.11 and running your first program

Role of compilation in C++

The compilation operation consists of translating a program written in a com plex and human-readable language like C++, C, PASCAL, ADA, etc. into assembler  code, which can be directly understood by the CPU. Practically, a compiler is itself a program which reads source files written by a human, for instance in C++, and generate object files which contains assembler code.

Debugging in C++

There are two kinds of errors you’ll run into when writing C++ programs:  compilation errors  runtime errors.  Compilation errors are problems raised by the compiler,generally resulting from violations of the syntax rules or misuse of types. These are often caused by typos and the like.  Runtime errors are problems that you only spot when you run the program: you did specify a legal program, but it doesn’t do what you wanted it to. These are usually more tricky to catch, since the compiler won’t tell you about them.

Understanding while and do-while

The while loop has a form similar to the if conditional: while(condition) { statement1 statement2 … } As long as condition holds, the block of statements will be repeatedly executed. If there is only one statement, the curly braces may be omitted.  while loop Here is an example : #include <iostream> using namespace std; int main() { int x = 0; while(x < 10) x = x + 1; 9 10 11 cout << “x is “ << x << “\n”; 12 13 } return 0; This program will print x is 10. The do-while loop is a variation that guarantees the block of statements will be executed at least once: Do while  do { statement1 statement2 … } while(condition); The block of statements is executed and then, if the condition holds, the program returns to the top of the block. Curly braces are always required. Also note the semicolon after the while condition.

Values and Statements

First, a few definitions: •   A statement is a unit of code that does something –a basic building block of a program. •  An expression is a statement that has a value – for instance, a number, a string, the sum of two numbers, etc. 4+2, x-1, and "Hello, world!\n" are all expressions. Not every statement is an expression. It makes no sense to talk about the value of an #include statement, for instance.

Line-By-Line Explanation

Lets understand each and every thing in detail :- 1.  // indicates that everything following it until the end of the line is a comment: it is ignored by the compiler. Another way to write a comment is to put it between /* and */ (e.g. x = 1 + /*sneaky comment here*/ 1;). A comment of this form may span multiple lines. Comments exist to explain non-obvious things going on in the code. Use them: document your code well! 2.  Lines beginning with # are preprocessor commands, which usually change what code is actually being compiled. #include tells the preprocessor to dump in the contents of another file, here the iostream file, which defines the procedures for input/output. 4.  int main() {...}defines the code that should execute when the program starts up. The curly braces represent grouping of multiple commands into a block. More about this syntax in the next few lectures. 5.  • cout << : This is the syntax for outputting some piece of text to the scree...