Computer Programming Operators - Computer Programming

What is Computer Programming Operators?

An operator in a programming language is a symbol that tells the compiler or interpreter to perform specific mathematical, relational or logical operation and produce final result. This chapter will explain the concept of operators and it will take you through the important arithmetic and relational operators available in C, Java, and Python.

Arithmetic Operators

Computer programs are widely used for mathematical calculations. we can write a computer program which can do simple calculation like adding numbers (2 + 3) and we can also write a program, which can solve a complex equation like P(x) = x4 + 7x3 - 5x + 9. if you have been even a poor student, you must be aware that in first expression 2 and 3 are operands and + is an operator. similar concepts exist in computer Programming.

Take a look at the following two examples –

These statements are called arithmetic expressions in a programming language and plus, minus used in these expressions are called arithmetic operators and the values used in these expressions like 2, 3 and x, etc., are known as operands. in their simplest form, such expressions produce numerical results.

Similarly, a programming language provides various arithmetic operators. the following table lists down a few of the essential arithmetic operators available in c programming language. assume variable A holds 10 and variable B holds 20, then –

Operator

Description

Example

+

Adds two operands

A + B will give 30

-

Subtracts second operand from the first

A - B will give -10

*

Multiplies both operands

A * B will give 200

/

Divides numerator by de-numerator

B / A will give 2

%

This gives remainder of an integer division

B % A will give 0

Following is a simple example of C Programming to understand the above mathematical operators

When the above program is executed, it produces the following result –

Relational Operators

Consider a situation where we create two variables and assign them some values as follows –

Here, it is obvious that variable A is greater than B in values. So, we need the help of some symbols to write such expressions that are called relational expressions. If we use c programming language, then it will be written as follows –

Here, we used a symbol > and it is called a relational operator and of their simplest form, they produce Boolean results which means the result can be either true or false. similarly, a programming language provides various relational operators. the following table lists down a few of the important relational operators available in c programming language. expect variable A holds 10 and variable B holds 20, then –

Operator

Description

Example

==

Checks if the values of two operands are equal or not, if yes then condition becomes true.

(A == B) is not true.

!=

Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.

(A != B) is true.

>

Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.

(A > B) is not true.

<

Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.

(A < B) is true.

>=

Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.

(A >= B) is not true.

<=

Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

(A <= B) is true.

Here, we will show you one example of C Programming which makes use of if conditional statement. though this statement will be discussed later in a separate chapter, however in short, we use if statement to check a condition and if the situation is true, then the body of if statement is executed, otherwise the body of if statement is skipped.

When the above program is executed, it produces the following result –

Logical Operators

Logical operators are very important in any programming language and they help us take decisions based on certain conditions. suppose we want to combine the result of two conditions, then logical AND and OR logical operators help us in producing the final result.

The following table indicates all of the logical operators supported through the c language. assume variable A holds 1 and variable B holds zero, then −

Operator Description Example
&& Called Logical AND operator. If both the operands are non-zero, then condition becomes true. (A && B) is false.
|| Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. (A || B) is true.
! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is true.

Try the following example to understand all the logical operators available in C programming language –

When you compile and execute the above program, it produces the following result –

Operators in Java

Following is the equivalent program written in Java. C programming and Java provide almost identical set of operators and conditional statements. This program will create two variables a and b, very similar to C programming, then we assign 10 and 20 in those variables and finally, we will use different arithmetic and relational operators −

You can try to execute the following program to see the output, which have to be same to the result generated through the above example.

Operators in Python

Following is the equivalent program written in Python. This program will create variables a and b and at the same time, assign 10 and 20 in those variables. fortunately, C programming and Python programming languages provide almost identical set of operators. This program will create two variables a and b, very similar to C programming, then we assign 10 and 20 in these variables and finally, we will use different arithmetic and relational operators.

You can try to execute the following program to see the output, which must be identical to the result generated through the above example.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Computer Programming Topics