Contents  1-5  6-11  12-16  17-21  22-27   28-33  34 - 38  39-46  Projects   MFC

   Contact
   Search
   C
   C++
   Visual Basic
   Java
   JavaScript
   DHTML
   Style Sheets
   About
   Active X
   TDC Binding
   PHP
   Perl and CGI
   Flash
   XML
   SQL
   Messages
   Chat
   MCSE
   Linux
   Cabling   
   ActionScript
   Downloads
   E-Cards   
 
    
    

.

Note: Updated!  See top of introduction page c1.html for using a C++ .Net compiler for these standard C++ tutorials.

Statements, or "expressions" in C++, control the sequence of execution of an algorithm, evaluate an expression, or do
nothing at all (null statements).  A common and simple form of a statement is an assignment.  Example:
  x = a + b;
The assignment operator assigns whatever is on the right side to whatever is on the left side.

"The most beautiful thing we can experience is the mysterious. It is the source of all true art and all science. He to whom this emotion is a stranger, who can no longer pause to wonder and stand rapt in awe, is as good as dead: his eyes are closed." - Albert  Einstein

White space consists of all spaces, tabs, and indentations.  They are ignored by the compiler and only used to make programs easier to read.
In C++, compound statements must begin and close with braces { }, every statement in the compound statement must end with a semicolon.  Example:

 {
 
temp = a;
  a = b;
  b = temp;
}

Expressions - Anything that returns a value is an expression in C++.  Examples:  3.2   //returns the value 3.2 PI    //returns the value 3.14, SecondsPerMinute   //returns the value 60.  The expression  y = x = a + b;  is evaluated in the following order:  Add a to b.  Assign the result of the expression a + b to x.  Assign the result of the assignment expression x = a + b to y.  It is a mistake to confuse the assignment operator with what we normally associate as "equals" or "evaluates to".  Rather than =, this would be == in C++.  You can write x=5, but not 5=x.

#include <iostream.h>
int main()
{

    int a=0, b=0, x=0, y=35;
   
    cout << "a: " << a << " b: " << b; cout << " x: " << x << " y: " << y << endl;
   
    a = 9;
    b = 7;
    y = x = a+b;
   
    cout << "a: " << a << " b: " << b;
    cout << " x: " << x << " y: " << y << endl;

    return 0;

}



Output =
a:  0      b: 0     x: 0      y: 35
a:  9      b: 7     x: 16    y: 16


Operator - a symbol that causes the compiler to take an action.   
Assignment operator - (=) takes what is on the right ans stores it on the left.
lvalue - an operand that can legally be on the left side of an assignment operator.
rvalue - an operand that can legally be on the right side of an assignment operator.
Relational operators - used in comparisons.

There are 5 mathematical operators and 6 relational operators:

Mathematical Operators Relational Operators
+    addition ==    Equals  (evaluates to)
-    subtraction !=      Not Equals
*      multiplication >    Greater Than
/    division >=    Greater Than or Equals 
%     modulus <    Less Than
  <=    Less Than or Equals 

Modulus - Integers don't have fractions so in 21/4 the remainder of 1 is lopped off.   The value returned is only 5.  The modulus operator % returns the remainder value of integer division.  21 % 4 is 1, because 21/4 is 5 with a remainder of 1.  Example:

You want to print a statement on every 10th action.  Any number modulo 10 will return 0 if the number is a multiple of 10.  20 % 10 is 0,  30 % 10 is also 0.
  The modulus operator is also useful for telling whether numbers are even or odd.  Example:

#include <iostream.h>
int main()
{
//Ask for two numbers.  Assign the numbers to bigNumber and littleNumber. If bigNumber is bigger
//than littleNumber, see if they are evenly divisible. If they are, see if they are the same number

   int firstNumber, secondNumber;
   cout << "Enter two numbers.\nFirst: ";
   cin >> firstNumber;
   cout << "\nSecond: ";
   cin >> secondNumber;
   cout << "\n\n";

   if(firstNumber >= secondNumber)
   {
        //If there is no remainder left over after division, then they are evenly divisible.
      if( (firstNumber % secondNumber) == 0)
      {
         if(firstNumber == secondNumber){
             cout << "They are the same!\n"; }
         else {
             cout << "They are evenly divisible!\n"; }
      }
      else
         cout << "They are not evenly divisible!\n";
   }
   else {
     cout << "Hey! The second one is larger!\n"; }

  return 0;
}

