Can I assign an operator symbol to a variable and use that variable for a conditional check?
char operator= '>';
int val1=10;
int val2=24;
if(val2 operator val1){
/* some code*/
}
Why cant I use the operator variable inside conditions?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Code written in any programming language needs to be converted to Assembly Language. When this happens, every code statement written in High Level language gets translated to instruction / set of instructions in Middle Level equivalent i.e.
JAVA code will get translated to Machine Specific instructions in Assembly Language.
Here addition operation in following statement,
May get converted to
ADD A 1010;
And when we try to access a variables value, it may get converted to READ instruction.
So, when you try to use ‘>’ in a variable,
The if statement,
will convert to an invalid instruction.
Instead of generating an equivalent for comparison of two values, it will READ ‘operator’ variable.
This will obviously, lead to wrong interpretation.
Hence, doing such thing is not allowed.
Every compiler(java , gcc etc ) may behave differently but the target is same. If you read Complier / Compilation / Execution more, you will know more.
These are some links:
http://www.coderanch.com/t/559258/java/java/java-codes-converted-assembly-JVM
Do programming language compilers first translate to assembly or directly to machine code?
Steps for Compilation of A C program.
http://www.herongyang.com/Computer-History/C-Program-Compilation-and-Execution-Process.html