explain how operands are evaluated and how the expressions derive their answers,
ID: 3930595 • Letter: E
Question
explain how operands are evaluated and how the expressions derive their answers, FOR EACH EXPRESSION!!! Execute the programs in the given language and explain the results Language : Java /******************************** * Explain how the value of each of the operands in each of * these experssions are evaluated. **************************************/ public class ex3{ public static void main(String args[]) { int a=2, b=3, c=4, d=0; System.out.println("Before the calculation 1 "); System.out.println("D= " + d + " a= " + a+ " b= " + b + " c=" + c); d = (a*b) - (a++) + (--c)/a; System.out.println("After the calculation 1 "); System.out.println("D= " + d + " a= " + a +" b= " + b + " c=" + c); a=2; b=3; c=4; d=20; System.out.println("Before the calculation 2 "); System.out.println("D= " + d + " a= " + a+ " b= " + b + " c=" + c); a = b + ( c = d/ b++) + b -1; System.out.println("After the calculation 2 "); System.out.println("D= " + d + " a= " + a +" b= " + b + " c=" + c); }// end main }
Explanation / Answer
Here is the explanation as comments.
/********************************
* Explain how the value of each of the operands in each of
* these experssions are evaluated.
**************************************/
public class OperatorPrecedence
{
public static void main(String args[])
{
int a=2, b=3, c=4, d=0; //a = 2, b = 3, c = 4, d = 0.
System.out.println("Before the calculation 1 ");
System.out.println("D= " + d + " a= " + a+ " b= " + b
+ " c=" + c); //No modifications, so, the output is: D = 0 a = 2 b = 3 c = 4
d = (a*b) - (a++) + (--c)/a;
//d = (2*3) - (a++) + (--c) / a
// = 6 - 2 + 3 / a
// = 6 - 2 + 3 / 3
// = 6 - 2 + 1
// = 4 + 1
// = 5.
//So, after this operation: a = 3, b = 3, c = 3, d = 5.
System.out.println("After the calculation 1 ");
System.out.println("D= " + d + " a= " + a +" b= " + b
+ " c=" + c); //So the output is: D = 5 a = 3 b = 3 c = 3
a=2; b=3; c=4; d=20; //Hardcoded the values, so, a = 2, b = 3, c = 4, d = 20.
System.out.println("Before the calculation 2 ");
System.out.println("D= " + d + " a= " + a+ " b= " + b
+ " c=" + c); //No modifications, so the output is: D = 20 a = 2 b = 3 c = 4
a = b + ( c = d/ b++) + b -1;
//a = 3 + (c = 20 / 3) + b - 1
//a = 3 + (c = 6) + b - 1
//a = 3 + 6 + 4 - 1
//a = 9 + 4 - 1
//a = 13 - 1
//a = 12
System.out.println("After the calculation 2 ");
System.out.println("D= " + d + " a= " + a +" b= " + b
+ " c=" + c); //So, the output is: D = 20 a = 12 b = 4 c = 6
}// end main
}
//So, finally, after executing the whole code, the output is:
//D = 0 a = 2 b = 3 c = 4
//D = 5 a = 3 b = 3 c = 3
//D = 20 a = 2 b = 3 c = 4
//D = 20 a = 12 b = 4 c = 6
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.