domingo, 10 de noviembre de 2013

Learning more about Variables and Assignments

In C++ variables are names for a memory locations.  Variables names are called identifiers.

How to declare a variable?

First, we tells the compiler the type of data to store.

  • For example: 
    • int number_of_bars;
      • int is a abbreviation for integer (3, 102, 3452, -123, etc.
    • double total_weight;
      • double represents numbers with a fractional component (1.0, 3.14, -564.3, etc.)
An assignment statement changes the value of a variable.
  • For example:
    • total_weight = one_weight + number_of_bars;
    • const age = 21
    • my_cost = your_cost
  • The '=' operator in C++ means the new value of a variable.
Arithmetic operators:
  • '+' - addiction
  • '-' - subtraction
  • '*' - multiplication
  • '/' - division
  • '%' - modulo: operation that give the remainder of a division of two value.
For example:
  • z = x + y;
  • c = a - b;
  • a = 11 % 3;
Compound assignment:

When we want to modify the value of a variable by performing an operation on the value currently stored in that variable we can use compound assignment operators:

  • The expression 'value += product;'  is equivalent to 'value = value + product;'
  • We can use expression like:
    • a -= b;
    • x -= y;
    • price *= unit;
Increase and decrease:

The increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. For example:
  • -= 1;
  • += 1;
  • ++c;
  • a = ++b;
Other operators:
  • '= =' - equal to
  • '! =' - not equal to
  • '>' - greater than
  • '<' - less than 
  • '>=' greater than or equal to
  • '<=' less than or equal to
Logical operators:
  • '!' - operator to perform the Boolean operation NOT, it has only one operand, located at its right, and the only thing that it does is to inverse the value of it, producing false if its operand is true and true if its operand is false.
  • The logical operators '&&' and '||' are used when evaluating two expressions to obtain a single relational result:
    • The operator '&&' corresponds with Boolean logical operation AND. This operation results true if both its two operands are true, and false otherwise.
    • The operator '||' corresponds with Boolean logical operation OR. This operation results true if either one of its two operands is true, thus being false only when both operands are false themselves.
&& OPERATOR
aba && b
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

|| OPERATOR
aba || b
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

For example:
  • ! (5 = = 5) 
  • while (ans == 'Y' && ans == 'y');
  • ( (5 == 5) || (2 > 8) )
Conditional Operator (?)

The conditional operator evaluates an expression returning a value if that expression is true and a different one if the expression is evaluated as false. Its format is: 
condition ? result1 : result2

If condition is true the expression will return result1, if it is not it will return result2.
For example:
  • 7 == 5 ? 4 : 3 // return 3, since 7 is not equal to 5.
  • 7 == 5+2 ? 4 : 3 //return 4, since 7 is equal to 5+2.

Comma Operator

The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the rightmost expression is considered.
For example:
  • a = (b=3, b+2);

No hay comentarios.:

Publicar un comentario