5 Replies - 481 Views - Last Post: 28 April 2009 - 05:59 AM Rate Topic: -----

#1 apw5020  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Dumb Down the Following Code...

Posted 27 April 2009 - 09:31 PM

Hi, could someone please dumb down and/or explain the following code for me? I know what 'enum' does, as well as the function arguments and stuff, but I am not familiar with the '?' or ':' symbols. Thanks in advance.

enum CELL_STATE {
	STATE_NOTSET = 0,
	STATE_FIX,
	STATE_FIXTEMP,
};
void SetValue(int rows, int columns, int currentValue, char CellValue[PUZZLE_SIZE][PUZZLE_SIZE])
{
	CELL_STATE iState = STATE_FIX;
	CellValue[rows][columns] = currentValue * iState == 0 ? '0' : (iState == STATE_FIX ? 'a':'A') + currentValue;
}



Actually, if someone could rewrite it without using the '?' and ':' I would really appreciate it.

This post has been edited by apw5020: 27 April 2009 - 09:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Dumb Down the Following Code...

#2 BlakeJustBlake  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: Dumb Down the Following Code...

Posted 27 April 2009 - 10:05 PM

? : is called the ternary operator.

What it does is like this:

a>b ? cout<<a : cout<<b;

is the same as saying

if(a>B) cout<<a; else cout<<b;

So whatever comes before the question mark is the if statement, what comes after it is what to do if the condition is true, what comes after the colon is what to execute when it's not the case the if statement is true.
Was This Post Helpful? 1

#3 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Dumb Down the Following Code...

Posted 27 April 2009 - 11:39 PM

I never knew that, learn something new every day. Well explained BlakeJustBlake.
Was This Post Helpful? 0
  • +
  • -

#4 BlakeJustBlake  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: Dumb Down the Following Code...

Posted 27 April 2009 - 11:47 PM

I wouldn't go throwing it around everywhere though, it's very hard to read. The place I see it used the most is obfuscated perl code.
Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Dumb Down the Following Code...

Posted 28 April 2009 - 03:46 AM

Read some more here
http://en.wikipedia....tional_operator

As BlakeJustBlake has pointed out, treat with care and caution.
Can bite you badly and is hard to debug.
Was This Post Helpful? 0
  • +
  • -

#6 apw5020  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: Dumb Down the Following Code...

Posted 28 April 2009 - 05:59 AM

Alright, thanks for the help everyone.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1