cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
jthi
Super User

If-else in jmp (and ifmz)

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 if_expr_three (and same for ifmz_expr_three

 

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();

I will most likely send question about this to JMP support but I want to also ask for opinions in JMP community.

 

This is a bit similar case as Associative arrays and removal of keys with value same as default value 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).

-Jarmo
1 ACCEPTED SOLUTION

Accepted Solutions
Jeff_Perkinson
Community Manager Community Manager

Re: If-else in jmp (and ifmz)

From the Scripting Index for the If() function (emphasis mine) :


Evaluates the first of each pair 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. If all of the condition arguments evaluate to zero, the optional elseResult is evaluated and the result is returned. If no elseResult is specified, and none of the conditions are true, a missing value is returned. If all of the condition arguments evaluate to missing, a missing value is returned.

I bolded the import part, "Evaluates the first of each pair..."

 

So, an If() with three commas has two pairs. Consider this:

 

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
);

That If() will return missing because there was no "first of each pair" expression which returned a non-zero value.

 

In your If() function:

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
	);

Remember that JSL is a purely functional language with operators to make it easier to read but the ; is the operator for the Glue() function. The Glue() function returns the last expression it evaluates.

 

See: The difference between glue and if 

 

-Jeff

View solution in original post

1 REPLY 1
Jeff_Perkinson
Community Manager Community Manager

Re: If-else in jmp (and ifmz)

From the Scripting Index for the If() function (emphasis mine) :


Evaluates the first of each pair 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. If all of the condition arguments evaluate to zero, the optional elseResult is evaluated and the result is returned. If no elseResult is specified, and none of the conditions are true, a missing value is returned. If all of the condition arguments evaluate to missing, a missing value is returned.

I bolded the import part, "Evaluates the first of each pair..."

 

So, an If() with three commas has two pairs. Consider this:

 

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
);

That If() will return missing because there was no "first of each pair" expression which returned a non-zero value.

 

In your If() function:

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
	);

Remember that JSL is a purely functional language with operators to make it easier to read but the ; is the operator for the Glue() function. The Glue() function returns the last expression it evaluates.

 

See: The difference between glue and if 

 

-Jeff