C++ – If Statement

C++ – If Statement

if (x == 100)
  cout << "x is 100";
...
if (x == 100) { cout << "x is "; cout << x; }
...
if (x == 100)
  cout << "x is 100";
else
  cout << "x is not 100";
...
if (x > 0)
  cout << "x is positive";
else if (x < 0)
  cout << "x is negative";
else
  cout << "x is 0";
...

Working sample:


#include <iostream>
using namespace std;

int main ()
{
int x = 3;

if (x > 0)
  cout << "x is positive";
else if (x < 0)
  cout << "x is negative";
else
  cout << "x is 0";
return 0;
}

By |C++, Video Games Development|Commenti disabilitati su C++ – If Statement

C++ – Basic Input Output

C++ – Basic Input Output

C++ uses a convenient abstraction called streams to perform input and output operations in sequential media such as the screen, the keyboard or a file.
The standard library defines:

cin standard input stream
cout standard output stream

Sample


// i/o example

#include <iostream>
using namespace std;

int main ()
{
  int i;
  // character output
  cout << "Please enter an integer value: ";
  // character input is stored in variable
  cin >> i;
  cout << "The value you entered is " << i;
  cout << " and its double is " << i*2 << ".\n";
  return 0;
}

The result is:
Please enter an integer value: 702
The value you entered is 702 and its double is 1404.

By |C++, Video Games Development|Commenti disabilitati su C++ – Basic Input Output

C++ – Operators

C++ – Operators

Assignment


// assignment operator
#include <iostream>
using namespace std;

int main ()
{
  int a, b;         // a:?,  b:?
  a = 10;           // a:10, b:?
  b = 4;            // a:10, b:4
  a = b;            // a:4,  b:4
  b = 7;            // a:4,  b:7

  cout << "a:";
  cout << a;
  cout << " b:";
  cout << b;
}

Arithmetic operators


...	
y = 2 + (x = 5);
...
x = 5;
y = 2 + x;
...
x = y = z = 5;
...

On C++ you can use:

+ addition
– subtraction
* multiplication
/ division
% modulo

Compound assignment


// compound assignment operators
#include <iostream>
using namespace std;

int main ()
{
  int a, b=3;
  a = b;
  a+=2;             // equivalent to a=a+2
  cout << a;
}

Increment and decrement

...
++x;
x+=1;
x=x+1;
...

Relational and comparison operators

...
(7 == 5)     // evaluates to false
(5 > 4)      // evaluates to true
(3 != 2)     // evaluates to true
(6 >= 6)     // evaluates to true
(5 < 5)      // evaluates to false
...
(a == 5)     // evaluates to false, since a is not equal to 5
(a*b >= c)   // evaluates to true, since (2*3 >= 6) is true
(b+4 > a*c)  // evaluates to false, since (3+4 > 2*6) is false
((b=2) == a) // evaluates to true 
...

Logical operators

...
!(5 == 5)   // evaluates to false because the expression at its right (5 == 5) is true
!(6 <= 4)   // evaluates to true because (6 <= 4) would be false
!true       // evaluates to false
!false      // evaluates to true 
...
( (5 == 5) && (3 > 6) )  // evaluates to false ( true && false )
( (5 == 5) || (3 > 6) )  // evaluates to true ( true || false )
...

Conditional ternary operator

...
7==5 ? 4 : 3     // evaluates to 3, since 7 is not equal to 5.
7==5+2 ? 4 : 3   // evaluates to 4, since 7 is equal to 5+2.
5>3 ? a : b      // evaluates to the value of a, since 5 is greater than 3.
a>b ? a : b      // evaluates to whichever is greater, a or b.  
...

Reference: http://www.cplusplus.com/doc/tutorial/operators/

By |C++, Video Games Development|Commenti disabilitati su C++ – Operators

C++ – Strings

C++ – Strings

Inizialization


...
string mystring = "This is a string";
string mystring ("This is a string");
string mystring {"This is a string"};
...

Basic Statement


// include standard input and output operations
#include <iostream>
// include strings
#include <string>
using namespace std;

int main ()
{
  string mystring;
  mystring = "This is the initial string content";
  // character output - variable - end line
  cout << mystring << endl;
  mystring = "This is a different string content";
  cout << mystring << endl;
  return 0;
}

Escape Codes

\n newline
\r carriage return
\t tab
\v vertical tab
\b backspace
\f form feed (page feed)
\a alert (beep)
\’ single quote (‘)
\” double quote (“)
\? question mark (?)
\\ backslash (\)

By |C++, Video Games Development|Commenti disabilitati su C++ – Strings

C++ – Variables and Datatypes

C++ – Variables and Datatypes

Variables let you store values dynamically.

Declare a variable

First you have to declare the variable type, the most used are:

Character types
– char Exactly one byte in size. At least 8 bits.
– char16_t Not smaller than char. At least 16 bits.
– char32_t Not smaller than char16_t. At least 32 bits.
– wchar_t Can represent the largest supported character set.

Integer types (signed)
– signed char Same size as char. At least 8 bits.
– signed short int Not smaller than char. At least 16 bits.
– signed int Not smaller than short. At least 16 bits.
– signed long int Not smaller than int. At least 32 bits.
– signed long long int Not smaller than long. At least 64 bits.

Integer types (unsigned) Same size as their signed counterparts
– unsigned char
– unsigned short int
– unsigned int
– unsigned long int
– unsigned long long int

Floating-point types
– float
– double Precision not less than float
– long double Precision not less than double

Boolean type
– bool

Void type
– void no storage

Null pointer
– decltype(nullptr)

Declaration of variables sample:


#include <iostream>
using namespace std;

int main ()
{
  // declaring variables:
  int a, b;
  int result;

  // process:
  a = 5;
  b = 2;
  a = a + 1;
  result = a - b;

  // print out the result:
  cout << result;

  // terminate the program:
  return 0;
}

Initialization

Initialization of variables, the initial value of a variable:


#include <iostream>
using namespace std;

int main ()
{
  // initialization of variables
  int a=5;               // initial value: 5
  int b(3);              // initial value: 3
  int c{2};              // initial value: 2
  int result;            // initial value undetermined

  a = a + b;
  result = a - c;
  cout << result; // the result is 6

  return 0;
}

Type deduction: auto and decltype


// auto
int foo = 0;
auto bar = foo;  // the same as: int bar = foo; 

//decltype
int foo = 0;
decltype(foo) bar;  // the same as: int bar; 

By |C++, Video Games Development|Commenti disabilitati su C++ – Variables and Datatypes