Dani 2,137 The Queen of DaniWeb Administrator Featured Poster Premium Member

Working With C++
C++ syntax is the way which the code is arranged to form the language. Each computer language has its own unique syntax. For example, the syntax for adding two numbers may be different in one language than in another. Syntax is to programming as grammar is to literature. For the most part, C++ code is divided into expressions, statements, and declarations. Each of the three use different syntax which tell the computer that they are to be handled differently.

Declaration Statements
Declaration statements are, simply put, used to declare variables. As in math, computer variables are letters (or words) used to represent variables. C++ variables may consist of any combination of letters, numbers, or symbols. Variable names are often used to represent what a variable does in a program. For example, the variable sum would most likely hold the value of a sum of numbers. Because declarations are a form of statements, they, too, must end in a semi-colon. We declare variables by first naming their type. Variable types include, but are not limited to, whole numbers (int), decimals (float or double), and characters (char). Once we name a variable's type, we leave a space and then name them. As an example, to declare a variable number as a whole number (an integer), we would say: int number; For the most part, all variables are declared together at the top of your program.

Expressions
Expressions are very similar to their mathematical counterparts. 5+7 is an expression, as is N+5, as is N itself, where N is a variable representing a number. In other words, they are simply numbers, characters, or words which the computer stores in RAM in the form of variables. Types of expressions include: numerical expressions, textual (ASCII) expressions, structural expressions, function calls, and boolean expressions.

Numerical expressions are simply numbers such as 28 or 3.14. Complex numerical expressions may consist of operators (+, -, *, /, etc.). For example, in the expression 3.14 / 2.6, the / symbol is used to represent division.

Operator Math Usage
+ addition
- subtraction
* multiplication
/ division
% modular (remainder) division
^ exponential power

Textual expressions, on the other hand, can be divided into their own two types: characters and strings.

Characters are single letters, numbers, or symbols such as 'a' or '$'. According to C++ syntax, they are enclosed in single quotes to denote that they are characters. A very important, yet subtle, concept is that the number 3 is a numerical expression while '3' with single quotes around it is a character. In C++, as well as other languages, there is a large distinction between numerical and textual expressions. Such are handled differently by the computer.

Strings, meanwhile, are sequences of characters such as "happy" or "123". Strings, which may include spaces, are enclosed in doule quotes. In C++, anything enclosed in double quotes, deemed a string, is to be taken literally. For example, S = "2+3"; actually assigns the string of characters, 2+3 to the variable S, without evaluating the sum. The single character \n may be added anywhere inside a string to denote a hard return. endl used along with cout can also be used. (For example, cout << endl;)

Statements
Statements put expressions together. They are used to tell the computer how to handle expressions. (In other words, what to do with them.)

Assignment statements are used to assign values to variables. The assignment statement N = 2 + 3; tells the computer to add 2 and 3, and store the sum, 5, in the variable N. Note the semi-colon after the 3. While all programming languages generally consist of statements, C++ code, specifically, ends all its statements in a semi-colon. In an assigngment statement, a variable must always be on the left (lvalue) while an expression you want the lvalue to be equal to must always be on the right (rvalue). Assignment statements act behind-the-scenes in the sense that a user running your program doesn't know what is happening.

Statement Valid?
x = 5a; no
2+1 = 3+0; no
60 = variable; no
3 = x+1; no
9/3 = 3; no
num+1 = num; no
x = 2+5; yes
letter = 9/4; yes
value = x; yes
x = x+2; yes

Not all statements, however, are assignment statements. Some statements tell a computer to print information to the screen for the user to see. The C++ output statement, for example, is cout<< while the input statement is cin>>. Output statements print information to the screen while input statements give the user a prompt asking the user to enter data via the keyboard. Note how the arrows in input and output statements are facing different directions. A simple C++ statement is cout<<"Hello World"; and will simply print the text Hello World to the user's screen.

Working With Statements


#include<iostream.h>

int main() {

int a, b;
cout << "Enter value: ";
cin >> a;
cout << "Enter value: ";
cin >> b;
cout << "Sum is: " << a + b;
return 0;

}


The above program is very simple in that it declares two variables as integers: a and b. It then asks the user to input the values of each variable from the keyboard. The program then prints the sum of the two numbers to the screen.


Statement Shortcuts
What makes C++ so streamlined is that there are additional tricks which can be used to perform many tasks. For example, say you had an accumulator variable acc, which keeps increasing its value throughout a program. Perhaps acc had a value of 5 and you wanted to increase this value by 5. The most direct way to do this would be to say acc = acc + 5; The expression acc + 5 would be evaulated as 5 + 5, or 10. This new value of 10 would then be thrown into the acc variable, overwriting the 5 that was previously there. Note that the value of each variable is being stored in RAM. Each time a call is made out to a variable, the computer must search through RAM to find the location of the variable and decide what to do with it. For example, one call is being made to RAM to determine that the accumulator variable is equal to 5. Another call is being made to set the value of the variable equal to 10. There is, however, an easier way to do this entire operation. In the C++ language, one can also say acc+=5; and it will mean the exact same thing, as well as only needing to access RAM once, instead of twice. In large programs with many variables, this is much more efficient.

In addition, shortcuts can be used to combine declaration and assignment statements into one. For example, suppose you want to create a variable X and set it equal to the value 1 right from the start. You could do this in two statements by saing int X; and then X = 1; However, it is also possible to combine these two statements into int X = 1; Note that rvalue expressions may consist of the lvalue variable or other variables, or combinations thereof. Please also note that one cannot work with a variable until it has been assigned. This is either through a hard-coded assignment statement, or by asking for user's input and setting what the user types in to the value of the variable. Variables are initially assigned garble values unless specifically coded otherwise.

Statement Shortcuts

x = x+10; AND x += 10;
x = x+1; AND x++;
a = a/b; AND a /= b;
num = num*2; AND num *= 2;
int a; a = 10; AND int a = 10;
int n; n = 3*5; AND int n = (3*5);