C++ Language Basics

C++ Statements
=
Assigns what is on the right side of the = to what is on the left. If a was 12, then a now becomes 25.
a = a + 13;
==
Asks whether what is on the right of the = is the same as what is on the left. If a is 25, then the statement is evaluated as false.
 (a == 56)
()
Groups code to clear up any ambiguities in the order in which they are evaluated.
Also encloses the parameters of a function call.
(3+6) * (-2-a) / 5
random(6);
{}
Groups larger blocks of code.
for (i = 0; i < 10; i++)
   {
      sum = sum + i;
   }
[]
An array subscript, the elements within a list or table.
x[23] = 6;
;
Terminates every statement.
x = x * x;

Shorthand Operators
++
Increment
i++;
is the same as
i = i + 1;
--
Decrement
i--;
is the same as
i = i - 1;
+=  
b += 100;
is the same as
b = b + 100;
-=  
c -= 10;
is the same as
c = c - 10;

switch statements
if/else commands
while loops
do-while loops
for loops
nested for loops
break statement
goto statement
continue statement
return statement
Operators in
order of Precedence
()
Function call
[]
Array subscript
->
Indirect component selector
.
Direct component selector
!
Logical negation
+
Plus
-
Minus
()
Expression parentheses
*
Multiply
/
Divide
%
Remainder (modulus divide)
<
Less than
<=
Less than or equal to
>
Greater than
>=
Greater than or equal to
==
Equal to
!=
Not equal to
&&
Logical and
||

Logical or

=
Assignment

switch statements enable multiple decision branches:
switch (year) {
   case 1: {
      Edit1->Text = "You are a Freshman.";
      break; 
   }
   case 2: {
      Edit1->Text = "You are a Sophomore.";
      break;
   } 
   case 3: {
      Edit1->Text = "You are a Junior.";
      break;
   } 
   case 4: {
      Edit1->Text = "You are a Senior.";
      break;
   } 
   default: {
      Edit1->Text = "Are you a Grad?";
   } 
}
Note: The "break;" statements transfer control below and outside the scope of the "switch."

if / else commands execute blocks of code only if the value in parentheses is true:
A single block of code may be executed:

if (gpa > 3.25) {
   Edit1->Text = "That's better than good.";
}

Any or all blocks of code may be executed:

if (zipCode == 90290) {
   Edit1->Text = "You live in Topanga.";
}
if (zipCode == 27708) {
   Edit1->Text = "You live near Duke.";
}
if (zipCode == 90077) {
   Edit1->Text = "You live in Beverly Glen.";
}
if (zipCode == 27278) {
   Edit1->Text = "You live in Hillsborough.";
}
if (sex == 0) {
   Edit2->Text = "You are female.";
}
if (age > 30) {
   Edit3->Text = "You can't be trusted.";
}

Only one of the two blocks of code will be executed:

if (hunger > 50) {
   searchForFood();
}
else {
   searchForMate();
}

Only one of the blocks of code will be executed:

if (hungerForChocolate > 10) { 
   goForChocolate();
}
else if (thirstForWater > 50) {
   goForWater();
}
else if (wakefulness < 70) {
   goForCoffee();
}
else if (thirstForWater < 30) {
   goForWater();
}
else {
   goHome();
}

while loops test a condition before entering the code block.
The code is never executed unless the condition is met.

while (runWayClear == true) {
   allowAircraftToLand();
}

You will never allow an aircraft to land as long as the runway is not clear.

do-while loops test a condition after leaving the code block.
The code is always executed once.

do {
   dance();
}
while (musicStopped == false);

You will always have one dance, even if the music has stopped.

for loops take three parameters enabling you to initialize, terminate and increment the loop counter:
Assuming you wished to do something with each agent from id 23 to 79,
setting first to 23 and last to 79 would do the job.
If you wanted every odd numbered agent from 23 to 79, then the last parameter should be id = id + 2:

for (int id = first; id <= last; id = id + 1) {
   // these agents increase in age
   agent[id].age ++;
}
nested for loops
allow you to cycle through all the cells in a 2d grid.
for (row = 0; row < 500; row ++) {
   for (column = 0; column < 500; column ++) {
      // make resource grow
      resource[row][column] ++;   
   }
}
// grow food in every cell in the world
The break statement causes an immediate exit from a switch or a loop:
for (student = 0; student < 500; student++) {
   if (score[student] == 99) break;
}
// the first student with a "99" score
// or student 500 will be shown
Edit1->Text = student;
The goto statement may be used to break out of a double or more deeply nested loop:
for (row = 0; row < 500; row ++) {
   for (column = 0; column < 500; column ++) {
      // look for row and column containing 77
      if (77 == cell[row][column]) {
         goto exit;
      }   
   }
}
exit:
// we have now found the row and column
// of the first cell containing "77"
The continue statement returns you to the loop's beginning,
skipping statements that follow it and incrementing the loop counter:
for (student = 0; student < 500; student++) {
   if (score[student] < 50) continue;
   score[student] = score[student] * 1.2;
   bonus[student] = true;
   bonusesAwarded++;
   notify(student);
   notify(professor);
}
// students with scores of "50" or 
// higher receive bonus points
The return statement causes an immediate exit from a function,
returning the value of "sum" as an integer :
int addTwoNumbers (int a, int b) {
   int sum = a + b;
   return sum;
}