Csound Csound-dev Csound-tekno Search About

Question Mark operator

Date2005-10-25 14:02
FromDavid Akbari
SubjectQuestion Mark operator
Hi List,

So I was looking at one of Art Hunkins' pieces today and I noticed a 
syntax resembling the following:

krel	=	(gkoff1 == 1? gkr: 0)

kamp	=	(gkoff1 == 1? 0: kamp)

Basically what I'm wondering is, what is the function of the question 
mark operator is in this context? Is it some kind of conditional 
statement?

Any help appreciated!


-David

Date2005-10-25 14:08
Fromjpff
SubjectRe: Question Mark operator
Standard C notation for conditional expression
==John ff

Date2005-10-25 14:14
From
SubjectRe: Question Mark operator

Hi David,

 

In English I would read this statement “krel   =    (gkoff1 /x-tad-smaller>==/x-tad-smaller>/color> 1? gkr: 0) as  “Does gkoff1 equal 1? If yes then krel is gkr otherwise krel is zero.”

 

It is a standard if/then/else statement.

 

Hope this helps.

 

Michael Rhoades

 

 

 

-----Original Message-----
From: David Akbari [mailto:dakbari@gmail.com]
Sent:
Tuesday, October 25, 2005
To: csound@lists.bath.ac.uk
Subject: [Csnd] Question Mark operator

 

Hi List,

So I was looking at one of Art Hunkins' pieces today and I noticed a syntax resembling the following:

krel /x-tad-smaller>=/x-tad-smaller>/color> (gkoff1 /x-tad-smaller>==/x-tad-smaller>/color> 1? gkr: 0)

kamp /x-tad-smaller>=/x-tad-smaller>/color> (gkoff1 /x-tad-smaller>==/x-tad-smaller>/color> 1? 0: kamp)

/x-tad-smaller>Basically what I'm wondering is, what is the function of the question mark operator is in this context? Is it some kind of conditional statement?

Any help appreciated!


-David

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.859 / Virus Database: 585 - Release Date:
2/14/2005


Date2005-10-25 14:22
FromDave Seidel
SubjectRe: Question Mark operator
Hi David,

This is what's called the "ternary" operator, originally from C, but now 
also in Java and other languages.  It's essentially a kind of "if" 
statement.  The structure is like this:

conditional-statement ? do-this-if-true : do-this is-false

where the conditional statement is a boolean (i.e., evaluated to true or 
false), and the other statements are executed based on how the 
conditional statement is evaluated.

For example:

krel = (gkoff1 == 1? gkr: 0)

can be translated into "pseudocode" like so:

   if gkoff1 is equal to 1 then
       set krel1 to gkr
   else
       set krel1 to 0

- Dave


David Akbari wrote:
> Hi List,
> 
> So I was looking at one of Art Hunkins' pieces today and I noticed a 
> syntax resembling the following:
> 
> krel = (gkoff1 == 1? gkr: 0)
> 
> kamp = (gkoff1 == 1? 0: kamp)
> 
> Basically what I'm wondering is, what is the function of the question 
> mark operator is in this context? Is it some kind of conditional statement?
> 
> Any help appreciated!
> 
> 
> -David

Date2005-10-25 15:33
FromDavid Akbari
SubjectRe: Question Mark operator
On Oct 25, 2005, at 9:22 AM, Dave Seidel wrote:

> where the conditional statement is a boolean (i.e., evaluated to true 
> or false), and the other statements are executed based on how the 
> conditional statement is evaluated.

I find this very interesting. I am not a programmer so I have not 
become acquainted with this until recently.

What advantages (or disadvantages) does this technique (question mark 
as a conditional boolean expression) offer as opposed to:

ex1
**	if (gkoff == 1) kgoto somecode
		kgoto bypasscode

ex2
**	if (gkoff == 1) then
		somecode = something
	else
		somecode = 0
		endif

ex3
**	if (gkoff == 1) then
		somecode = something
	elseif (gkoff == 2) then
		othercode = otherthing
	elseif ...
	... etc
		endif

and also, how (if at all) is the question mark boolean expression 
implementation different in Csound5 than in previous versions?


-David

Date2005-10-25 15:53
FromDave Seidel
SubjectRe: Question Mark operator
It really just comes down to a matter of style more than anything else. 
  In the programing language world, the ternary operator is what's 
referred to as "syntactic sugar", meaning it's an inessential but 
elegant way to expressing something.  The ternary operator is nice and 
compact, so it's suitable when all three expressions are simple ones. 
If the conditional expression is more complex, or if the code the is 
executed on the branches is more complex (e.g., more than one line of 
code), then the ternary expression is not very suitable.

Beyond style and personally taste, consider the readability of your 
code, both for yourself coming back to it after time has passed, and for 
other people reading your code.  In general, it's best to write code 
that clearly expresses your intention, unless you have some reason (such 
as a critical optimization, for example in realtime code) to do otherwise.

I'm not aware of any difference between Csound4 and Csound5 regarding 
the ternary operator, but I'm sure John or Istvan will correct me of 
these is.  Likewise, I don't know if there is any difference in 
performance between the different styles of conditional statements.

- Dave


David Akbari wrote:
> 
> On Oct 25, 2005, at 9:22 AM, Dave Seidel wrote:
> 
>> where the conditional statement is a boolean (i.e., evaluated to true 
>> or false), and the other statements are executed based on how the 
>> conditional statement is evaluated.
> 
> 
> I find this very interesting. I am not a programmer so I have not become 
> acquainted with this until recently.
> 
> What advantages (or disadvantages) does this technique (question mark as 
> a conditional boolean expression) offer as opposed to:
> 
> ex1
> **    if (gkoff == 1) kgoto somecode
>         kgoto bypasscode
> 
> ex2
> **    if (gkoff == 1) then
>         somecode = something
>     else
>         somecode = 0
>         endif
> 
> ex3
> **    if (gkoff == 1) then
>         somecode = something
>     elseif (gkoff == 2) then
>         othercode = otherthing
>     elseif ...
>     ... etc
>         endif
> 
> and also, how (if at all) is the question mark boolean expression 
> implementation different in Csound5 than in previous versions?
> 
> 
> -David
> 

Date2005-10-25 22:50
FromIain Duncan
SubjectRe: Question Mark operator
You can also include the left hand side in the conditional, ie

k_foo = ( k_something > k_foo ? k_foo : k_something )

The above can be really handy, and done a bunch of times in a row can 
make what would be a page of ugly if-thens into 5 lines. As I recall 
nesting in the conditional is ok too:

k_foo = ( ( k_something > k_foo ) && ( k_bar > 2  ) ? k_foo + 1 : 
k_somthing)

Iain


David Akbari wrote:
> Hi List,
> 
> So I was looking at one of Art Hunkins' pieces today and I noticed a 
> syntax resembling the following:
> 
> krel = (gkoff1 == 1? gkr: 0)
> 
> kamp = (gkoff1 == 1? 0: kamp)
> 
> Basically what I'm wondering is, what is the function of the question 
> mark operator is in this context? Is it some kind of conditional statement?
> 
> Any help appreciated!
> 
> 
> -David