Title: modifying variable in if statement....
Description: unorthodox coding is ....fun :P
ih8censorship - March 5, 2006 05:15 AM (GMT)
Ive managed to get myself in a situation where i need to change the value of a variable IN an if statement, or else my code will be really redundant and writing a really huge function to do what i need done would be kind of a pain..but im writing a function now anyway i guess..at least this one is a ton smaller than the other one would have been.. anyway at first i tryed this code because i was being an optimist-
| CODE |
#include <iostream> using namespace std;
int main() { int x=10; int d=1; if(d>0 && x=11 ) cout<<x; cin.get(); return 0; }
|
and when that didnt work i wrote this and this works fine, but leaves me wishing there was an easyer way.
| CODE |
#include <iostream> using namespace std;
bool setint(int * blah,int value)//this needs to return true always or it may not work. { *blah=value; return true; }
int main() { int x=10; int d=1; if(d>0 && setint(&x,11) ) cout<<x; cin.get(); return 0; }
|
so anyway i know this sortof thing is really unorthodox, but like i said the alternative is a huge function with lots of redundancy/arguments... and this is just my attempt at keeping the redundancy to a minimum. any sort of comments are welcome, after all thats why im posting it.
:P
Viper - March 5, 2006 08:10 AM (GMT)
| CODE |
| (d < 0 && (x = 11)) ? (cout << x) : (0); |
Should work, might be possible to compile even with some of the parantheses removed.
myork - March 5, 2006 01:30 PM (GMT)
What's wrong with:
| CODE |
if(d>0) {x=11; cout<<x;} |
but the problem with
is operator precedence. Add some brckets around the code to inforce the order you require.
Currently it is doing:
| CODE |
bool a1 = d>0; // PLevel 7 bool a2 = a2 && x; // PLevel 12 bool result = a2=11; // PLevel 15 /* This causes g++ to fail so your code does not compile on my machine */ if (result) |
AquaFox - March 5, 2006 01:49 PM (GMT)
Isn't it supposed to be double equals for comparing? "=="?
myork - March 5, 2006 01:57 PM (GMT)
| QUOTE (AquaFox @ Mar 5 2006, 08:49 AM) |
| Isn't it supposed to be double equals for comparing? "=="? |
Yes:
But ih8censorship is tryng to be far too clever and putting an assignment inside an expression in an attempt to save space.
AquaFox - March 5, 2006 02:09 PM (GMT)
Wow, ih8censorhip is far too clever :). I didn't know that was even possible. I learn a new thing a day I guess.
C00L - March 5, 2006 07:13 PM (GMT)
Why not just do it before or after the if statement? I have never been in a situation where it would make the code more complex.
aab - March 7, 2006 05:43 PM (GMT)
You could always use a list of statements (This works here at least (vc++6)
| CODE |
if ( ( x=11, d>0 ) )
|
This reminded me of something that may sometimes actually be usefull.
In University we use a secure sot of language called Ada:
There are operators
"and then" and "or else"
so eg:
| CODE |
if n<array_size and then MyArray(n).valid then
|
would only evaluate 'MyArray(n).valid' if 'n' was less than array_size, for example.
'or else' is ..duh.
lets see....C++:
| CODE |
if ( ( n<sz ? MyArray[n] : false ) )
|
or to be evil and weird...
| CODE |
#define and_then(expression) ? expression : false #define or_else(expression) ? true : expression if ( ( n<sz and_then( MyArray[n] ) )
|
..Sorry for going off on a lil' journey there.
myork - March 7, 2006 07:28 PM (GMT)
| QUOTE (aab @ Mar 7 2006, 12:43 PM) |
You could always use a list of statements (This works here at least (vc++6)
| CODE | if ( ( x=11, d>0 ) )
|
|
Subtle difference:
| CODE |
| if ((d > 0) && (x = 11)) { /*DO Somthing */} |
11 is assigned to x only if (d> 0).
This is because of the shortcut behavior of the '&&' operator.
Shortcut behavior: && ||
(A && B ): If A is false it does not matter what B is the expression is false.
So the shortcut kicks in and B is not evaluated.
(A || B ): If A is true it does not matter what B is the expression is true.
So the shortcut kicks in and B is not evaluated.
NB: WARNING: This does not apply to user overloading of these operators. It only applies to the builtin versions of the operator.
This technique is often used for accessing pointers when they could be NULL:
| CODE |
X* x = getX(); if ((x) && (x->isOK)) { /* * If x is NULL then (x) is 0 or false. * So the second half of the test is never performed. */ /* Do Somthing */ } |