C++ Syntax

From TRCCompSci - AQA Computer Science
Revision as of 11:29, 13 June 2019 by Admin (talk | contribs) (Escape Characters)
Jump to: navigation, search

Comments

C++ implements comments by:

/* This is a comment */

/* C++ comments can also
   * span multiple lines
*/

// Single line comment also

Write to Console

Writing to the console is a little more complicated than other languages:

#include <iostream>
using namespace std;

main() {
   cout << "Hello World"; // prints Hello World
   
   return 0;
}

You can also use 'endl' to have a new line:

#include <iostream>
using namespace std;

int main() {
   cout << "Hello World" << endl;
   cout << "Hola Mundo"  << endl;
   
   return 0;
}

You can also concatenate using the '<<' symbols:

#include <iostream>
using namespace std;

main() {
   cout << "Hello World " << 0 <<endl; // prints Hello World followed by a 0
   
   return 0;
}

This method can be used to concatenate variable into your output (ie replace '0' with a variable name).

Escape Characters

These can also include escape characters in the string, eg '\n':

#include <iostream>
using namespace std;

main() {
   cout << "Hello\nWorld"; // prints Hello followed by a new line and then World
   
   return 0;
}

Here is a list of the escape characters available:

Escape sequence Meaning
\\ \ character
\' ' character
\" " character
\? ? character
\A Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ooo Octal number of one to three digits
\xhh . . . Hexadecimal number of one or more digits

Read from Console

C++ can read from the keyboard (ie Console.ReadLine() in C# or input() in Python), This uses the 'cin' command. The example is for will read an integer (notice the direction of the '>>' is swapped for inputs):

#include <iostream>
using namespace std;

int main ()
{
  int i;
  cout << "Please enter an integer value: ";
  cin >> i;
  cout << "The value you entered is " << i;
  return 0;
}

Strings are handled differently, because using 'cin' a space is considered to be terminating character. using this method you can only enter a single word. So you need to do this instead:

#include <iostream>
#include <string> //added this include for handling strings
using namespace std;

int main ()
{
  string mystr;
  cout << "What's your name? ";
  getline (cin, mystr); //getline is passed cin and the string to read in
  cout << "Hello " << mystr << endl;

  return 0;
}

Variables

Lua uses variables similar to python, you don't need to specify a data type:

Numbers

num = 42  -- All numbers are doubles.

Strings

s = 'walternate'  -- Immutable strings like Python.
t = "double-quotes are also fine"
u = [[ Double brackets
       start and end
       multi-line strings.]]
-- String concatenation uses the .. operator:
message = 'Winter is coming, ' .. line

Empty / Null

t = nil  -- Undefines t; Lua has garbage collection.

Global Variables

The default in Lua is that all variables are global (in python all variables are local):

  -- Variables are global by default.
  thisIsGlobal = 5  -- Camel case is common.

Local Variables

To specify a variable is only local, you need to include 'local' before the variable name:

  -- How to make a variable local:
  local line = io.read()  -- Reads next stdin line.

If Statement

C++ If statements are exactly the same as C#.

Operators

Within if statements and also within loops, the following relational and conditional operates exist (these are the same as C#):

Relational

Symbols Explanation
== Equal
!= Not equal
<= Less than or equal
>= Greater than or equal
< Less than
> Greater than

Conditional

Operator Description Example
&& Called Logical AND operator. If both the operands are non zero then condition becomes true. (A && B) is false
|| Called Logical OR Operator. If any of the two operands is non zero then condition becomes true. (A || B) is true.
! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is true.

Loops

Just like other languages Lua has the standard 3 types of loop. While will run 0 or more times (may never run), Repeat (do..while) will run at least once, For will run an exact number of times:

Each of the loop structures in C++ are exactly the same as in C#.

Functions

Declaring a function

You can declare a function using the command 'function', the parameters will be within the round brackets '()'. The use of 'local' is not really required:

local function add(first_number, second_number)
	print(first_number + second_number)
end

add(2, 3) -- calling a method

Returning a value

You can return a value from a function by using 'return'

local function add(first_number, second_number)
	return first_number + second_number
end

print(add(5, 6))