<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: If-else in jmp (and ifmz) in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/If-else-in-jmp-and-ifmz/m-p/463092#M70775</link>
    <description>&lt;P&gt;From the Scripting Index for the If() function (&lt;STRONG&gt;emphasis mine) :&lt;/STRONG&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;P&gt;&lt;STRONG&gt;Evaluates the first of each pair&lt;/STRONG&gt; of arguments and returns the evaluation of the result expression associated with the first condition argument that evaluates to a nonzero result. The condition arguments are evaluated in order. &lt;SPAN&gt;If&lt;/SPAN&gt; all of the condition arguments evaluate to zero, the optional elseResult is evaluated and the result is returned. &lt;SPAN&gt;If&lt;/SPAN&gt; no elseResult is spec&lt;SPAN&gt;if&lt;/SPAN&gt;ied, and none of the conditions are true, a missing value is returned. &lt;SPAN&gt;If&lt;/SPAN&gt; all of the condition arguments evaluate to missing, a missing value is returned.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I bolded the import part, "Evaluates the first of each pair..."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, an If() with three commas has two pairs. Consider this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;a=3;

If(
	a==1, show("a==1"), //first pair - evaluate whether a==1
	a==2, show("a==3") //second pair - evaluate whether a==2
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That If() will return missing because there was no "first of each pair" expression which returned a non-zero value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your If() function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If(

	val,Print(Eval Insert("if_expr_three\!t^val^\!ttrue")); 1;, // first pair - evaluate whether val is nonZero 
	
	//begin second pair
	Print(Eval Insert("if_expr_three\!t^val^\!tfalse?"));0; , 
	Print(Eval Insert("if_expr_three\!t^val^\!tsomething?"));-1; 
	
	// second pair -- evaluate Print(); 0; - that returns 0 (since that was the last value in the expression) - so the second condition isn't nozero
	);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Remember that JSL is a purely functional language with operators to make it easier to read but the ; is the operator for the &lt;A href="https://www.jmp.com/support/help/en/16.2/#page/jmp/operators.shtml" target="_self"&gt;Glue() function&lt;/A&gt;. The Glue() function returns the last expression it evaluates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See:&amp;nbsp;&lt;LI-MESSAGE title="The difference between glue and if" uid="60206" url="https://community.jmp.com/t5/Discussions/The-difference-between-glue-and-if/m-p/60206#U60206" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-forum-thread lia-fa-icon lia-fa-forum lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 23 Feb 2022 02:14:25 GMT</pubDate>
    <dc:creator>Jeff_Perkinson</dc:creator>
    <dc:date>2022-02-23T02:14:25Z</dc:date>
    <item>
      <title>If-else in jmp (and ifmz)</title>
      <link>https://community.jmp.com/t5/Discussions/If-else-in-jmp-and-ifmz/m-p/463070#M70773</link>
      <description>&lt;P&gt;I know (now..) that if you have a possibility of having missing value and you would like to have it evaluated as 0 you should use IfMZ in JMP. What I'm wondering what is happening in the example below, if you have if-statement with "two elses" (won't return syntax error). In the example below it is expression &lt;CODE class=" language-jsl"&gt;if_expr_three&lt;/CODE&gt; (and same for &lt;CODE class=" language-jsl"&gt;ifmz_expr_three&lt;/CODE&gt;:(&lt;/img&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

if_expr_two = Expr(
	res = If(val,
		Print(Eval Insert("if_expr_two\!t^val^\!ttrue"));
		1;
	, 
		Print(Eval Insert("if_expr_two\!t^val^\!tfalse"));
		0
	);
	show(res);	
);

// what does this evaluate to and why does val = . return second statement
if_expr_three = Expr(
	res = If(val,
		Print(Eval Insert("if_expr_three\!t^val^\!ttrue"));
		1;
	, 
		Print(Eval Insert("if_expr_three\!t^val^\!tfalse?"));
		0;
	,
		Print(Eval Insert("if_expr_three\!t^val^\!tsomething?"));
		-1;
	);
	show(res);
);

ifmz_expr_two = Expr(
	res = IfMZ(val,
		Print(Eval Insert("ifmz_expr_two\!t^val^\!ttrue"));
		1;
	, 
		Print(Eval Insert("ifmz_expr_two\!t^val^\!tfalse"));
		0;
	);
	show(res);
);

ifmz_expr_three = Expr(
	res = IfMZ(val,
		Print(Eval Insert("ifmz_expr_three\!t^val^\!ttrue"));
		1;
	, 
		Print(Eval Insert("ifmz_expr_three\!t^val^\!tfalse?"));
		0;
	,
		Print(Eval Insert("ifmz_expr_three\!t^val^\!tsomething?"));
		-1;
	);
	show(res);
);

val = 1;
if_expr_two; // OK, prints true and returns 1
if_expr_three; // OK?, prints true and returns 1
ifmz_expr_two; // OK, prints true and returns 1
ifmz_expr_three; // OK?, prints true and returns 1
Write("\!N");
val = 0;
if_expr_two; // OK, prints false and returns 0
if_expr_three; // ? prints false?, but returns missing
ifmz_expr_two; // OK, prints false and returns 0
ifmz_expr_three; // ? prints false?, but returns missing
Write("\!N");
val = .;
if_expr_two; // OK, no print but returns missing value
if_expr_three; // ? prints false? and returns missing
ifmz_expr_two; // OK, prints false and returns 0
ifmz_expr_three; // ? prints false? and returns missing
Write();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I will most likely send question about this to JMP support but I want to also ask for opinions in JMP community.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a bit similar case as &lt;A href="https://community.jmp.com/t5/Discussions/Associative-arrays-and-removal-of-keys-with-value-same-as/td-p/461239" target="_blank" rel="noopener"&gt;Associative arrays and removal of keys with value same as default value&lt;/A&gt; in that sense that I'm not sure why this is happening (I did send message about that to jmp support, still waiting for final answer).&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 18:11:18 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/If-else-in-jmp-and-ifmz/m-p/463070#M70773</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2023-06-09T18:11:18Z</dc:date>
    </item>
    <item>
      <title>Re: If-else in jmp (and ifmz)</title>
      <link>https://community.jmp.com/t5/Discussions/If-else-in-jmp-and-ifmz/m-p/463092#M70775</link>
      <description>&lt;P&gt;From the Scripting Index for the If() function (&lt;STRONG&gt;emphasis mine) :&lt;/STRONG&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;P&gt;&lt;STRONG&gt;Evaluates the first of each pair&lt;/STRONG&gt; of arguments and returns the evaluation of the result expression associated with the first condition argument that evaluates to a nonzero result. The condition arguments are evaluated in order. &lt;SPAN&gt;If&lt;/SPAN&gt; all of the condition arguments evaluate to zero, the optional elseResult is evaluated and the result is returned. &lt;SPAN&gt;If&lt;/SPAN&gt; no elseResult is spec&lt;SPAN&gt;if&lt;/SPAN&gt;ied, and none of the conditions are true, a missing value is returned. &lt;SPAN&gt;If&lt;/SPAN&gt; all of the condition arguments evaluate to missing, a missing value is returned.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I bolded the import part, "Evaluates the first of each pair..."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, an If() with three commas has two pairs. Consider this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;a=3;

If(
	a==1, show("a==1"), //first pair - evaluate whether a==1
	a==2, show("a==3") //second pair - evaluate whether a==2
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That If() will return missing because there was no "first of each pair" expression which returned a non-zero value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your If() function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If(

	val,Print(Eval Insert("if_expr_three\!t^val^\!ttrue")); 1;, // first pair - evaluate whether val is nonZero 
	
	//begin second pair
	Print(Eval Insert("if_expr_three\!t^val^\!tfalse?"));0; , 
	Print(Eval Insert("if_expr_three\!t^val^\!tsomething?"));-1; 
	
	// second pair -- evaluate Print(); 0; - that returns 0 (since that was the last value in the expression) - so the second condition isn't nozero
	);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Remember that JSL is a purely functional language with operators to make it easier to read but the ; is the operator for the &lt;A href="https://www.jmp.com/support/help/en/16.2/#page/jmp/operators.shtml" target="_self"&gt;Glue() function&lt;/A&gt;. The Glue() function returns the last expression it evaluates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See:&amp;nbsp;&lt;LI-MESSAGE title="The difference between glue and if" uid="60206" url="https://community.jmp.com/t5/Discussions/The-difference-between-glue-and-if/m-p/60206#U60206" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-forum-thread lia-fa-icon lia-fa-forum lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Feb 2022 02:14:25 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/If-else-in-jmp-and-ifmz/m-p/463092#M70775</guid>
      <dc:creator>Jeff_Perkinson</dc:creator>
      <dc:date>2022-02-23T02:14:25Z</dc:date>
    </item>
  </channel>
</rss>

