Question Operator

View previous topic View next topic Go down

Question Operator

Post by BIT0102-Mohaimin on Tue Mar 02, 2010 3:27 am

Question operator may be used instead of s single if else block in many cases.

Syntax: ////////////////
conditional statement ? statement1 : statement2.
////////////////

Here, the expressions can be any valid non-conditional statements, including function calls.

Example 1:
Code:
int a = 2, b = 3;
// Find the bigger one;

// replace this
int big = a;
if(big<b){
 big = b;
}

// with thisint big;
a>b ? big=a : big=b;

// or with this
int big = a>b ? a : b


Last edited by BIT0122-Amit on Tue Mar 02, 2010 4:16 am; edited 2 times in total (Reason for editing : merged double posts)
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Re: Question Operator

Post by BIT0122-Amit on Tue Mar 02, 2010 4:18 am

Interesting post Mohaimin Smile
I have a question though. I know I can use question operator in C/C++. Probably in Java too.
can i use this operator in any other programming language?
And

conditional statement ? statement1 : statement2.
is not exactly clear to me, neither is the example. Can you please explain the example?

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Question Operator

Post by BIT0102-Mohaimin on Wed Mar 03, 2010 12:19 am

@Amit:
Java and C support this, I do not know about other languages... Smile

conditional statement means something that returns true or false...
The explanation of the total syntax is:
if the conditional statement is true, then statement1 is executed, otherwise, statement2 is executed.
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Re: Question Operator

Post by BIT0122-Amit on Wed Mar 03, 2010 12:44 am

The explanation of the total syntax is:
if the conditional statement is true, then statement1 is executed, otherwise, statement2 is executed.

cheers perfectly explained Mohaimin Smile
Exactly what I was looking for.

And by the way, it is also called ternary operator Very Happy

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Question Operator

Post by BIT0102-Mohaimin on Wed Mar 03, 2010 1:28 am

Yah,,, ternary operator... Because there are three operands...
I searched for more ternary operators but didn't find anything else... So I thing ?: is the only (common) ternary operator in C/C++ and Java
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Re: Question Operator

Post by BIT0122-Amit on Wed Mar 03, 2010 2:02 am

:p actually, i think ternary operator is the correct name instead of question operator then

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Question Operator

Post by BIT0102-Mohaimin on Wed Mar 03, 2010 3:08 am

Ternary Operator is a generic name. There may be more ternary operators.
Like, + - etc are binary operators.
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Re: Question Operator

Post by BIT0122-Amit on Wed Mar 03, 2010 3:20 am

BIT0102-Mohaimin wrote:Ternary Operator is a generic name. There may be more ternary operators.
Like, + - etc are binary operators.

Hmmm.. but

?: is a ternary operator in that it takes three values, this is the only ternary operator C has.
-source
So, when we say the term "ternary operator" in C/C++/Java, it does mean only ? .
However, I am now even more interested in another particular factor Very Happy

Is ternary operation the only feature '?' has in C?
or in other words, can we use '?' in any other way other than the ternary operations?

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Question Operator

Post by BIT0102-Mohaimin on Wed Mar 03, 2010 6:37 pm

I Don't know...
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Re: Question Operator

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum