Unit Ii
Unit Ii
UNIT-II
BASICS OF C++
Character Set
The character set is a combination of English language
comprising of the alphabets and the White spaces
and some symbols from the mathematics including
the Digits and the Special symbols. C++ character set
means the characters and the symbols that are
understandable and acceptable by the C++ Program.
These are grouped to create and give the commands,
expressions, words, c-statements, and some of the
other tokens for the C++ Language.
IDENTIFIERS
1. Constants
2. Variables
3. Functions
4. Labels
5. Defined data types
Some naming rules are common in both C and C++. They are as follows:
The identifier name cannot start with a digit, i.e., the first letter should be
alphabetical. After the first letter, we can use letters, digits, or underscores.
In C++, uppercase and lowercase letters are distinct. Therefore, we can say that C++
C does no support polymorphism, encapsulation, and C++ supports polymorphism, encapsulation, and
inheritance which means that C does not support inheritance because it is an object oriented
object oriented programming. programming language.
+ Addition
- Subtraction
* Multiplication
/ Division
Greater Than or
>= 3 >= 5 give us false
Equal To
Logical AND.
expression1 &&
&& True only if all the
expression2
operands are true.
Logical OR.
expression1 ||
|| True if at least one of the
expression2
operands is true.
Logical NOT.
! !expression True only if the operand is
false.
Bitwise Operators
Operator Description
| Binary OR
^ Binary XOR
represents memory
& # // address of num
address of the operand
int main()
{
int i=0;
return 0;
}
WHILE LOOP
initialization expression;
while (test_expression)
{
// statements update_expression;
}
Eg: while loop
#include <stdio.h>
int main()
{
// initialization expression
int i = 1;
// test expression
while (i < 6)
{
printf( "Hello World\n");
// update expression
i++;
}
return 0;
}
initialization expression;
do
{
// statements update_expression;
}
while (test_expression);
Eg. : do while
#include <stdio.h>
int main()
{
int i = 2; // Initialization expression
do
{
// loop body
printf( "Hello World\n");
// update expression
i++;
return 0;
}
Break Statement Example
#include <iostream>
int main() {
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
break;
}
cout<<i<<"\n";
}
}
Output:
1
2
3
4
COMMENTS
Comments can be singled-lined or multi-lined.
Single-line Comments
Example:
//This is a comment
cout << "Hello World!";
Multi-line Comments
Example
#include <iomanip>
#include <ios>
#include <iostream>
using namespace std;
int main()
{
// Using setw()
cout << "Setting the width"
<< " using setw to 5: \n"
<< setw(5);
cout << num << endl;
return 0;
}
Before setting the width: 50 Setting the width using setw to 5: 50
setprecision()
// Using setprecision()
cout << "Setting the precision using"
<< " setprecision to 5: \n"
<< setprecision(5);
// Using setprecision()
cout << "Setting the precision using"
<< " setprecision to 9 : \n "
<< setprecision(9);
return 0;
}
Before setting the precision: 3.14286
Setting the precision using setprecision to 5: 3.1429
Setting the precision using setprecision to 9: 3.14285714