Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

06
Dec

C++ Basics Tutorial - Lesson 2

Related Blog Items

Please refer Lesson 1..
2. Primitive Types and Operators
2.1 Modulus Operator %
2.2 Conversion Between Different Types in Calculation
2.3 Do Not Rely On the Precision of Float Numbers
2.4 Assignment Operators
2.5 Value of Assignment Expression
2.6 The Integer Value of Character
2.7 Logic Operators

2. Primitive Types and Operators

2.1 Modulus Operator %

It yields the remainder after integer division. This calculation has the same priority as manipulation and division.

2.2 Conversion Between Different Types in Calculation

In the calculation of different types of variables, C++ promotes the lower-level variable to higher-level variable, and the result is higher-level. For example, the result of int with float is float.

2.3 Do Not Rely On the Precision of Float Numbers

Floating-point numbers are represented only approximately in most computers. So do not compare floating-point values for equality. Instead, test whether the absolute value of the difference is less than a specified small value.

2.4 Assignment Operators

c += 7 c = c + 7
c -= 7 c = c - 7
c *= 7 c = c * 7
c /= 7 c = c / 7
c %= 7 c = c % 7
a = ++b b = b + 1, then a = b
a = b++ a = b, then b = b + 1
a = –b b = b - 1, then a = b
a = b– a = b, then b = b - 1

++b or b++ can either be used as a operand in a expression, or as a independent statement. Using assignment operator can save a bit of compiling and running time. It is useful when the statement is in a loop which will be run many times.

2.5 Value of Assignment Expression

Because = operator operates from right to left, such expression can be used: a=b=c=d=3. An assignment expression itself also has a value:

if( (a = cin.get( ) ) != ‘a’ )

2.6 The Integer Value of Character

The integer value of a character is its ASCII code, for example, 97 for a , 98 for b , 99 for c ,… To acquire this value, use variable type converting operator “static_cast <>” :

 
#include <iostream>
#include <iostream>
 
int main ()
{
  char keyboard;
  int a;
  do
  {
    cout << "Enter one character, and I will tell you its ASCII: \n \n";
    cin >> keyboard;
    cout << (a = static_cast <int> (keyboard)) << endl <<"\n \n \n \n";
   }while (a != 101 && a != 69);
 
   return 0;
}

2.7 Logic Operators

&&: AND

||: OR

!: Logic Negation, turning the value of a logic expression from true to false or from false to true.

Popularity: 10%

You need to log on to convert this article into PDF


Related Blog Items

2 Comments

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image