Jump to content

Ternary operator clarification ($1 = 0) ? ($b = 0) : ($b = 1)


kylomas
 Share

Recommended Posts

Okay, I still don't like the name 'ternary operator'. I think it breaks some ground rules.

What are you talking abut czardas?! The reason this operator is called "the ternary operator" is because it's the only ternary operator in the language. An alternative is to call it "question_mark_colon" operator, or maybe not even calling it at all.

If "plus" is the only binary operator it would also be called "the binary operator". But it's not so it's called "binary operator plus" or simply fucking "plus".

I used word "fucking" as modifier, not unary operator.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

What are you talking abut czardas?! The reason this operator is called "the ternary operator" is because it's the only ternary operator in the language. An alternative is to call it "question_mark_colon" operator, or maybe not even calling it at all.If "plus" is the only binary operator it would also be called "the binary operator". But it's not so it's called "binary operator plus" or simply fucking "plus".I used word "fucking" as modifier, not unary operator.

Well met.

Edited by Mobius

wtfpl-badge-1.png

Link to comment
Share on other sites

It doesn't matter what I'm on about. Mat's links exposed my misunderstanding, but even so: someone has to challenge mainstream opinion. I do not like the naming strategy becasue it conflicts with other interpretations of both the word 'ternary' and 'argument'. There are the three arguments that you see plus the two that I see. Where would programming be without logic?

;

MsgBox(0, "czardas is", Random(0 ,1, 1) ? "stupid" : "not stupid")

;

As for my idea of a ternary operator, am I to now call that a quaternary operator when it is an implimentation in TVL? That hurts a lot I'm afraid.

Edited by czardas
Link to comment
Share on other sites

My argument is this: there is a conflict. Is this misuse of the word argument? No it's just a different meaning of the word which also applies, in context to this operator, with equal validity. I take on board your arguments, but the justification seems somewhat tenuous.

I'm obviously not going to win this one.

Edit

Actually Mat's links did jog my memory (thanks). I have come across this use of the word 'argument' previously in school all those years ago. I remember thinking it was stupid back then. I still see no justification for this counterintuitive terminology. Until I'm presented with a reasonable case for it, I have no reason to change my opinion. Peer pressure is not always enough. If it makes sense to everyone else then fine - to me it's a semantic collision which is simply too close to for comfort. This is my only criticism, but one I feel is important enough to maintain.

It is my conviction that programming should be accessible to all in the future, and not continue its current course towards impregnable terminology. To me an argument must have a point. Random(0, 1, 1) does not have a point and is therefore no argument.

I'm finished.

Edited by czardas
Link to comment
Share on other sites

Is it possible to assign a value with the ternary operator in C++? 

You know:

= - assignment

== - expression

I'd call it "The_two_choice_operator_based_on_three_arguments_thingy"

Edited by DatMCEyeBall

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

Link to comment
Share on other sites

Is it possible to assign a value with the ternary operator in C++? 

You know:

= - assignment

== - expression

I'd call it "The_two_choice_operator_based_on_three_arguments_thingy"

Well, best check before asking.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Is it possible to assign a value with the ternary operator in C++? 

You know:

= - assignment

== - expression

I'd call it "The_two_choice_operator_based_on_three_arguments_thingy"

Assignment *is* an expression.

So "true ? (a = 5) : (a = 6)" would return 5, and assign the value of 5 to a.

Link to comment
Share on other sites

Yes.

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int a = 0;
    int b = 22;
    
    cout << "Before: " << a << "\n";
    (b == 23) ? (a = 12) : (a = 13); // b <> 23, so a = 13
    cout << "After: " << a << "\n";
        
    system("PAUSE");
    return 0;
}

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

Link to comment
Share on other sites

As we are in Dev forum and this topic ask for clarification about ternary operator, I would like to know if there is a chance to have the same new syntax like in php 5.3 ?

As kylomas write in the first post, we can write the expression like this :

$Var = (@Condition) ? $ExpTrue : $ExpFalse

In php we can write :

$Return = $Var1 ? $Var1 : $Var2;

and contract it in :

$Return = $Var1 ?: $Var2;

Do you think that it will be possible to have the same syntax in future version of AutoIt ?

Edited by Tlem

Best Regards.Thierry

Link to comment
Share on other sites

Last one is the @Condition $Var1 and the true expression also $Var1?

That could prove useful...  :geek:

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

Link to comment
Share on other sites

What, another macro?

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

What, another macro?

@Condition isn't a macro, it's something that  Tlem used to describe the condition in the ternary operator example that he posted.

; This
$Return = $Var1 ? $Var1 : $Var2;
; Convert (shows the contraction process)
$Return = $Var1 ?[$Var1]: $Var2;

; Can be this (The first expression (the "Run on true") one is omitted and in the contracted one the conditional statement becomes the "Run on true" expression)
$Return = $Var1 ?: $Var2;

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

Link to comment
Share on other sites

I explicitly disallowed that back then in hope someone would ask for it in future. (It should have been nice game that I never got to play.)

Yes, it's possible. Make feature request and see what Jon says. This isn't just syntactic sugar, being able to omit second argument makes valuable difference.
For example here:

$c = $a ? $a : $b

... $a is evaluated twice, whereas here:

$c = $a ?: $b

...only once.

At first you may not realize the quality of the difference, until you gear-up and imagine what would happen if $a isn't simple variable but, for example function call. In former case, that function would possibly be called twice based on it's return.

All in all, it would make nice addition to the feature

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

I explicitly disallowed that back then in hope someone would ask for it in future. (It should have been nice game that I never got to play.)

Yes, it's possible. Make feature request and see what Jon says. This isn't just syntactic sugar, being able to omit second argument makes valuable difference.

For example here:

$c = $a ? $a : $b

... $a is evaluated twice, whereas here:

$c = $a ?: $b

...only once.

At first you may not realize the quality of the difference, until you gear-up and imagine what would happen if $a isn't simple variable but, for example function call. In former case, that function would possibly be called twice based on it's return.

All in all, it would make nice addition to the feature

If you are calling a function twice due to checking its return value for non-false-equivalence, then you need to cache better.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...