C++ Documentation

Comments

#include 
using namespace std;

main()
{
    /* comments can be one separate line */
   cout << "Hello World"; // the compiler will ignore this in-line comment
   /* C++ comments can  also
    * take up multiple lines
    * like this one.
    */
   return 0;
}

Printing

#include 

using namespace std;

int main()
{
   char str[] = "Hello World!";

   cout << "Programmers love to say : " << str << endl;
}

Comparison Operators

/* Comparison operators return booleans (true/false values) */
/* is x equal to y */
x == y
/* is x not equal to y */
x != y
/* is x greater than y */
x > y
/* is x greater than or equal to y */
x >= y
/* is x less than y */
x < y
/* is x less than or equal to y */
x <= y

If Statement

if (boolean_expression)
{
   // this will execute if the boolean expression is true
}
else
{
  // this will execute if the boolean expression is false
}

// example

#include 
using namespace std;

int main()
{
   // local variable declaration
   int a = 1;

   // check boolean condition
   if (a < 100)
   {
       // if condition is true then print the next line
       cout << "a is less than 100;" << endl;
   }
   else
   {
       // if condition is false then print the next line
       cout << "a is not less than 100;" << endl;
   }
   cout << "a is equal to: " << a << endl;

   return 0;
}

Math

/* Remember to include the following line at the top of your program */
#include <cmath>

/* Operators: */
+   Addition
-   Subtraction
*   Multiplication
/   Division
%   Modulus (Remainder)


/* Examples: */
int z = x + y;
int w = x * y;

/* Increment (add one) */
x++

/* Decrement (subtract one) */
x--

/* Absolute value */
double value = double abs(double x)

/* Square Root */
double sqrt = double sqrt(double x)


/* Rounding */
int rounded = round(5.86) round()


/* Example */
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    // round
    cout << "round(+3.14) = " << round(3.14)
}
// will print out "round(+3.14) = 3"

Arrays

/* syntax to declare an array */
type arrayName [ arraySize ];

/* declare a 7-element array called weights */
double weights[7];

/* initialize array */
double weights[5] = {10.0, 200.0, 45.4, 70.0, 99.0};

/* assign 4th elements of the ages array to be 33.0 */
weights[3] = 33.0;

/* index into array to access the third element */
double third = weights[2];

While Loop

/* while loop syntax */
while (condition) {
   statement(s);
}

/* while loop example */
#include <iostream>
using namespace std;

int main()
{
   int a = 1;

   // while loop execution
   while (a < 100)
   {
       cout << "a is still less than 100! a is: " << a << endl;
       a++;
   }

   return 0;
}

For Loop

/* for loop syntax */
for (initialize; condition; increment) {
   statement(s);
}

/* for loop example */
#include <iostream>
using namespace std;

int main()
{
   // for loop execution
   for (int a = 1; a < 100; a++)
   {
       cout << "value of a: " << a << endl;
   }

   return 0;
}

Function

#include <iostream>
using namespace std;

// function declaration
int min(int num1, int num2);

int main()
{
   // local variable declaration
   int a = 100;
   int b = 200;
   int minimum;

   // calling the min function
   minimum = min(a, b);

   cout << "The minimum value is: " << minimum << endl;

   return 0;
}

// function returning the minimum of two numbers
int min(int num1, int num2)
{
   // local variable declaration
   int result;

   if (num1 < num2) {
      result = num1;
   }
   else {
      result = num2;
   }

   return result;
}