Showing posts with label Operator. Show all posts
Showing posts with label Operator. Show all posts

Sunday, July 20, 2014

Conditional Execution Using Operator (?:)

Conditional Execution Using Operator (?:)
C++ has an interesting and powerful operator called the conditional operator that is similar to a compacted if-else construct.
Syntax:
(conditional expression evaluated to bool) ? expression1 if true : expression2 if false;

Such an operator can be used in compactly evaluating the greater of two given numbers,
as seen here:
int a=8, b=12, max;
max = (a > b)? a : b; // max contains greater of a and b

Example:
#include <iostream>
using namespace std;
int main(){
    cout << “Enter two numbers: ” << endl;
    int a = 8, b = 0;
    cin >> a;
    cin >> b;
    int max = (a > b)? a : b;
    cout << “The greater of “ << a << “ and  "<< b << “ is: “ << max << endl;
    return 0;

    getch();
}
Output:
Enter two numbers: 
123
27
The greater of 123 and 27 is: 123 

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