View Single Post
  #3   Spotlight this post!  
Unread 16-01-2004, 13:56
deltacoder1020's Avatar
deltacoder1020 deltacoder1020 is offline
Computer Guy
AKA: Dav
#1020 (The Indiana Prank Monkeys)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Muncie, Indiana
Posts: 340
deltacoder1020 has a spectacular aura aboutdeltacoder1020 has a spectacular aura about
Send a message via AIM to deltacoder1020
Re: Probably a basic question, but....

are you sure it's not

Code:
int hi = 0;

return hi ? 1 : 0;
what i've just written is equivalent to this:

Code:
int hi = 0;

if(hi) return 1;
else return 0;
basically, the code
Code:
a ? b : c;
means "replace this with b is a is true, otherwise, replace this with c"

so you could use
Code:
var1 += (x>2 ? 4 : 1);
to increment var1 by 4 if x is greater than 2, but only by 1 if x is less than or equal to 2.

it's more than just a compact way of doing if statements, as i've illustrated - you can't embed a if statement in an expression, whereas you can embed the ternary operator.
__________________
Team 1020, the Indiana Prank Monkeys (www.team1020.org)