If you have the variable myAge and you want to increase it by 2 you could wastefully write:

int myAge = 5;
int temp;
temp = myAge + 2;    
//add 5 + 2 and put it in temp
myAge = temp;        
//put it back into myAge

Or you could put the same variable on both sides of the assignment operator:

myAge = myAge + 2;

It is read as "Add 2 to the value myAge and assign it to myAge".  You could write it:

myAge += 2;

(+=) - the self assigned addition operator (plus equals) adds the rvalue to the lvalue and reassigns it to the lvalue. If myAge were 4 before it would now be 6.

Incrementing - increasing a value by 1.  The increment operator is  (++). 
Decrementing - decreasing a value by 1.  the decrement operator is (--).

C++;  is equivalent to C = C + 1;    or   C += 1; .

Prefix - (++myAge)   It is evaluated before the assignment, before end of statement semicolon.  Increment the value then fetch it.
Postfix - (myAge++)  It is evaluated after the assignment, after end of statement semicolon.  Fetch the value then increment the original.

In a simple statement it doesn't matter which is used.  In a complex statement it does.

x = 5;
int a = ++x;   
/
/means increment x, fetch the value which is 6, and assign it to a.  So a=6  and   x=6.
int b = x++;
     
//means fetch the value in x which is 6, assign it to b, then go back and increment x.  So b=6 but x=7.

Example:

#include <iostream.h>
int main()
{
  int myAge = 39;     
// initialize two integers
  int yourAge = 39;

  cout << "I am:\t" << myAge << "\tyears old.\n";
  cout << "You are:\t" << yourAge << "\tyears old\n";

  myAge++;        
// postfix increment
  ++yourAge;      
// prefix increment

  cout << "One year passes...\n";
  cout << "I am:\t" << myAge << "\tyears old.\n";
  cout << "You are:\t" << yourAge << "\tyears old\n";

  cout << "Another year passes\n";
  cout << "I am:\t" << myAge++ << "\tyears old.\n";  
//post-fix increment
  cout << "You are:\t" << ++yourAge << "\tyears old\n";  
//pre-fix increment
 
  cout << "Let's print it again.\n";
  cout << "I am:\t" << myAge << "\tyears old.\n";
  cout << "You are:\t" << yourAge << "\tyears old\n";

  return 0;
}

Output:

I am:               39 years old.
You are:         39 years old.
One year passes...
I am:               40 years old.
You are:         40 years old.
Another year passes...
I am:               40 years old.
You are:         41 years old.
Let's print it again.
I am:               41 years old.
You are:         41 years old.

Precedence - Every operator has a precedence value.  Multiplication has higher precedence than addition.  When 2 mathematical operators have the same precedence, they are performed in left to right order.  The expression x = 5 + 3 + 8 * 9 + 6 * 4;   is evaluated multiplication first, so the expression is:  x = 5 + 3 + 72 + 24; .

Use parentheses to change precedence order:
TotalSeconds = (NumMinutesToThink + NumMinutesToType) * 60.  For complex expressions you can nest parentheses one within another.  To compute the total number of people who are involved BEFORE multiplying seconds times the people, we could nest parentheses like:

TotalPersonSeconds = (    ( (NumMinutesToThink + NumMinutesToType)  *  60 )   *   (PeopleInTheOffice + PeopleOnVacation)    )

This expression is read from the inside out.  Simpler, you might write:

TotalMinutes = NumMinutesToThink + NumMinutesToType;
TotalSeconds = TotalMinutes * 60;
TotalPeople = PeopleInTheOffice + PeopleOnVacation;
TotalPersonSeconds = TotalPeople * TotalSeconds;

The nature of truth - In C++ the number 0 is considered false and all other values are considered true.  If an expression returns 0 it is false, and if it is false it returns 0.  Truth is represented by 1, though any value other than 0 that is returned can be used.

myAge = 39;
yourAge = 40;
myAge == yourAge;  
 //expression evaluates as false
myAge > yourAge;    
//expression evaluates as false


©2004 C. Germany