Chapter 2:
Programming Languages Syntax
What is C++?
C++ is a cross-platformed language that can be used to create
sophisticated high-performance applications.
C++ was developed by Bjarne Stroustrup at Bell labs in 1979,
as an extension to the C language.
C++ gives programmers a high level of control over system
resources and memory.
Why Use C++
C++ is one of the world's most popular programming languages.
C++ can be found in today's operating systems, Graphical User Interfaces, and
embedded systems.
C++ is an object oriented language which gives a clear structure to programs and
allows code to be reused, lowering development costs.
C++ is portable and can be used to develop applications that can be adapted to
multiple platforms.
C++ is fun and easy to learn!
C++ Quickstart
Let's create our first C++ file.
Open Codeblocks and go to File > New > Empty File.
Write the following C++ code and save the file as myfirstprogram.cpp (File > Save File as):
myfirstprogram.cpp
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
C++ Syntax
Let's break up the following code to understand it better:
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
Example explained
Line 1: #include <iostream> is a header file library that lets us work with
input and output objects, such as cout (used in line 5). Header files add
functionality to C++ programs.
Line 2: using namespace std means that we can use names for objects and
variables from the standard library.
Line 3: A blank line. C++ ignores white space.
Line 4: Another thing that always appear in a C++ program, is int main().
This is called a function. Any code inside its curly brackets {} will be
executed.
Line 5: cout (pronounced "see-out") is an object used to output/print text. In
our example it will output "Hello World".
Example explained
Note: Every C++ statement ends with a semicolon ;.
Note: The body of int main() could also been written as:
int main () { cout << "Hello World! "; return 0; }
Remember: The compiler ignores white spaces. However,
multiple lines makes the code more readable.
Line 6: return 0 ends the main function.
Omitting Namespace
You might see some C++ programs that runs without the standard
namespace library. The using namespace std line can be omitted
and replaced with the std keyword, followed by the :: operator for
some objects:
Example
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
C++ Data Types
As explained in the Variables chapter, a variable in C++ must
be a specified data type:
Example
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
double myDoubleNum = 9.9888; // Floating point number
char myLetter = 'D'; // Character
bool myBoolean = true; // Boolean
string myText = "Hello"; // String
Basic Data Types
The data type specifies the size and type of information the
variable will store:
INT
Use int when you need to store a whole number without
decimals, like 35 or 1000, and float or double when you need a
floating point number (with decimals), like 9.99 or 3.14515.
Example:
int myNum = 1000;
cout << myNum;
float
#include <iostream>
using namespace std;
int main () {
float myNum = 5.75;
cout << myNum;
return 0;
}
double
#include <iostream>
using namespace std;
int main () {
double myNum = 19.9999;
cout << myNum;
return 0;
}
float vs. double
The precision of a floating point value indicates how many
digits the value can have after the decimal point.
The precision of float is only six or seven decimal digits, while
double variables have a precision of about 15 digits.
Therefore it is safer to use double for most calculations.
Scientific Numbers
A floating point number can also be a scientific number with an "e" to indicate the
power of 10:
#include <iostream>
using namespace std;
int main () {
float f1 = 35e3;
double d1 = 12E4;
cout << f1 << "\n";
cout << d1;
return 0;
}
Booleans
A boolean data type is declared with the bool keyword and
can only take the values true or false. When the value is
returned, true = 1 and false = 0.
Example
bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun; // Outputs 1 (true)
cout << isFishTasty; // Outputs 0 (false)
Characters
The char data type is used to store a single character. The character must be
surrounded by single quotes, like 'A' or 'c':
#include <iostream>
using namespace std;
int main () {
char myGrade = 'B';
cout << myGrade;
return 0;
}
Example
Alternatively, you can use ASCII values to display certain characters:
#include <iostream>
using namespace std;
int main () {
char a = 65, b = 66, c = 67;
cout << a;
cout << b;
cout << c;
return 0;
}
Strings
The string type is used to store a sequence of characters
(text). This is not a built-in type, but it behaves like one in its
most basic usage. String values must be surrounded by
double quotes:
Example
string greeting = "Hello";
cout << greeting;
Example
To use strings, you must include an additional header file in the source code, the
<string> library:
#include <iostream>
#include <string>
using namespace std;
int main() {
string greeting = "Hello";
cout << greeting;
return 0;
}
Operators Precedence in C++
Operator precedence determines the grouping of terms in an
expression.
The associativity of an operator is a property that determines
how operators of the same precedence are grouped in the
absence of parentheses.
This affects how an expression is evaluated. Certain operators
have higher precedence than others; for example, the
multiplication operator has higher precedence than the
addition operator:
Operators Precedence in C++
For example x = 7 + 3 * 2; here, x is assigned 13, not 20
because operator * has higher precedence than +, so it first
gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the
top of the table, those with the lowest appear at the bottom.
Within an expression, higher precedence operators will be
evaluated first.
Operators Precedence in C++
C++ Increment and Decrement Operators
The increment operator ++ adds 1 to its operand, and the
decrement operator -- subtracts 1 from its operand. Thus,
x = x+1;
is the same as
x++;
C++ Increment and Decrement Operators
And similarly
x = x-1;
is the same as
x--;
C++ Increment and Decrement Operators
Both the increment and decrement operators can either precede (prefix) or
follow (postfix) the operand. For example:
x = x+1;
can be written as
++x; // prefix form
or as :
x++; // postfix form
C++ Increment and Decrement Operators
When an increment or decrement is used as part of an
expression, there is an important difference in prefix and
postfix forms.
If you are using prefix form then increment or decrement will
be done before rest of the expression, and if you are using
postfix form, then increment or decrement will be done after
the complete expression is evaluated
Example
#include <iostream>
using namespace std;
main() {
int a = 21;
int c ;
// Value of a will not be increased before assignment.
c = a++;
cout << "Line 1 - Value of a++ is :" << c << endl ;
// After expression value of a is increased
cout << "Line 2 - Value of a is :" << a << endl ;
// Value of a will be increased before assignment.
c = ++a;
cout << "Line 3 - Value of ++a is :" << c << endl ;
return 0;
}
Example
When the above code is compiled and executed, it produces
the following result:
Line 1 - Value of a++ is :21
Line 2 - Value of a is :22
Line 3 - Value of ++a is :23
Assignment Operator
Basically, assignment operator is used to assign the value of
one variable to another variable.
Or assign value to a variable.
Here is the general form to assign variable's value or value to a
variable
a = b;
x = 10;
Assignment Operator
In the above statements, the value of b is assigned to a, and the value 10 is assigned to x. Here is an example
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b=10, x;
a = b;
x = 10;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"x = "<<x<<endl;
}
Assignment Statement
As you know, an expression is composed of one or more operations. An expression
terminated ; (semicolon) becomes a statement.
Statements from the smallest executable unit within a C++ program. Statements are
terminated with a semicolon.
where a is a variable to whom the value is being assigned and cve can either be a constant
or variable or an expression. Following are some examples of assignment statements :
x = 16;
y = 3.6;
z = x + y;
z = z × x;
a = b + c - d;
Assignments can be chained together.
The assignment operator = returns the value of the
assignment as well as actually assigning the value to the left
hand operand.
Because of that, assignments can be chained together.
This can be useful when assigning the same value to a number
of items. For example,
x = y = z = 13
C++ Operators
Operators are used to perform operations on variables and
values.
The value is called an operand, while the operation (to be
performed between the two operands) is defined by an
operator:
C++ Operators
In the example below, the numbers 100 and 50 are operands,
and the + sign is an operator:
#include <iostream>
using namespace std;
int main() {
int x = 100 + 50;
cout << x;
return 0;
}
C++ Operators
Although the + operator is often used to add together two values, like in the example above,
it can also be used to add together a variable and a value, or a variable and another variable:
#include <iostream>
using namespace std;
int main() {
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
cout << sum1 << "\n";
cout << sum2 << "\n";
cout << sum3;
return 0;
}
C++ Operators
C++ divides the operators into the following
groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
Arithmetic Operators
Arithmetic operators are used to perform
common mathematical operations.
C++ Assignment Operators
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to assign the value 10 to
a variable call x:
#include <iostream>
using namespace std;
int main() {
int x = 10;
cout << x;
return 0;
}
C++ Assignment Operators
#include <iostream>
using namespace std;
int main() {
int x = 10;
x += 5;
cout << x;
return 0;
}
C++ Assignment Operators
A list of all assignment operators:
C++ Comparison Operators
Comparison operators are used to compare two values:
C++ Logical Operators
Logical operators are used to determine the logic between
variables or values:
Thank you