What is mixed mode arithmetic expression with an example?

What is Mixed Mode Arithmetic Expression?

In a statement or expression if one the operand is real (float) and another one is integer then expression is called as Mixed Mode Arithmetic Expression. If in an expression either operand is of real then output is always in real format. If both operands are real then output will be in real formats.

Example

Real operand and Integer operand = Real operand (Output)
Real operand and Real operand = Real operand (Output).

Types of Arithmetic Operators used in Mixed Mode Expression

"*" - Multiply use for Multiplication
"/"  - Divide use for division
"+" - Plus use for addition
"-" - Minus use for subtraction
"%" - Modulus use for getting a reminder
"**" - Square  use for getting square number

Rules for Evaluation Mixed Mode Arithmetic Expression

Rule 1

Evaluate Expressions always from Left to Right

For Example: 3 + 5 - 4 = 4

Rule 2

Priority of an operator is also considered while calculating an expression

Operator Priorities in descending order


1. (**) Known as Square operator which executes From Right To Left .
   
For example 5**2 is equal to 25

2. (*) Known as multiplication operator which executes From Left to Right
   
For example 5 * 2 is equal to 10

3. (/) Known as Division operator which executes From Left to Right
   
For example 6 / 2 is equal to 3

4. (+) Known as Plus or Addition operator which executes From Left to Right
   
For example 6 + 2 is equal to 8

5. (-) Known as Minus or Subtraction operator which executes From Left to Right
   
For example 6 - 2 is equal to 4

Mixed mode operator example
 
2 + 2 * 5 ** 3

==> 2 + 2 * (5**3)
==> 2 + 2 * 125
==> 2 + (2 * 125)
==> 2 + 250 = 252

Rule 3

If the operands of this operator are of the same type means either Integer or Real, compute the same for result in that operator only.

For Example

Both Real Numbers

double a = 2.0;
double b = 3.0;

Double add = 2.0 + 3.0 = 5.0 (Result in Real Number only)

Both Integer Number

int a = 2;
int b = 3;
int add = 2 + 3 = 5 (Result in Integer Number only)

Rule 4

If one of the operator is Integer and other one is real then convert integer number to real number and compute the result in real number only.

For Example

double a = 5.0;

int b = 5;

Result = 5.0 = 5 = 10.0 (Result in Real Number only).

So this all about evaluating mixed mode arithmetic expression. Let me know your thoughts while working on mixed mode arithmetic expression. Kindly post your comments.

Author: Gurunatha Dogi

Gurunatha Dogi

Gurunatha Dogi is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server and lots more..... read more

Add a Comment