Calculation in C++: B-C && (B-A) /C - Which Answer is Right?

  • Context: C/C++ 
  • Thread starter Thread starter ccky
  • Start date Start date
  • Tags Tags
    C++ Calculation
Click For Summary

Discussion Overview

The discussion revolves around a C++ calculation involving logical and arithmetic operations. Participants are examining the expression "B-C && (B-A) / C" with specific integer values assigned to A, B, and C. The focus is on understanding the evaluation of the expression and the implications of integer to boolean conversion in C++.

Discussion Character

  • Homework-related
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant believes the answer is 0 because the first part of the expression is negative.
  • Another participant argues that the answer is 1, stating that -1 does not equate to false.
  • A third participant asserts that both previous claims are incorrect and explains the evaluation process, indicating that the expression evaluates to false (0) based on C++ rules regarding boolean conversion.
  • A later reply acknowledges a misunderstanding regarding the evaluation of the entire expression.

Areas of Agreement / Disagreement

Participants do not reach a consensus, as differing interpretations of the expression's evaluation persist. Multiple competing views remain regarding the final result of the calculation.

Contextual Notes

There are unresolved assumptions regarding operator precedence and the implications of integer division in C++. The discussion does not clarify the exact steps leading to the final evaluation.

ccky
Messages
15
Reaction score
0
I am doing my homework regarding calculation in c++.
I am confused by the below question.
Int A=2 B=3 C=4
B-C && (B-A) /C
I think the answer is 0 because the first part is negative.
But,my friends say the answer is 1 because -1 not mean false.
I want to ask which answer is right?
Thanks.
 
Technology news on Phys.org
Your friend is right - -1 doesn't mean false. 0 and only 0 means false.
 
  • Like
Likes   Reactions: 1 person
You and your friend are both incorrect. You could easily check this by running it through a C++ compiler.

In C++, there is implicit conversion from ints to bools. && has pretty low precedence, so first the following expressions are evaluated: B - C is 3 - 4 = -1, and (B - A) / C is 1 / 4 = 0 (in integer division).
So the resulting expression is -1 && 0 which is equivalent to true && false, hence the result you get is false (0).
 
Last edited:
  • Like
Likes   Reactions: 1 person
Ah, I referred just to the small part of the post, I have not actually bothered to evaluate whole expression :shy:
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
69
Views
11K
  • · Replies 25 ·
Replies
25
Views
3K
Replies
1
Views
2K