Posts

Conditional Statements in c++: if-else

              Conditional statements are the decision-making statements which makes the program to flow in a direction based on the inputs. In c++, the list is given below…. 1.       If-else 2.       Switch 3.       Ternary operator Each one is explained with an example. ‘if-else’:               The most commonly used conditional statement is ‘if-else’. It has the following syntax.. ‘if’ statement: If(condition) { ………………………………. } C++ Program: #include <iostream> using namespace std; int main() {     int a;     cout <<"Enter the number"<<endl;     cin >> a;          if (a>1)     {         cout<<"a is greater than 1...

6.Ternary operator and 7. Bitwise Operators

 6.Ternary operator:               It is a conditional operator(?:) which minimalize the conditional statements.it is easily inserted between code. Syntax: Symbol: ‘?:’ (condition)? statement: else statement; (x > y)? x: y; C++ program: which is greater one? #include <iostream> using namespace std; int main() {     int a, b;     cout<<"Enter the two numbers" <<endl;     cin>> a >> b;         // Using ternary operator     int g_val = (a > b) ? a : b;     cout << "The biggest of two numbers is: " << g_val;     return 0; } Output: Enter the two numbers 45 56 The biggest of two numbers is: 56 7. Bitwise Operators:               If you want to...

5. Unary Operators

     These operators need a single value to process the expression. The unary Operators are listed below. 1.    Increment operator (++) and Decrement operator(--): This operator deals with pre-increment(++Operand), post-increment(operand++), pre decrement(--Operand) and post decrement(Operand--).      Program: #include <iostream> using namespace std; int main() {     int x,y,z;     cout<<"Enter the number:";     cin>>x;     //pre increment     y = ++x;     //post incremnt     z = x++;     cout << "Pre increment value: " << y <<endl ;     cout << "Post increment value: " << x <<endl;     return 0; } Output: Enter the number:6 Pre increment value: 7 Post increment value: 8      Note ...

4.Relational Operators

               When you want to compare two values in terms of operands, this operator is used. List of relational   operators: ‘==’ – it checks that both operands are equal or not. ‘>’ – it checks the first operand is greater than the second operand. ‘<’ – it checks the first operand is less than or not. ‘>=’ – it checks for greater than or equal to ‘<=’ – it checks for less than or equal to ‘!=’ -it checks for not equal The c++ program example is given below. #include <iostream> using namespace std; int main() {     int x = 6, y = 4;     // checks for Equal operator     cout << "x == y is " << (x == y) << endl;     // checks for x is Greater than y     cout << "x > y is " << (x > y) << endl;     // checks for Greater than Equal     cout <...

3.Logical Operators

              Some symbols are used to find the logical values from the conditions. It returns the output as Boolean values either true or false. There are three logical operators in c++. Let us list out as follows… ‘&&’ – Logical AND Operator ‘||’ – Logical OR Operator ‘!’ – Logical NOT Operator Each and every logical operator is illustrated with examples…. ------------------------------------------------------------------------------------------------------------------------- Logical AND Operator(&&):               This operator returns true value when both of the operands are true. The truth table is given below. Operand1 Operand2 Output False False False False True False True False False True True True ...

2.Assignment Operators

               Assignment Operators are mainly used to assign the value. It uses the right to left assignment. The operators are listed below. ‘=’ – Assigns a value. ‘+=’ -Add the value and assign it. ‘-=’ – subtract the value and assign the value. ‘*=’ – multiply it and assign the value. ‘/=’ – divide and assign. Eg: x+=y means x=x+y; x-=y means x=x-y; x*=y means x =x*y; x/=y means x=x/y; A program displays the functionality of assignment operators #include <iostream> using namespace std;   int main() {     int x = 18, y = 3;       // Assignment Operator example.     cout << "x = " << x << endl;     cout << "y = " << y << endl;       //  Addition and Assignment Operator.     cout << "x += y is " << (x += y) << endl;       // Subtraction and ...

Operators in C++

              Operators are key components in any programming language. Operators can be classified into many types listed as follows…        Arithmetic Operators        Assignment Operators        Logical Operators         Relational Operators        Unary operators        Ternary Operators or Conditional Operators        Bitwise operators Each and every operators are detailed explained here. 1.Arithmetic Operators               As the name indicates, these are the operators for perform arithmetic expressions. It has ‘+’ for addition, ’-‘ for subtraction, ‘/’ for division, ‘*’ for multiplication and ‘%’ for mod operation. Each one is expressed in coding. C++ program to use Arithmetic operators:     ...