What is C++?
C++ is a middle-level programming language that was developed as an
extension of the C programming language.
an enhancement of the C language to provide support for OOPs (Object
Oriented Programming Systems) and additional features.
It is regarded as a "middle-level" language, as it comprises a combination of both
high-level and low-level language features.
Low level languages are closer to hardware resources and there is very less
abstraction layering in them while High level languages have higher level of
abstraction which makes low level languages more efficient and have more
control over hardware while high level languages have precise control and easy to
implement.
3.
Why C++ Programming?
C++ has the Standard Template
Library(STL) which is very useful as it helps in
writing code compactly and quickly as required.
C++ is extremely fast, since it is a quite low-
level language, compared to Java, Python, JS,
and some other common languages.
4.
First Program inC++
#include <iostream>
using namespace std;
int main()
{
cout << "Learning C++ with IEEE and
IET";
return 0;
}
The nameof the variable contains letters, digits, and underscores.
The name of the variable is case sensitive (ex Arr and arr both are
different variables).
The name of the variable does not contain any whitespace and special
characters (ex #,$,%,*, etc).
All the variable names must begin with a letter of the alphabet or an
underscore(_).
We cannot used C++ keyword(ex float,double,class)as a variable name
Rules For Declaring Variable
Scope of variablesin C++
The scope of a variable is defined as the extent of the program
code within which the variable can be accessed or declared or
worked with.
Types of variable scopes:
1.Local Variable
2.Global Variable
10.
#include<iostream>
using namespace std;
voidfunc()
{
int age=18;
}
int main()
{
cout<<"Age is: "<<age;
return 0;
}
#include<iostream>
using namespace std;
int global = 5;
void display()
{
cout<<global<<endl;
}
int main()
{
display();
global = 10;
display();
}
11.
What if thereexists a local variable with the
same name as that of global variable inside a
function?
#include<iostream>
using namespace std;
int global = 5;
int main()
{
int global = 2;
cout << global << endl;
}
12.
What if thereexists a local variable with the same
name as that of global variable inside a function?
#include<iostream>
using namespace std;
int global = 5;
int main()
{
int global = 2;
cout << global << endl;
}
compiler will give precedence to the local variable
13.
How to accessa global variable when
there is a local variable with same name?
#include<iostream>
using namespace std;
int x = 0;
int main()
{
int x = 10;
cout << "Value of global x is " << ::x;
cout<< "nValue of local x is " << x;
return 0;
}
Escape Sequence inC++
b Backspace
It is used to move the cursor one place
backward.
n New Line
It moves the cursor to the start of the
next line.
17.
Operators in C++
Anoperator is a symbol that operates on a value to perform specific
mathematical or logical computations.
18.
Name Symbol DescriptionExample
Increment Operator ++
Increases the integer value of the
variable by one
int a = 5;
a++; // returns 6
Decrement Operator --
Decreases the integer value of
the variable by one
int a = 5;
a--; // returns 4
A) Unary Operators:
19.
Name Symbol DescriptionExample
Addition + Adds two operands
int a = 3, b = 6;
int c = a+b; // c = 9
Subtraction –
Subtracts second operand from
the first
int a = 9, b = 6;
int c = a-b; // c = 3
Multiplication * Multiplies two operands
int a = 3, b = 6;
int c = a*b; // c = 18
Division /
Divides first operand by the
second operand
int a = 12, b = 6;
int c = a/b; // c = 2
Modulo Operation %
Returns the remainder an integer
division
int a = 8, b = 6;
int c = a%b; // c = 2
B) Binary Operators:
20.
Relational Operators
These operatorsare used for the comparison of the values of two operands.
Name Symbol Description Example
Is Equal To == Checks if both operands are equal
int a = 3, b = 6;
a==b;
// returns false
Greater Than >
Checks if first operand is greater than the
second operand
int a = 3, b = 6;
a>b;
// returns false
Greater Than or Equal To >=
Checks if first operand is greater than or equal
to the second operand
int a = 3, b = 6;
a>=b;
// returns false
Less Than <
Checks if first operand is lesser than the second
operand
int a = 3, b = 6;
a<b;
// returns true
Less Than or Equal To <=
Checks if first operand is lesser than or equal to
the second operand
int a = 3, b = 6;
a<=b;
// returns true
Not Equal To != Checks if both operands are not equal
int a = 3, b = 6;
a!=b;
// returns true
21.
Name Symbol DescriptionExample
Logical AND &&
Returns true only if all the
operands are true or non-
zero
int a = 3, b = 6;
a&&b;
// returns true
Logical OR ||
Returns true if either of the
operands is true or non-zero
int a = 3, b = 6;
a||b;
// returns true
Logical NOT !
Returns true if the operand is
false or zero
int a = 3;
!a;
// returns false
Logical Operators
22.
Name Symbol DescriptionExample
Assignment Operator = Assigns the value on the right to the variable on the left
int a = 2;
// a = 2
Add and Assignment Operator +=
First adds the current value of the variable on left to the
value on the right and then assigns the result to the
variable on the left
int a = 2, b = 4;
a+=b; // a = 6
Subtract and Assignment Operator -=
First subtracts the value on the right from the current
value of the variable on left and then assign the result to
the variable on the left
int a = 2, b = 4;
a-=b; // a = -2
Multiply and Assignment Operator *=
First multiplies the current value of the variable on left to
the value on the right and then assign the result to the
variable on the left
int a = 2, b = 4;
a*=b; // a = 8
Divide and Assignment Operator /=
First divides the current value of the variable on left by the
value on the right and then assign the result to the
variable on the left
int a = 4, b = 2;
a /=b; // a = 2
Assignment Operators
operators are used to assign value to a variable.
23.
#include <iostream>
using namespacestd;
int main()
{
int a = 3, b = 4;
// Conditional Operator
int result = (a < b) ? b : a;
cout << "The greatest number is " << result << endl;
return 0;
Ternary or Conditional Operators(?:)
This operator takes three operands, therefore it is known as a Ternary
Operator.
FOR LOOP
Syntax:
for(initializationexpr ; condition ; updation) {
//Body Of the Loop
}
Initialization: Initialize the loop counter to some value
Condition: If the condition evaluates to true then we will execute the body of loop
and go to update expression otherwise we will exit from the for loop
Updation: After executing loop body this expression increments/decrements the
loop variable by some value
29.
QUESTION TIME
What isthe output of the code?
a) 12345678910
b) 0123456789
c) Error
d) None of these
30.
WHILE LOOP
Syntax:
initializationexpression;
while (test_expression){
// statements
Update_expression;
}
Why While Loop?
In For Loop we have to know the number of iterations before
hand but in while loop the we only set the test expression which
controls the iterations.
31.
QUESTION TIME
What isthe output of the code?
a) Hello World (5 times)
b) Error
c) Infinite loop
d) None of these
32.
DO WHILE LOOP
Syntax:
initialization expression;
do{
//Loop Body
//updation expression
}while(test_condition) ;
Why do while Loop?
It is an exit controlled loop i.e we use the test_condition at the end of an iteration .
So the loop is run at least one time irrespective of the Condition Used.
33.
RANGE BASED FORLOOP
Syntax:
for(declaration : range){
//loop body
}
This Loop is used exclusively for ranges .
This kind of for loop iterates over all the elements in range, where
declaration declares some variable able to take the value of an element
in this range. Ranges are sequences of elements, including arrays,
containers, and any other type supporting the functions begin and end.
34.
JUMP STATEMENTS
What areJump Statements?
Jump statements allow altering the flow of a
program by performing jumps to specific
locations.
Jump Statements:
Break
Continue
Go To
35.
BREAK STATEMENT
break;
leavesa loop, even if the condition for its end is not fulfilled. It can be used to end an
infinite loop, or to force it to end before its natural end.
Flow:
CONTINUE STATEMENT
continue;
statementcauses the program to skip the rest of the loop in the current iteration, as if the
end of the statement block had been reached, causing it to jump to the start of the
following iteration.
Flow:
38.
QUESTION TIME
What isthe output of the code?
a) 012345678910
b) 0123456789
c) Error
d) Infinite loop
39.
GOTO STATEMENT
gotoallows to make an absolute jump to another point in the program.This
unconditional jump ignores nesting levels, and does not cause any automatic stack
unwinding.Therefore, it is a feature to use with care, and preferably within the
same block of statements, especially in the presence of local variables.
Syntax:
The destination point is identified by a label, which is then used as an argument for
the goto statement. A label is made of a valid identifier followed by a colon (:)
goto mylabel;
//code
mylabel:
*Note: Goto has no particular use case in modern c++ programs.