Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Can any one explain this statement
int x=1,y=11,z;
z=x--&&y++¦¦--x;
I know the answer but not able to think that how it is coming
It pretty simple
x--
returns 1 (true). Therefore
y++
is evaluated. It returns 11
1 && 11
returns true. Since
true || X
is always true
--x
is not evaluated. The statement returns true (1).
This is called short-circuit evaluation. E.g.
a && b || c
means
if a is true
evaluate b
if a && b is false
evaluate c
You could rewrite it as
int x=1,y=11,z;
z=[&]() {
if (!(x--)) {
return false;
}
if (y++) {
return true;
}
if (x++) {
return true;
}
return false;
}();
It's important to notice that the order of evaluation is as described. That's one of the reasons you shouldn't overload operator&& or operator||. The described behavior only works if the operators are not overloaded.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
This problem is to output a single integer, the number of possible combinations calculated:
int power(int a, int n)
{
if (n == 0)
return 1;
// else
if (n % 2 == 0) {
int temp = power(a, n / 2);
return temp * temp;
}
// else
return a * power(a, n - 1);
}
This function uses a technique called exponentation by squaring.
It's a particularly efficient way of evaluating the power for integral type arguments. The standard C function uses floating point arguments, and the C standard doesn't require an exact result even if the floating point arguments represent whole numbers.
In C++ though you can probably rely on one of the overloads of std::pow that takes integral type arguments, and cast the result, subject to your making the necessary size checks. But again even the C++ standard does not require that the best possible result is returned (cf. std::sqrt under IEEE754), although one could reasonably regard a std::pow function that does not return the correct result for integral arguments to be defective.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I would like to know the correct usage of conditionals such as if statements to avoid undefined behaviours. Let's start with an example:
uint8_t x = 0;
bool y = false;
bool z = false;
if ((x == 135) and !y and !z) {
//do something
}
else if ((x == 135) and y) {
x = 5;
z = true;
}
else if ((x == 5) and z) {
x = 135;
z = false;
}
else {
//do something
}
Now, will I get undefined behaviour by not including all 3 variables into every condition? Will every unaccounted for condition go into the else statement? If so, what happens if I get rid of the else statement? I have the exact same if statement (in a more complex scenario) and I seem not to be getting into the right statements every time.
Please enlighten me if there is a rule for this?
will I get undefined behaviour by not including all 3 variables into every condition?
The behaviour of not including all variables into every condition is not undefined by itself.
Will every unaccounted for condition go into the else statement?
Statement-false (i.e. the statement after the keyword else) is executed if the condition is false.
what happens if I get rid of the else statement?
The execution continues from the statement after the if statement.
As a rule of thumb yes, it's a bad idea to attempt to read a variable that has not been initialised as quite often the behaviour on doing that is undefined. But that's not the case here: all your variables are initialised.
But all you need in an if(...) condition is something that evaluates to either true or false. On that point your code is absolutely fine. I'd use && and || rather than and and or though as the former are more common.
For a pretty comprehensive set of constructs that are undefined, see What are all the common undefined behaviours that a C++ programmer should know about?
No, you don't have undefined behaviour here. What it sounds like is that you don't have an accurate mental model of conditional (boolean) logic. else is always an optional part of an if statement.
Exactly one of the { ... } blocks will be executed. Changing the values of x, y or z inside any of them will not cause any others to be executed, the decision is made first.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Consider the speed of the following examples (please ignore that this example is completely ridiculous):
int multiply(int a, int b) {
if (a == 0 || b == 0) {
return 0;
}
if (a == b) {
return pow(a, 2);
}
return a*b;
}
versus
int multiply(int a, int b) {
if (a == 0 || b == 0) {
return 0;
} else if (a == b) {
return pow(a, 2);
} else {
return a*b;
}
}
Obviously, it's not really necessary here, but when I'm working with complex operations, I find it a lot easier to read when formatted as the latter. Does it take any longer to run in the second configuration? Will I be sacrificing anything?
EDIT: Answering's OP question first, then talking about the general case.
Sepcific-to-your-problem Answer: In your case, since you have the return, under each condition, it will break out of the flow-control. In a general case, chaining them, when necessary, is better.
General Answer: Yes, essentially when you're only using if, your program is checking all the conditions, even if one of them was met.
When doing a chain of if,else-if,else, once one of the conditions is met, all the others, in that chain, will be ignored.
In this particular case, no compiler I'm aware of would fail to
generate the exact same output for both. However, in the general
case:
Write for readability and maintainability
Profile profile profile
Optimize only where you find bottlenecks
Test your optimization using more profiling
The fourth part is important, hardware have many surprising
optimizations builtin, it's not at all intuitive what will
be faster.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If a unsigned byte overflows it goes from 255 to 0 and vica versa -1 gives 255.
Would it be possible to have it overflow at for example 200?
Without using if statements.
Overflow is fairly simple:
unsigned int a = 150, b = 150;
a += b; // do an operation
a %= 200; // wrap it
However, with underflow, it's a bit harder (see orlp's answer for this).
To make it less error prone if you use this variable several times, in C++ with operator overloading, you can make a class that simulates an integer type which wraps after every operation with operator overloading.
The modulo operator does what you want, with some trickery for negative values:
int wrap(int x, int n) {
return x < 0 ? ((x % n) + n) % n : x % n;
}
// wrap(205, 200) == 5
// wrap(-1, 200) == 199
Unless your willing to learn assembly, such an action would be impossible for several reasons.
All the types like char, short, int, etc. are builtin and predefined by the parser.
200 isnt a power of two; computer represent numbers in binary.
Note: The above is only true if you want implicit overflow; modulas lets you do explicit overflow.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
What does this function do ?
inline bool myFunc(int aVal) {return aVal & 1;}
Edit
I have edited the code to be runnable.
Keyword inline shall be written with lower case letters.
inline bool myFunc(int aVal) {return aVal & 1;}
The function returns true if the first bit of the value aVal is set to 1. Otherwise it returns false.
Using this function you can check for example whether aVal is odd or even number. :) If the function will return true then it means that the number is odd.
The operator & is bitwise AND operator.