Question

In: Computer Science

Although Java has the rule that the left operand of every binary operator is evaluated before...

Although Java has the rule that the left operand of every binary operator is evaluated before the right operand, most languages give the compiler the freedom to choose which operand is evaluated first. When expressions have side effects, the value of the expression can be different depending upon which order is used. Give an example in C++ of an expression whose value depends upon the evaluation order. Show the orders that produce different values and the values they produce. Explain what side effect is the expression contains.

Solutions

Expert Solution

In C++, the expression is checked in such a way that the checking starts from the left hand side then move to the right hand side.

With the operators such as &&, ||, if there is no requirement only one side gets evaluated. Example of && operator. If the first condition becomes false, there is no need to check the second condition.

Ex1:

#include <iostream>

using namespace std;

int main() {

int a = 0;

int b = 0;

if(a == 0 && b++ > 0)

{

}

cout << "a = " << a << " b = " << b << endl;

}

Explanation:

  • The value of a and b is displayed as 0 and 1 respectively.
  • Since, the value of a is checked first (a == 0) and becomes true so the next statement b++ > 0 is being checked.
  • Now, this time from b++, the value of b first become 1 (increased by 1) and then checked.
  • After checking it becomes true.
  • Hence, the value of a is 0 and the value of b becomes 1, so displayed 0 and 1.

Ex2:

#include <iostream>

using namespace std;

int main() {

int a = 1;

int b = 0;

if(a == 0 && b++ > 0)

{

}

cout << "a = " << a << " b = " << b << endl;

}

Explanation:

  • The value of a and b is displayed as 1 and 0.
  • In this case, the condition (a == 0) becomes false so the next term b++ > 0 is not checked.
  • Hence, the value of both a and b is 0, So displayed as 0 and 0.

For the operator ||, if the first condition becomes true, there is no requirement for checking the second condition.

And if the first condition becomes false, it checks the second condition and if it is true then the whole statement considered to be true.

Ex1:

#include <iostream>

using namespace std;

int main() {

int a = 0;

int b = 0;

if(a == 0 || b++ > 0)

{

}

cout << "a = " << a << " b = " << b << endl;

}

Explanation:

  • In this case, the value of a and b is 0. When the condition a == 0 is checked, it becomes true so the next expression b++ > 0 is not checked so the value of b is not increased.
  • Hence, the value of a and b is displayed as 0 0.

Ex2:

#include <iostream>

using namespace std;

int main() {

int a = 1;

int b = 0;

if(a == 0 || b++ > 0)

{

}

cout << "a = " << a << " b = " << b << endl;

}

Explanation:

  • In this case, the value of a and b is 1 and 0 respectively. When the condition a == 0 is checked it becomes false so the next expression is checked.
  • When the compiler moves to the next expression b++ > 0, the value increased by 1 and becomes true.
  • So, now the value of a is 1 (initialized value) and the value of b is become 1 (increased by 1 (b++)).
  • Thus, the value of a and b is displayed as 1, 1 respectively.

Related Solutions

How do I fix the "error: bad operand types for binary operator '*' " in my...
How do I fix the "error: bad operand types for binary operator '*' " in my code? What I am trying to do: double TotalPrice = TicketPrice * NoOfTickets;       My code: import javax.swing.*; /*provides interfaces and classes for different events by AWT components*/ import java.awt.event.*; import javax.swing.JOptionPane; //TicketReservation.java class TicketReservation { public static void main(String args[]) { /*Declare JFrame for place controls.*/ JFrame f= new JFrame("Movie Ticket Reservation");                                   /*Declare JLabels*/ JLabel...
Prove that Z/nZ is a group under the binary operator "+" for every n in positive...
Prove that Z/nZ is a group under the binary operator "+" for every n in positive Z, where Z is the set of integers.
a tree is binary, if every node has at most two children nodes. prove that the...
a tree is binary, if every node has at most two children nodes. prove that the maximum of nodes in a binary tree of height h is 2^(h+1)-1
Although the FDA has issued a new rule requiring that cigarette packages bear graphic images warning...
Although the FDA has issued a new rule requiring that cigarette packages bear graphic images warning of the health risks associated with smoking, it has been enjoined from enforcing this requirement until October 2021 while a federal court considers a legal challenge brought by several tobacco companies. In their suit, the tobacco companies claim that the rule compels them to speak an "anti-tobacco" message in violation of the First Amendment. Which of the following statements about this legal challenge is...
Prove If C is a binary self-dual  code, show that every codeword has even weight. Furthermore, prove...
Prove If C is a binary self-dual  code, show that every codeword has even weight. Furthermore, prove if each row of the generator matrix of C has weight divisible by 4, then so does every codeword.
In the country of Zip, Zappo is a very important resource. The economy of Zip has been using 1,000 tons of Zappos every year, and there are only 30,000 tons of Zappos left
In the country of Zip, Zappo is a very important resource. The economy of Zip has been using 1,000 tons of Zappos every year, and there are only 30,000 tons of Zappos left. Sam is a concerned citizen of Zip. Sam says “we need new government policies to conserve Zappo. If we don’t make some kind of policy, we will run out of Zappo in 30 years, and then the economy will collapse.”  Is the economy sure to collapse in 30...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT