Back Button
Back to Chapter
C++ Introduction
No items found.
C++ Basics
No items found.
C++ Control Statements
No items found.
C++ Logical Operators
No items found.
C++ Procedural Programming
No items found.
C++ Structural Programming
No items found.
C++ Implementation of OOPS
No items found.
C++ Arrays and Vectors
No items found.
C++ Error Handling
No items found.
C++ File Handling
No items found.
Collapse_Icon
Table of Contents

C++ is a language the computer understands, now what is the smallest fundamental unit of any language? Let's consider an example, English consists of words, punctuations, and grammar. Now words are further divided into nouns, pronouns, verbs, and so on.

Similarly, a token is a fundamental unit of C++ programming language, think of tokens as words.

In C++ there are 5 main tokens:-

Keywords

Keywords are pre-defined or reserved words in a programming language. Each Keyword has a reserved meaning. This means each keyword is like an instruction to the computer to perform a specific task. Consider the following example.

#include using namespace std; main() { int a=20; return 20; }

C++ automatically highlights the keywords you use in your code. In the above example include,using, namespace, int and return are all keywords.

All primitive data types are keywords. Loops, like for, while,, do while, and selection statements like if and else if are also keywords.

Identifiers

Identifiers are user-defined names which consist of a sequence of letter, digits and the underscore character.

Certain rules are to be followed while naming identifiers in C++

  1. The first character should always be a letter or an underscore.
  2. They must consist of only letters, digits, or underscore. No other special character is allowed.
  3. It should not be a keyword.
  4. The length of the identifier should be less than 31 characters. If an identifier-name exceeds this length, then only the first 31 significant characters are considered.

Example of valid identifiers:

int Return; void functionName(){ ......} float game; double _game; int la_st; int $game;

Examples of invalid identifiers:

int return; void break();//break is a keyword float 1num; // invalid number first character int last-name;//invalid special character

Constants

Constants refer to fixed values that are not altered throughout the entire program. These are declared using the keyword const .

const int num1;

Special Symbols

Special symbols like  [] () {}, ; * = # have special meaning and they cannot be used for other purposes. Let's find out what they mean.

  • Brackets [] : These are used as array element reference.
  • Parenthesis () : These are used to indicate function calls and function parameters.
  • Braces {}: The opening and ending curly braces mark the start and end of a block of code containing more than one executable statement.
  • Comma (,): It is used to separate more than one statement like for separating parameters in function calls or elements in an array.
  • Colon (:):It is an operator that essentially invokes something called an initialization list.
  • Semicolon (;): It is known as a statement terminator. It indicates the end of one logical entity.
  • Asterisk () : It is used to create a pointer variable.*
  • Assignment operator (=) :It is used to assign values.
  • Pre-processor (#): The preprocessor is a macro processor that is used automatically by the compiler to transform your program before the actual compilation.

Consider this line of code

int a[5]={10,20,0,30,60}; //[] to declare array variable //, to separate elements of an array //semi-colon to end a line in C++

Operators

Operators are symbols that when applied to C++ variables trigger an operation. The variables on which operators act upon are called operands. Operators can be divided into two broad categories based on the number of Binary and Unary operators.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/edee099f-0044-4215-be87-bc00b19eb33c/Untitled_Diagram_(33).png

Unary operators

Operators that act on a single operand are called Unary operators. These include increment and decrement operators.

#include using namespace std; main() { int a=10; a++; cout<

Binary operators

Operators that require two operands to act upon are called binary operators.

Binary operators are classified into :

Arithmetic operators:

  1. + addition
  2. - subtraction
  3. *  multiplication
  4. / division
  5. % modulus

Relational Operators

  1. expression 1>expression 2: this means to check if  expression 1 is greater than expression 2
  2. expression 1<expression 2: check if expression 1 is lesser than expression 2
  3. expression 1>=expression 2: check if expression 1 is greater than or equal to expression 2
  4. expression 1<=expression 2: expression 1 is lesser than or equal to expression 2,
  5. expression 1==expression 2 :check if expression 1 equals expression 2.
#include using namespace std; main() { int a=10; int b=20; bool A_B = a>b; cout<
Output: 0

Logical Operators

  1. AND &&  When all expression are true, only then the statement is true.
  2. OR || When any one expression, is true the entire statement is true.
  3. NOT**!** Simply reverses the value which means if the statement is true it turns it into false.
#include using namespace std; main() { int a=10; int b=20; int c=15; bool andValue= ab or cb :false or c
Output: AND value Or value NOT value 1 1 0

Assignment Operators: used to assign value to variables and constants.

int a =10; //assigns the value 10 to variable a

Conditional Operators: expression ? condition 1 : condition 2 ; if expression 1 is true perform condition 1 or else perform condition 2.

#include using namespace std; int main () { int a, b = 20; a = (b < 10) ? 30 : 40; cout << "value of a: " << a << endl; return 0; }
Output: value of a: 40