Showing posts with label Example. Show all posts
Showing posts with label Example. Show all posts
Sunday, June 29, 2014
Operators in C++
The numerical operators in C++ can be grouped into five types: arithmetic, assignment, comparison, logical and bitwise operators.
1. Arithmetic operators
Example:
x = 4 + 5; // x=9 // addition
x = 13 - 3; // x=10 // subtraction
x = 6 * 3; // x=18 // multiplication
x = 10 / 3; // x=3 // division
x = 10 % 3; // x=1 // modulus (division remainder)
2. Assignment operators
a. Assign a value to a variable
x=10;
b. Assign a variable to a variable
int a=10, b;
b=a;
c. Assign a expression to a variable
int a, b=5, c=6;
a=b+d;
3. Combined assignment operators
Example:
int x=10;
x += 5; // x = x+5 // x=15;
x -= 4; // x = x-4; //x=6;
x *= 3; // x = x*3; //x=30;
x /= 2; // x = x/2; //x=5;
x %= 3; // x = x%3; //x=1;
4. Operators to Increment (++) and Decrement (--)
Example:
int x=6;
x++; // x = x+1; //x=7;
x--; // x = x-1; //x=5;
Both of these can be used either before or after a variable.
x++; // post-increment
x--; // post-decrement
++x; // pre-increment
--x; // pre-decrement
The result on the variable is the same whichever is used. The difference is that the
post-operator returns the original value before it changes the variable, while the
pre-operator changes the variable first and then returns the value.
x = 5; y = x++; // y=5, x=6
x = 5; y = ++x; // y=6, x=6
5. Comparison operators
The comparison operators compare two values and return either true or false. They are mainly used to specify conditions, which are expressions that evaluate to either true or false.
Example:
bool x = (2 == 3); // false // equal to
x = (2 != 3); // true // not equal to
x = (2 > 3); // false // greater than
x = (2 < 3); // true // less than
x = (2 >= 3); // false // greater than or equal to
x = (2 <= 3); // true // less than or equal to
6. Logical operators
bool x = (true && false); // false // logical and
x = (true || false); // true // logical or
x = !(true); // false // logical not
7. Bitwise operators
int x = 5 & 4; // 101 & 100 = 100 (4) // and
x = 5 | 4; // 101 | 100 = 101 (5) // or
x = 5 ^ 4; // 101 ^ 100 = 001 (1) // xor
x = 4 << 1; // 100 << 1 =1000 (8) // left shift
x = 4 >> 1; // 100 >> 1 = 10 (2) // right shift
x = ~4; // ~00000100 = 11111011 (-5) // invert
The bitwise operators also have combined assignment operators.
int x=5; x &= 4; // 101 & 100 = 100 (4) // and
x=5; x |= 4; // 101 | 100 = 101 (5) // or
x=5; x ^= 4; // 101 ^ 100 = 001 (1) // xor
x=5; x <<= 1;// 101 << 1 =1010 (10)// left shift
x=5; x >>= 1;// 101 >> 1 = 10 (2) // right shift
Wednesday, June 11, 2014
Declare Constant and Use Constant in C++
Syntax:
#define <constant_name> <value>
Escape Sequences
#define <constant_name> <value>
or
const <data_type> <constant_name> = <value>
Declare:
#define
MAX 100
#define
PI 3.14
#define Newline '\n'
const
int MAX = 100;
const
float PI = 3.14;
const
char answer = ‘Y’;
Escape Sequences Represents
\a Bell (alert)
\b Backspace
\f Formfeed
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\' Single quotation mark
\" Double quotation mark
\\ Backslash
\? Literal question mark
Example 1: Calculate Area, Circumference of Circle using const keyword
#include <iostream>
#include <conio.h>
using namespace std;
const double Pi=3.14;
double Radius, Area, Circumference;
int main(){
cout<<"Input Radius of Circle: "; cin>>Radius;
Area = Pi * Radius * Radius;
Circumference = 2 * Pi * Radius;
cout<<"Area of Circle = "<<Area<<endl;
cout<<"Circumference of Circle = "<<Circumference;
getch();
return 0;
}
Example 2: Calculate Circle using #define keyword
#include <iostream>
#include <conio.h>
using namespace std;
#define PI 3.14159
#define NEWLINE '\n'
int main(){
double radius=7.0;
double circle;
circle = 2 * PI * radius;
cout <<"CIRCLE: " <<circle;
cout << NEWLINE;
getch();
return 0;
}
Monday, June 9, 2014
Examples: How to use variables in C++
Example 1: Integer Variables
Syntax: int variable; //comment
#include <iostream>
using namespace std;
int term; // term used in two expressions
int main(){
term = 4 * 7;
cout <<"Twice " << term << " is "<< 2*term <<endl;
cout <<"Three times " << term << " is " << 3*term <<endl;
return 0;
}
Example 2: Floating Point Variables
Syntax: float variable; //comment
#include <iostream>
#include <conio.h>
using namespace std;
int i; // an integer variable
float f; // a floating point number
int main(){
f = 1.0 / 2.0; // assign floating 0.5
cout<<" f= "<<f<<endl;
i = 1 / 3; // assign integer 0
cout<<" i= "<<i<<endl;
f = (1 / 2) + (1 / 2); // assign floating 0.0
cout<<" f= "<<f<<endl;
f = 3.0 / 2.0; // assign floating 1.5
cout<<" f= "<<f<<endl;
i = f; // assign integer 1
cout<<" i= "<<i<<endl;
return 0;
}
Example 3: Characters Variables
Syntax: char variable; //comment
#include <iostream>
#include <conio.h>
using namespace std;
char c1; // first character
char c2; // second character
char c3; // third character
int main(){
c1 = 'X';
c2 = 'Y';
c3 = 'Z';
cout << c1 << c2 << c3 << " reversed is "<<c3 << c2 << c1 << "\n";
return 0;
}
Subscribe to:
Comments (Atom)




