C++ What If The Conditional Operator Were Left Associative [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is a question I found that I don't really know the answer. Suppose we have the following code:
int grade = 100;
string finalgrade = (grade > 90) ? "High Pass"
: (grade > 75) ? "Pass"
: (grade >= 60) ? "low pass" : "fail";
How would the conditional operator be evaluated if it were left associative?

If ?: were left-associative, that would mean that:
a ? b : c ? d : e
would be equivalent to:
(a ? b : c) ? d : e
instead of:
a ? b : (c ? d : e)
Applying this to your example, you'd get:
string finalgrade = ( ( (grade > 90)
? "High Pass"
: (grade > 75))
? "Pass"
: (grade >= 60))
? "low pass"
: "fail";
which---assuming that the compiler didn't complain about the mismatched types---for grade = 100 would result in:
string finalgrade = ( ( true
? "High Pass"
: (grade > 75))
? "Pass"
: (grade >= 60))
? "low pass"
: "fail";
string finalgrade = ( "High Pass"
? "Pass"
: (grade >= 60))
? "low pass"
: "fail";
string finalgrade = "Pass"
? "low pass"
: "fail";
string finalgrade = "low pass";

in your example :
finalgrade = "High Pass"
the computations are done from right to left

Related

What is wrong with this Switch statement? Im getting error cannot convert value text to type True/False

Season =
SWITCH(TRUE(),
'sales'[Month] = "December" || "January" , "Winter",
'sales'[Month] = "February" || "March" , "Spring",
'sales'[Month] = "April" || "May" || "June" , "Summer",
'sales'[Month] = "July" || "August" || "September" , "Monsoon",
'sales'[Month] = "October" || "November", "Autumn",
"NA")
Cannot convert value 'January' of type Text to type True/False.
You need to repeat the column reference in each statement. e.g.
sales'[Month] = "December" || sales'[Month] = "January" , "Winter",

If/Else Statement One Line

I'd like to incorporate if they currently have mmsa or jmmsa. MMMSA is balance over 2500 and JMMSA is balance over 100,000.
combined_2 = (
combined
.withColumn('mmsa_eligible',
F.when(
(F.col('min_bal_0_90_days') >= 2500) & (F.col('current_bal') >= 2500), 1
).otherwise(0)
)
.withColumn('jmmsa_eligible' ,
F.when(
(F.col('min_bal_0_90_days') >= 100000) & (F.col('current_bal') >= 100000), 1
).otherwise(0)
)
if jmmsa_eligible == 1 and jmmsa_current_flag == 0:
print ('Y')
else:
print ('N')

What's wrong with this logical operation work?

I'm trying to create an if statement that validates that the user's bet is either exactly 100, 300 or 500. What am i doing wrong?
if ((roundBet != 100) || (roundBet != 300) || (roundBet != 500))
{
cout << "Incorrect input";
// Call round again
newRound();
}
if ((roundBet != 100) || (roundBet != 300) || (roundBet != 500))
This will evaluates as true for all roundBet, because a number is either not 100(roundBet != 100 true) or 100 (which is not 300, roundBet != 300 true)
What you need is:
if ((roundBet != 100) && (roundBet != 300) && (roundBet != 500))
One of the alternatives will always be true, since if roundBet is, say, 100, then it will be different from 300 and 500.
Use a logical AND &&

C++ Ternary operator logic

I'm having trouble figuring out what this if statement is doing. This is not my code so I am simply trying to understand what the Ternary operator is doing.
if((model[STRIDE].isLogging == true ? model[STRIDE].value : g_uiStride) == g_uiStride &&
(model[NUMVERTS].isLogging == true ? model[NUMVERTS].value : NumVertices) == NumVertices &&
(model[PRIMCOUNT].isLogging == true ? model[PRIMCOUNT].value : primCount) == primCount &&
(model[STARTINDEX].isLogging == true ? model[STARTINDEX].value : startIndex) == startIndex)
{
First,
(model[STRIDE].isLogging == true ? model[STRIDE].value : g_uiStride) == g_uiStride
could be written:
(model[STRIDE].isLogging ? model[STRIDE].value : g_uiStride) == g_uiStride
the ternary
model[STRIDE].isLogging ? model[STRIDE].value : g_uiStride
checks to see if model[STRIDE].isLogging is true. If it is, it takes the value model[STRIDE].value. If not, it takes the value g_uiStride. This is then compared to g_uiStride.
So, if it isn't logging, then this portion is automatically true because g_uiStride is compared to itself. If it is logging, it is true if mode[STRIDE].value == g_uiStride
and
#1.
if (model[STRIDE].isLogging is true then
RESULT1 = (model[STRIDE].value == g_uiStride) else
RESULT1 = (g_uiStride == g_uiStride)
)
#2.
if (model[NUMVERTS].isLogging is true then
RESULT2 = (model[NUMVERTS].value == NumVertices) else
RESULT2 = (mVertices == NumVertices)
)
#3.
if (model[PRIMCOUNT].isLogging is true then
RESULT3 = (model[PRIMCOUNT].value == primCount) else
RESULT3 = (primCount == primCount)
}
#4.
if (model[STARTINDEX].isLogging is true then
RESULT4 = (model[STARTINDEX].value == startIndex) else
RESULT4 = (startIndex == startIndex)
)
if (RESULT1 && RESULT2 && RESULT3 && RESULT4) {
/* yay */
} else {
/* damn */
}
In general the ternary conditional operator uses a condition to choose between two alternatives:
condition ? first_alternative : second_alternative
In this case it is very unnecessarily complicated by comparing to true and one object to itself
if((model[STRIDE].isLogging == true ? model[STRIDE].value : g_uiStride) == g_uiStride
This can be reduced to
if((model[STRIDE].isLogging ? model[STRIDE].value : g_uiStride) == g_uiStride
which is also equivalent to
if (model[STRIDE].value == g_uiStride || !model[STRIDE].isLogging
telling us that either value is equal to some global value, or we don't care because we are not logging anyway.
blah = (model[STRIDE].isLogging == true ? model[STRIDE].value : g_uiStride)
is the same as
if (model[STRIDE].isLogging) {
blah = model[STRIDE].value ;
} else {
blah = g_uiStride;
}
The ternary operator is as follows:
(condition) ? value_for_true_condition : value_for_false_condition
(model[STRIDE].isLogging == true ? model[STRIDE].value : g_uiStride) first checks to see if the isLogging == true, the (condition). If the condition is true the model[STRIDE].value value is used, if not true the g_uiStride value is used.
The statement as a whole checks the values on all those members of model, but only if the member .isLogging == true. Otherwise it uses the default value. Note that this statement will always be true if all members have .isLogging variable set to false.

a really basic SML issue I just can't seem to figure out (small code)

Just a basic Casaer Cipher. I've tested all of the sub functions, just encryptChar() does not particularly work. I get an infinite loop. It's supposed to be recursive. Here's the all code:
fun replace (str : string, index : int, newChar : char) : string = String.substring(str,0,index) ^ String.str(newChar) ^ String.substring(str,index+1,(size str) - index - 1;
fun encryptChar (msgStr : string, shiftAmnt : int, index : int) : string =
let val asciiCode = 0
in
if (not (String.sub(msgStr, index) = #" ")) then
(
asciiCode = ord( String.sub(msgStr, index) ) + shiftAmnt;
if (asciiCode < ord(#"A")) then asciiCode = asciiCode + 26
else if (asciiCode > ord(#"Z")) then asciiCode = asciiCode - 26
else asciiCode = asciiCode;
msgStr = replace(msgStr, index, chr(asciiCode))
)
else asciiCode = asciiCode;
index = index + 1;
if (index < (size msgStr - 1)) then encryptChar(msgStr, shiftAmnt, index)
else msgStr
end
;
fun encrypt(msgStr : string, shiftAmnt : int) : string = encryptChar (String.map Char.toUpper msgStr, shiftAmnt mod 26, 0);
The problem here is that you're misusing =. Outside of a variable definition, = is simply a boolean function which checks its arguments for equality. So if you do for example asciiCode = ord( String.sub(msgStr, index) ) + shiftAmnt;, it will simply return false (because asciiCode is not equal to ord( String.sub(msgStr, index) ) + shiftAmnt) and then throw that result away (because you have additional expressions after the ;). It will not reassign asciiCode.
Variables in SML are immutable. If you want to emulate mutable variables you can use refs and the := operator. However I would not recommend that approach as it is generally not good functional style and not necessary in this case. The preferable approach would be to rewrite the code in a way that each variable is only assigned once.
This is very basic indeed, and it's surprising that you ran into it in such a complicated situation.
Did you port this from some other language?
You need to forget everything you know about programming using assignments.
let val x = y in something
means more or less "within 'something', replace the identifier 'x' with the value of 'y'".
There is no way for you to change the value of x.
Do the substitution (this is not the actual evaluation order or anything, but it should give you an idea of what's going on):
encryptChar("THIS", amount, 0)
=>
let val asciiCode = 0
in
if (not (String.sub("THIS", 0) = #" ")) then
(
asciiCode = ord( String.sub("THIS", 0) ) + amount;
if (asciiCode < ord(#"A")) then asciiCode = asciiCode + 26
else if (asciiCode > ord(#"Z")) then asciiCode = asciiCode - 26
else asciiCode = asciiCode;
"THIS" = replace("THIS", 0, chr(asciiCode))
)
else asciiCode = asciiCode;
0 = 0 + 1;
if (0 < (size "THIS" - 1)) then encryptChar("THIS", amount, 0)
else str
end ;
=>
if (not (String.sub("THIS", 0) = #" ")) then
(
0 = ord( String.sub("THIS", 0) ) + amount;
if (0 < ord(#"A")) then 0 = 0 + 26
else if (0 > ord(#"Z")) then 0 = 0 - 26
else 0 = 0;
"THIS" = replace("THIS", 0, chr(0))
)
else 0 = 0;
0 = 0 + 1;
if (0 < (size "THIS" - 1)) then encryptChar("THIS", amount, 0)
else str
=>
if (not (String.sub("THIS", 0) = #" ")) then
(
0 = ord( String.sub("THIS", 0) ) + amount;
if true then false
else if false then false
else true;
false
)
else true;
false;
if (0 < (size "THIS" - 1)) then encryptChar("THIS", amount, 0)
else "this"
->
if (not false) then
(
false;
false;
false
)
else true;
false;
if true then encryptChar("THIS", amount, 0)
else "THIS"
=>
(
false;
false;
false
)
false;
encryptChar("THIS", amount, 0)
=>
encryptChar("THIS", amount, 0)
Which is where your infinite loop came from.
You would do well to get hold of an introductory text about ML programming.