m3 Ppts
m3 Ppts
Ex:
integers (type int),
floating-point numbers (type float), and
character(type char).
character for character storage (1 byte). Rrepresent single characters
Example:
char a = 'A'; // char type
Declaring Variables
Int a;
Assignment statement
int a=10;
char a=‘b’;
float a =2.5;
Expressions
An expression is a combination of values, variables,
and operators.
C=a+2;
Operators and operands
+ is for addition.
– is for subtraction.
* is for multiplication.
/ is for division.
% is for modulo.
#include <iostream.h>
voidmain()
{
int num1 = 240;
int num2 = 40;
cout<<"num1 + num2: "<<(num1 + num2)<<endl;
cout<<"num1 - num2: "<<(num1 - num2)<<endl;
cout<<"num1 * num2: "<<(num1 * num2)<<endl;
cout<<"num1 / num2: "<<(num1 / num2)<<endl;
cout<<"num1 % num2: "<<(num1 % num2)<<endl;
return 0;
}
Output:
#include <iostream.h>
void main()
{
int num1 = 240;
int num2 = 40;
num2 = num1;
cout<<"= Output: "<<num2<<endl;
num2 += num1;
cout<<"+= Output: "<<num2<<endl;
num2 -= num1;
cout<<"-= Output: "<<num2<<endl;
num2 *= num1;
Output:
cout<<"*= Output: "<<num2<<endl;
num2 /= num1;
= Output: 240
cout<<"/= Output: "<<num2<<endl;
+= Output: 480
num2 %= num1;
-= Output: 240
cout<<"%= Output: "<<num2<<endl;
*= Output: 57600
}
/= Output: 240
%= Output: 0
3) Increment and decrement Operators
++ and —
#include <iostream.h>
void main()
{
int num1 = 240;
int num2 = 40;
num1++; num2--;
cout<<"num1++ is: "<<num1<<endl;
cout<<"num2-- is: "<<num2; Output:
}
num1++ is: 241
num2-- is: 39
4) Logical Operators
Logical Operators are used with binary variables. They are mainly used in
conditional statements and loops for evaluating a condition.
b1&&b2 will return true if both b1 and b2 are true else it would return
false.
b1||b2 will return false if both b1 and b2 are false else it would return
true.
!b1 would return the opposite of b1, that means it would be true if b1 is
false and it would return false if b1 is true.
Example of Logical Operators
#include <iostream.h>
void main()
{
bool b1 = true;
bool b2 = false;
cout<<"b1 && b2: "<<(b1&&b2)<<endl;
cout<<"b1 || b2: "<<(b1||b2)<<endl;
cout<<"!(b1 && b2): "<<!(b1&&b2);
}
Output:
b1 && b2: 0
b1 || b2: 1
!(b1 && b2): 1
5) Relational operators
We have six relational operators in C++: ==, !=, >, <, >=, <=
== returns true if both the left side and right side are equal
!= returns true if left side is not equal to the right side of
operator.
> returns true if left side is greater than right.
< returns true if left side is less than right side.
>= returns true if left side is greater than or equal to right side.
<= returns true if left side is less than or equal to right side.
6) Ternary Operator
Syntax:
If the expression results true then the first value before the
colon (:) is assigned to the variable num1 else the second
value is assigned to the num1.
Example of Ternary Operator/ Relational operator
#include <iostream.h>
void main()
{
int num1, num2;
num1 = 99;
num2 = (num1 == 10) ? 100: 200;
cout<<"num2: "<<num2<<endl;
num2 = (num1 == 99) ? 100: 200;
cout<<"num2: "<<num2;
}
Output:
num2: 200
num2: 100
Order of operations in C++
• When more than one operator appears in an expression, the order of evaluation
depends on the rules of precedence.
• C++ follows the same precedence rules for its mathematical operators that
mathematics does.
• Operators with the same precedence are evaluated from left to right.
Enumerated data types
• This is a user-defined data type having a finite set of enumeration
constants.
• The keyword 'enum' is used to create enumerated data type.
• Enumerated type declares a new type-name along with a sequence of
values containing identifiers which has values starting from 0 and
incrementing by 1 every time.
void main()
{
enum colour {red, blue, green, yellow};
colour background=green;
cout<<background;
}
cout - Output statements
Variable cout is predefined to denote an output
stream that goes to the standard output device
(display screen).
– if statement
– if else statement
– nested if statement
– if-else if statement
– switch statement
if statement
Syntax:
if(condition)
{
//Statements;
}
If Example:
#include <iostream.h>
void main ()
{
int num = 10;
if (num % 2 == 0)
{
cout<<"It is even number";
}
}
Output:
It is even number
If else statement
syntax:
if(condition)
{
//Statements
}
else
{
//Statements
}
If else Example:
#include <iostream.h>
void main ()
{
int num = 11;
if (num % 2 == 0)
{
cout<<"It is even number";
}
else
{
cout<<"It is odd number";
} Output:
}
It is odd number
Nested if statement
Syntax:
if(condition_1)
{
// Statement1;
if(condition_2)
{
// Statement2;
}
}
Nested if Example:
#include <iostream>
void main()
{
int num=90;
if( num < 100 )
{
cout<<"number is less than 100"<<endl;
if(num > 50)
{
cout<<"number is greater than 50";
}
}
Output:
}
number is less than 100
number is greater than 50
If - else if Statement
if-else-if statement is used when we need to check multiple conditions.
Syntax: if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
...
Else
{
//code to be executed if all the conditions are false
}
#include <iostream.h>
Syntax:
Output:
1
2
3
4
5
6
7
8
9
10
While loop
• while loop is used to iterate a part of the program several times.
If the number of iteration is not fixed, it is recommended to use
while loop than for loop.
Syntax:
initialize variable;
while(condition)
{
body of the loop;
increment/ decrement variable
}
#include <iostream.h>
void main()
{
int i=1;
while(i<=10)
{
cout<<i <<"\n";
i++;
}
}
Output:
1
2
3
4
5
6
7
8
9
10
Do-While Loop
• do-while loop is used to iterate a part of the program several
times. If the number of iteration is not fixed and you must have to
execute the loop at least once, it is recommended to use do-while
loop.
Syntax:
initialize variable;
do
{
body of the loop;
increment/ decrement variable
} while(condition);
#include <iostream.h>
void main()
{
int i=1;
do
{
cout<<i <<"\n";
i++;
} while(i<=10) ;
}
Output:
1
2
3
4
5
6
7
8
9
10
Jump statements
break
• The break statement is used to terminate loop or switch
statements. Control of the program flows to the statement
immediately after the body of the loop or switch.
Syntax: break;
#include <iostream.h>
void main()
{
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
break;
}
cout<<i<<"\t";
}
}
Output:
1 2 3 4
continue
The continue statement is used to skip the rest of the code
inside a loop for the current iteration only. Loop does not
terminate but continues on with the next iteration.
Syntax: continue;
#include <iostream.h>
void main()
{
for(int i=1;i<=10;i++)
{
if(i==5)
{
continue;
}
cout<<i<<"\t";
}
}
Output:
1 2 3 4 6 7 8 9 10
goto
The C++ goto statement is also known as jump statement. It is
used to transfer control to the other part of the program. It
unconditionally jumps to the specified label.
Syntax: exit(0);
#include <iostream.h>
#include<stdlib.h>
void main()
{
char choice;
cout<<"Do you want to study C++?(y/n)\n";
cin>>choice;
if (choice=='n')
{
exit(0);
}
else
{
cout<<"Welcome to c++!";
}
} Output:
Do you want to study C++?(y/n)
y
Welcome to c++!