⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ifcmd.hlp

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 HLP
字号:
{smcl}
{* 09feb2005}{...}
{cmd:help ifcmd}, {cmd:help else}
{hline}

{title:Title}

{p2colset 5 15 17 2}{...}
{p2col :{hi:[P] if} {hline 2}}if programming command{p_end}
{p2colreset}{...}


{title:Syntax}

	{cmd:if} {it:exp} {cmd:{c -(}}{...}
{col 42}or{col 52}{cmd:if} {it:exp} {it:single_command}
		{it:multiple_commands}
	{cmd:{c )-}}

{pstd}
which, in either case, may be followed by

	{cmd:else} {cmd:{c -(}}{...}
{col 42}or{col 52}{cmd:else} {it:single_command}
		{it:multiple_commands}
	{cmd:{c )-}}

{pstd}
If you put braces following the {cmd:if} or {cmd:else},

{phang2}
1.  the open brace must appear on the same line as the {cmd:if} or
    {cmd:else};

{phang2}
2.  nothing may follow the open brace except, of course, comments;
    the first command to be executed must appear on a new line;

{phang2}
3.  the close brace must appear on a line by itself.

{pstd}
In versions prior to Stata 8, putting the open brace, command, and close brace
on the same line as the {cmd:if} or {cmd:else} was allowed, but omitting the
braces altogether was not.  Old behavior is restored under version control;
see {helpb version}.


{title:Description}

{pstd}
The {cmd:if} command (not to be confused with the {cmd:if} qualifier; see
{helpb if}) evaluates {it:exp}.  If the result is true
(nonzero), the commands inside the braces are executed.  If the result is
false (zero), those statements are ignored, and the statement (or statements 
if enclosed in braces) following the {cmd:else} is executed.

{pstd}
See {help exp} for an explanation of expressions.


{title:Remarks}

{pstd}
Remarks are presented under the headings

	{hi:Typical use}
	{hi:Typical use:  Example 1}
	{hi:Typical use:  Example 2}
	{hi:Typical use:  Example 3}
	{hi:Avoid single-line if and else with ++ and -- macro expansion}


{pstd}
That is, the following is invalid:

	{cmd:if x==2 {c -(} local y = 3 {c )-}}

{pstd}
Type instead

	{cmd:if x==2 local y = 3 }

{pstd}
Some programmers prefer to type

	{cmd:if (x==2) local y = 3 }

{pstd}
We are among them, but the braces around the expression are not required.


{title:Typical use:  Example 1}

    {cmd:program define} {it:...}
	    {cmd:syntax varlist [, Report ]}
	    {it:...}
	    {cmd:if "`report'" != "" {c -(}}
		    {it:(logic for doing the optional report)}
	    {cmd:{c )-}}
	    {it:...}
    {cmd:end}


{title:Typical use:  Example 2}

    {cmd:program define} {it:...}
	    {cmd:syntax varlist [, Adjust(string) ]}
	    {it:...}
	    {cmd:if "`adjust'" != "" {c -(}}
		    {cmd:if "`adjust'" == "means" {c -(}}
			    {it:...}
		    {cmd:{c )-}}
		    {cmd:else if "`adjust'" == "medians" {c -(}}
			    {it:...}
		    {cmd:{c )-}}
		    {cmd:else {c -(}}
			    {cmd:display as err /*}
			    {cmd:*/ "specify adjust(means) or adjust(medians)"}
			    {cmd:exit 198}
		    {cmd:{c )-}}
	    {cmd:{c )-}}
	    {it:...}
    {cmd:end}


{title:Typical use:  Example 3}

    {cmd:program define} {it:...}
	    {it:...}
	    {cmd:if x==1      local word "one"}
	    {cmd:else if x==2 local word "two"}
	    {cmd:else if x==3 local word "three"}
	    {cmd:else if x==4 local word "four"}
	    {cmd:else         local word "big"}
	    {it:...}
    {cmd:end}


{title:Avoid single-line if and else with ++ and -- macro expansion}

{pstd}
Do not use the singe-line forms of {cmd:if} and {cmd:else}{hline 2}do not omit
the braces{hline 2}when the action includes the {cmd:`++'} or {cmd:`--'}
macro-expansion operators.  For instance, do not code

	{cmd:if (}...{cmd:)} {it:somecommand} {cmd:`++i'}

{p 4 3 2}
Code instead,

	{cmd:if (}...{cmd:)} {cmd:{c -(}}
		{it:somecommand} {cmd:`++i'}
	{cmd:{c )-}}

{pstd}
In the first example, {cmd:i} will be incremented regardless of whether the
condition is true or false because macro expansion occurs before the line is
interpreted.  In the second example, if the condition is false, the line
inside the braces will not be macro expanded and so {cmd:i} will not be
incremented.

{pstd}
The same applies to the {cmd:else} statement; do not code

	{cmd:else} {it:somecommand} {cmd:`++i'}

{pstd}
Code instead,

	{cmd:else {c -(}}
		{it:somecommand} {cmd:`++i'}
	{cmd:{c )-}}

{phang}
{it:Technical Note:}{break}
    What was just said also applies to macro-induced execution of class
    programs that have side effects.  Consider

	    {cmd:if (}...{cmd:)} {it:somecommand} {cmd:`.clspgm.getnext'}

{pmore}
    Class member program {cmd:.getnext} would execute regardless of
    whether the condition were true or false.  In this case, code

	    {cmd:if (}...{cmd:) {c -(}}
		    {it:somecommand} {cmd:`.clspgm.getnext'}
	    {cmd:{c )-}}

{pmore}
    Understand that the problem only arises when macro substitution causes
    the invocation of the class program.  There would be nothing wrong with
    coding

	    {cmd:if (}...{cmd:)} {cmd:`.clspgm.getnext'}
{right:{c TLC}{c TRC}  }
{right:{c BLC}{c BRC}  }

{title:Also see}

{psee}
Manual:  {bf:[P] if}

{psee}
Online:  {helpb continue}, {helpb foreach},
{helpb forvalues}, {helpb while}
{p_end}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -