DO IF, ELSE: Else not being computed - if-statement

Only the initial if statement variable computation is being completed--the ELSE part is being ignored. Can someone explain why? Many thanks.
DATASET ACTIVATE DataSet1.
DO IF ((A1_SCN2_PR1_UE = 0 & A1_SCN3_PR1_UE = 0 & A1_SCN4_PR1_UE = 0 & A1_SCN5_PR1_UE = 0) |
(A2_SCN2_PR1_UE = 0 & A2_SCN3_PR1_UE = 0 & A2_SCN4_PR1_UE = 0 & A2_SCN5_PR1_UE = 0) |
(A3_SCN2_PR1_UE = 0 & A3_SCN3_PR1_UE = 0 & A3_SCN4_PR1_UE = 0 & A3_SCN5_PR1_UE = 0)).
Compute FM_zero = 1.
ELSE.
Compute FM_zero = 0.
End IF.
EXECUTE.

Not sure why your ELSE is not being computed, but I suggest you drop the DO IF and go this way instead:
compute FM_zero =
((A1_SCN2_PR1_UE = 0 & A1_SCN3_PR1_UE = 0 & A1_SCN4_PR1_UE = 0 & A1_SCN5_PR1_UE = 0) |
(A2_SCN2_PR1_UE = 0 & A2_SCN3_PR1_UE = 0 & A2_SCN4_PR1_UE = 0 & A2_SCN5_PR1_UE = 0) |
(A3_SCN2_PR1_UE = 0 & A3_SCN3_PR1_UE = 0 & A3_SCN4_PR1_UE = 0 & A3_SCN5_PR1_UE = 0)).
This will put a 1 in all true cases and 0 in all false cases.

SPSS uses three-valued logic: True, False, or don't know (sysmis).
From the Syntax Reference Manual...
Missing values returned by the logical expression on DO IF or on any ELSE IF cause control to pass to
the END IF command at that point.
So generally you should put the sysmis test first in your DO IF and follow with appropriate ELSE IF/ELSE.

Related

How to return value of if else statement in ocaml?

I'd like to return the value of an if else statement in ocaml.
For example, if I do
let myvalue = if my_condition != 0 then do_this else do_this_instead
But this doesn't seem to store the result of do_this or do_this_instead in myvalue. If I was doing this in C
if(my_condition)
{
return 1 + 1;
} else {
return 1 - 1;
}
I want to kind of achieve the same affect, where not only is the if-else logic executed, but there is also kind of like a return value associated with the executed statement that is caught and stored. How might I do this in ocaml?
If I do following...
# let myvalue = if 2 > 1 then 2 else 1;;
val myvalue : int = 2
# myvalue;;
- : int = 2
What else are you expecting?
if-then-else is an expression construct, and expressions produce/have values.
Or else, if you looking to call other functions for values in the corresponding then and else construct, we can try following way...
# let my_condition = 2 in
let do_this () = 1 + 1 in
let do_this_instead () = 1 - 1 in
myvalue = if my_condition != 0 then do_this () else do_this_instead ();;
- : bool = true
# myvalue;;
- : int = 2

If Statement logic is not correct

