Showing posts with label c operators. Show all posts
Showing posts with label c operators. Show all posts

Sunday 6 October 2013

Logical Operators

 Logical Operators:

C++ provides us the facility of combining logical expression and evaluate their behavior. So, in this C tutorial for beginner, we are going to discuss  logical operators in c. We are going to discuss how to form and evaluate logical expressions and see how they work.

Below are three logical operators in C++ that we normally use:

Operator        Description

  1. !                    not
  2. &&                and
  3. ||                    or

Logical operators in c take two logical values, combine them and finally gives one logical value.

Expression            !expression

True (nonzero)            false (0)
False (0)                     true (1)

When you put not “ ! ” operator with expression it changes
  1. True to false
  2. False to true
  3. Nonzero to zero
  4. Zero to one

Let’s see some examples of “not” operator:

  1. ! (‘B’ > ‘A’)          !(true)             false
  2. !( 1 <= 10 )        !(true)             false
  3. !(1 != 1)            !(false)             true

Let’s see some examples of “and” operator:

  1. ( 100 >= 101 && 100 <= 101)                          false
  2. ( “shapes” == “shapes” && “me” == “me”)        true
  3. (!false && true)                                                true
  4. (1>0 && 1<0)                                                   false

Let’s see some examples of “or” operator:

  1. ( 100 >= 101|| 100 == 101)                             false
  2. ( “shapes” > “shapes” || “me” == “me”)            true
  3. (!false || !true)                                                true
  4. (1==0 || 1<0)                                                  false


Final words:

Logical operators are important in C++ programming. They have been discussed in detail in this c tutorial for beginners.
We hope that this post will be help full for our viewers.

Relational Operators in C++

Relational Operators In C++:

To make decisions in a C++ program, you must be able to compare conditions. So, in this C tutorial for beginners we’re going to discuss relational c operators. For example, if we want to see who has the highest, average and lowest marks in some class then we need to develop such algorithm which would require comparing marks of students so, we are going to need relational c operators for that.

In C++, a condition is represented by a logical expression. It would be either true or false. And true and false are Boolean values.

Consider:

            i>j        // i and j have some int values stored in them
It is a logical expression so, it would either have a true value or a false. “ > ” is a relational c operator. And relational operators allow you to make comparisons in a C++ program.
C++ includes six relational operators.

Operator                Description

  1. ==                      equal to
  2. !=                       not equal to
  3. <                        less than
  4. >                        greater than
  5. <=                      less than or equal to
  6. >=                      greater than or equal to

Note:

 “ ==  “ and “ =  “ are different signs. “ ==  “ is a double equal sign known as equality operator ad used to check equality of operands whereas “ = “ is known as assignment operator.

Expression            Result

  1. 1 < 10                true
  2. 2 == 2                true
  3. 4 > 11                 false
  4. 6.1 <= 7.9           true
  5. 5.8 >= 7.3           false

Relational operators in c can also be used to compare char values.

For example:

‘R’ > ‘T’            False
‘+’ < ‘*’             False
‘A’ <= ‘a’           False

Relational operators in c can also be used to compare strings.
  • string str1 = “Hello”
  • string str2 = “Hi”
  • string str3 = “Hello”
  • string str4 = “Air”

  • str1 < str2         true
  • str1 == str3       true
  • str1 > str 2        false
  • str < “An”          true

Final words:

 Relational operators are very important in C++ programming. They are the fundamental tools for comparing different things. In this C tutorial for beginners they have been discussed in detail with examples.

Saturday 28 September 2013

Operators And Their Precedence In C++

Operators:

Precedence of operators in c++ is the most basic thing used in computer for calculations,these calculations can be done by use of several arithmetic operators. So, we are going to discuss arithmetic operators in this c tutorial for beginners. There are five arithmetic operators in C++. Those c++ operators are listed below:

  1. +  (Addition)
  2. –  (Subtraction)
  3. *  (Multiplication)
  4. /  (Division)
  5. %  (Mod, modulus or remainder)


You can use these operator in both integral and floating number data types except modulus operator. As modulus operator is used in integral data type to find the remainder. 


Let’s assume an expression:

x + 2 * 5 + 6 / y

x and y are unknown variables. This arithmetic expression is formed by using operators and numbers. The numbers in the expression are called operands. 


C++ has two type of operators:

Unary operator: An operator that has only one operands.
Binary operator: An operator that has two operands.

Expression1: 3+4
In this expression, ‘+’ is a binary operator as ‘+’ has two operands.
Expression2: -3, +4
In this expression, ‘-‘ and ‘+’ are unary operators as they have only one operand. The ‘-‘ states that 3 is a negative number and ‘+’ indicates that 4 is a positive number. 


Examples:

Expression Result

  2 + 10 =  12
21 + 89  = 110
30 -  2010
30 -  90  =  -60
2   *  20 =  40
5   /   2  =   2
34 % 5  =   4
4  &   6  =   4


Note:

You should be careful in taking mod of numbers. For example, 22 % 5 = 2 and in -22 % 5 = -2. So, you should be careful when taking mods as there is a difference when signs with the number change.


Associativity And Precedence Of Operators In C:

When more than one operator is used in arithmetic expression, C++ follows the rules of precedence of arithmetic operators in c++ to evaluate the expression. 
Operator precedence in C++:
  1.  * , / , %
  2. + , -
 
Final words:
In this c tutorial for beginners of shapes in c we discussed different operators used in programming , there use and the precedence of operators in c++