# Help with 'tertiary operator'

**URL:** <https://forum.kirupa.com/t/help-with-tertiary-operator/22212>\
**Category:** Uncategorized\
**Created:** [June 1, 2003, 1:59am UTC](https://forum.kirupa.com/t/help-with-tertiary-operator/22212 "2003-06-01T01:59:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zylum](https://avatars.discourse-cdn.com/v4/letter/z/d26b3c/32.png) [@zylum](https://forum.kirupa.com/u/zylum)\
**Post date:** [June 1, 2003, 1:59am UTC](https://forum.kirupa.com/t/help-with-tertiary-operator/22212/1 "2003-06-01T01:59:12Z")

</div>

is there a way to use the ‘tertiary operator’ to shorten this code?  
[AS]if (this.\_x\<0) {  
this.\_x = Width;  
}  
if (this.\_x\>Width) {  
this.\_x = 0;  
}  
if (this.\_y\<0) {  
this.\_y = Heigth;  
}  
if (this.\_y\>Heigth) {  
this.\_y = 0;  
}[/AS]  
i tried  
AS?this.\_x = Width;  
(this.\_x\>Width)?this.\_x = 0;  
(this.\_y\<0)?this.\_y = Heigth;  
(this.\_y\>Heigth)?this.\_y = 0;[/AS]  
but an error comes up…

can anyone help?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 1, 2003, 2:57am UTC](https://forum.kirupa.com/t/help-with-tertiary-operator/22212/2 "2003-06-01T02:57:23Z")

</div>

never mind, i figured it out, you need a : null at the end of the statement for the else. so it would be:

AS ? this.\_x = Width : null;  
(this.\_x\>Width) ? this.\_x = 0 : null;  
(this.\_y\<0) ? this.\_y = Heigth : null;  
(this.\_y\>Heigth) ? this.\_y = 0 : null;[/AS]  
or to shorten it:

AS ? this.\_x = Width : (this.\_x\>Width) ? this.\_x = 0 : (this.\_y\<0) ? this.\_y = Heigth : (this.\_y\>Heigth) ? this.\_y = 0 : null;[/AS]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 1, 2003, 3:34am UTC](https://forum.kirupa.com/t/help-with-tertiary-operator/22212/3 "2003-06-01T03:34:29Z")

</div>

actually, the last script is not a really good idea… :-\

you should use this one instead. 😉

```auto
this._x<0 ? this._x=Width : this._x>Width ? this._x=0 : null;
this._y<0 ? this._y=Heigth : this._y>Heigth ? this._y=0 : null;

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 1, 2003, 7:53am UTC](https://forum.kirupa.com/t/help-with-tertiary-operator/22212/4 "2003-06-01T07:53:15Z")

</div>

I think you’re kinda missing the poin of the operator 😉

```auto

this._x = (this._x < 0) ? Width : (this._x > Width) ? 0 : this._x;
this._y = (this._y < 0) ? Height : (this._y > Heigth) ? 0 : this._y;

```

🥚

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 1, 2003, 7:18pm UTC](https://forum.kirupa.com/t/help-with-tertiary-operator/22212/5 "2003-06-01T19:18:16Z")

</div>

😛

nice! i didn’t thought about that. =)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 1, 2003, 11:07pm UTC](https://forum.kirupa.com/t/help-with-tertiary-operator/22212/6 "2003-06-01T23:07:53Z")

</div>

I think I spelled one of the words wrong :!:

😉