I have this if statement and it is not working:
if (pEntry->GetStudentMaterialStudyPoint(StudentAssign::kBibleReading) == 1 &&
(pEntry->GetStudentMaterialStudyPoint(StudentAssign::kItem1) == 1 && LOBYTE(LOWORD(pEntry->GetStudentAssignFlags()))) &&
(pEntry->GetStudentMaterialStudyPoint(StudentAssign::kItem2) == 1 && HIBYTE(LOWORD(pEntry->GetStudentAssignFlags()))) &&
(pEntry->GetStudentMaterialStudyPoint(StudentAssign::kItem3) == 1 && LOBYTE(HIWORD(pEntry->GetStudentAssignFlags()))) &&
(pEntry->GetStudentMaterialStudyPoint(StudentAssign::kItem4) == 1 && HIBYTE(HIWORD(pEntry->GetStudentAssignFlags()))))
{
MWBValidationErrorStruct errorMWBValidation;
errorMWBValidation.iDateIndex = iDateIndex;
errorMWBValidation.datMeeting = pEntry->GetMeetingDate();
errorMWBValidation.eValidationErrorType = CChristianLifeMinistryDefines::MWBValidationErrorType::MaterialStudyPoints;
m_listValidationErrors.push_back(errorMWBValidation);
}
}
I am trying to find out if all of the items have a value of 1. The first item (bible reading) will always be checked. But the items 1 to 4 only need to be checked if they are "included". That is what the LOBYTE(LOWORD(pEntry->GetStudentAssignFlags())) is for.
So,
Bible Reading - 1
Item 1 - 1
Item 2 - 1 - Not included
Item 3 - 1 - Not included
Item 4 - 1 - Not included
In the above scenario the if should be true because both BR and Item 1 are set to 1. We ignore the other 3 items.
Bible Reading - 1
Item 1 - 2
Item 2 - 3
Item 3 - 1 - Not included
Item 4 - 1 - Not included
In the above scenario the if should return false because all the values are not 1 and we are ignoring the last two items.
What is wrong with my if logic?
You should use (!included || x == 1) to ignore checking an item if it's not included. Because of short circuiting, if included is false, you won't even check the other side of the OR, which is exactly what you want.
Your if could look like:
if (pEntry->GetStudentMaterialStudyPoint(StudentAssign::kBibleReading) == 1 &&
(!LOBYTE(LOWORD(pEntry->GetStudentAssignFlags())) || pEntry->GetStudentMaterialStudyPoint(StudentAssign::kItem1) == 1) &&
...
This may be a little confusing, so let's make a truth table...
included | x | !included | x == 1 | (!included || x == 1)
------------------------------------------------------
false | 3 | true | false | true
true | 3 | false | false | false
true | 1 | false | true | true
If included = false, then !included will be true, so (!included || x == 1) will always be true. This is what we want--if we're not included, just evaluate to true without even checking x == 1.
If included = true, then !included is false, so the value of (!included || x == 1) will be whatever x == 1 is. Which is again what we want. If we're included, then depend on x == 1.

Getting all timeDuration from a day which are not in a list of timeDuration in Scala

I have a list of timestamp tuples of the form List((startTime,endTime)), which are basically denoting periods of time throughout the day.
For example:
(("2016-03-28 14:00:00","2016-03-28 15:00:00"),
("2016-03-28 17:00:00","2016-03-28 21:00:00"),
("2016-03-28 01:00:00","2016-03-28 13:00:00"))
I want to get a list of the durations which are not included in this list.
So the output should be:
(("2016-03-28 00:00:00","2016-03-28 01:00:00"),
("2016-03-28 13:00:00","2016-03-28 14:00:00"),
("2016-03-28 15:00:00","2016-03-28 17:00:00"),
("2016-03-28 17:00:00","2016-03-28 24:00:00"))
Can anyone suggest a good and efficient way of doing that in Scala?
The naive solution that I've tried so far is as follows:
import java.sql.Timestamp
import scala.collection.mutable.ListBuffer
def comparator(first: (Timestamp,Timestamp), second: (Timestamp, Timestamp)) = first._2.getTime <= second._1.getTime
val data = List((Timestamp.valueOf("2016-03-28 00:00:00"),Timestamp.valueOf("2016-03-28 10:00:00")),
(Timestamp.valueOf("2016-03-28 12:00:00"),Timestamp.valueOf("2016-03-28 15:00:00")),
(Timestamp.valueOf("2016-03-28 23:00:00"),Timestamp.valueOf("2016-03-28 23:59:59")),
(Timestamp.valueOf("2016-03-28 16:00:00"),Timestamp.valueOf("2016-03-28 21:00:00"))
).sortWith(comparator)
var emptySlots = new ListBuffer[(Timestamp,Timestamp)]()
var currTime = Timestamp.valueOf("2016-03-28 00:00:00")
var index = 0
var cond = 0
while(cond == 0){
if (currTime.compareTo(Timestamp.valueOf("2016-03-28 23:59:59")) < 0 && index >= data.size){
emptySlots += ((currTime,Timestamp.valueOf("2016-03-28 23:59:59") ))
cond = 1
}
else if(index >= data.size)
{
cond = 1
}
else if(currTime.compareTo(data(index)._1) < 0) {
emptySlots += ((currTime, data(index)._1))
currTime = data(index)._2
index += 1
}
else if(currTime.compareTo(data(index)._1) >= 0 && currTime.compareTo(data(index)._2) < 0 ) {
currTime = data(index)._2
index += 1
}
else if(currTime.compareTo(data(index)._1) > 0 && currTime.compareTo(data(index)._2) > 0 ) {
index += 1
}
}
emptySlots.toList

C++ operators precedence

I am using this statement
if ((pm && pn) || (pm == false && pn == false))
it is supposed to return true only if both pm and pn are true or if both are false. But this is also returning true if only only first one (pm) is true.
So now it is acting like this:
0 0 = 1
0 1 = 0
1 0 = 1
1 1 = 1
but I need it to work like this:
0 0 = 1
0 1 = 0
1 0 = 0
1 1 = 1
can you tell me where am I making mistake?
What you want is simply:
if (pm == pn)
You are checking if pm is true twice. You also need to check if both are the same, not whether they are both true. So,
if ((pm == pn)
^^ ^^
pm && pm
should be
pm && pn
^
The whole expression can be simplified to
pm == pn
if the variables already have bool type.
Why not try xor?
if (!(pm ^ pn)) { /*...*/ }
Or simply equal?
if (pm == pn) { /*...*/ }
if ((pm && pm) || (pm == false && pn == false))
it is supposed to return true only if both pm and pn are true or if both are false. But this is also returning true if only only first one (pm) is true.
Because you made a typo. You meant pm && pn.
Instead just write if (pm == pn), which is equivalent along as the only semantic values are indeed true and false for both variables.
Plus, consider making your variable names clearer and more distinct.
Note that operator precedence has nothing to do with this.
Since the question's title asks about precedence, note that || has lower precedence than &&. So the two sets of inner parentheses are redundant, and the original expression is just a longer way of saying
if (pm && pm || pm == false && pn == false)
Now, fixing the obvious typo:
if (pm && pn || pm == false && pn == false)
Removing the unneeded explicit comparisons:
if (pm && pn || !pm && !pn)
And, finally, a less obvious transformation, which others have suggested:
if (pm == pn)

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.