tag:blogger.com,1999:blog-865923359735383241.post193930655943131197..comments2023-10-29T07:27:09.012-06:00Comments on Ccna final exam - java, php, javascript, ios, cshap all in one: Is there a better way of writing v = (v == 0 ? 1 : 0);Unknownnoreply@blogger.comBlogger8125tag:blogger.com,1999:blog-865923359735383241.post-89091833092246754512012-05-30T03:52:11.411-06:002012-05-30T03:52:11.411-06:00If it must be the integer 1 or 0, then the way you...If it must be the integer 1 or 0, then the way you're doing it is fine, though parentheses aren't needed. If these a are to be used as booleans, then you can just do:<br /><br />v = !v;Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-40809591238716620192012-05-30T03:52:10.290-06:002012-05-30T03:52:10.290-06:00You could do
v = Math.abs(--v);
The decrement ...You could do <br /><br />v = Math.abs(--v);<br /><br /><br />The decrement sets the value to 0 or -1, and then the Math.abs converts -1 to +1.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-11072740027612992552012-05-30T03:52:09.275-06:002012-05-30T03:52:09.275-06:00Lines like v = 1 - v, or v ^= 1 or v= +!v will all...Lines like v = 1 - v, or v ^= 1 or v= +!v will all get the job done, but they constitute what I would refer to as hacks. These are not beautiful lines of code, but cheap tricks to have the intended effect. 1 - v does not communicate "toggle the value between 0 and 1". This makes your code less expressive and introduces a place (albeit a small one) where another developer will have to parse your code.<br /><br />Having instead a function like v = toggle(v) communicates the intent at the quickest glance.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-76295356685379140862012-05-30T03:52:08.255-06:002012-05-30T03:52:08.255-06:00If you don't care about any possibility other ...If you don't care about any possibility other than 1:<br /><br />v = v ? 0 : 1;<br /><br /><br />In the above case, v will end up being 1 if v is 0, false, undefined or null. Take care using this kind of approach - v will be 0 even if v is "hello world".Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-91969367517720999032012-05-30T03:52:07.315-06:002012-05-30T03:52:07.315-06:00You could write a function for it and use it like:...You could write a function for it and use it like:<br /><br />v = inv(v)Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-16673944016694503082012-05-30T03:52:06.476-06:002012-05-30T03:52:06.476-06:00v = (v + 1) % 2 and if you need to cycle through m...v = (v + 1) % 2 and if you need to cycle through more values just change 2 for (n + 1). Say you need to cycle 0,1,2 just do v = (v + 1) % 3.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-45236736583756136642012-05-30T03:52:05.117-06:002012-05-30T03:52:05.117-06:00Since 0 is a false value and 1 is a true value.
v...Since 0 is a false value and 1 is a true value.<br /><br />v = (v ? 0 : 1);<br /><br /><br />If you are happy to use true and false instead of numbers<br /><br />v = !v;<br /><br /><br />or if they must be numbers:<br /><br />v = +!v; /* Boolean invert v then cast back to a Number */Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-83318853465876331142012-05-30T03:52:03.270-06:002012-05-30T03:52:03.270-06:00You can simply use:
v = 1 - v;
This of course a...You can simply use:<br /><br />v = 1 - v;<br /><br /><br />This of course assumes that the variable is initialised properly, i.e. that it only has the value 0 or 1.<br /><br />Another method that is shorter but uses a less common operator:<br /><br />v ^= 1;<br /><br /><br />Edit:<br /><br />To be clear; I never approached this question as code golf, just to find a short way of doing the task without using any obscuring tricks like side effects of operators.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.com