Electron microscopy
 
IF Statement in DM
- Practical Electron Microscopy and Database -
- An Online Book -
Microanalysis | EM Book                                                                                   http://www.globalsino.com/EM/        


=================================================================================

 

In IF (condition) statement, the condition is an expression that is evaluated to be either true or false. In C++, true is non-zero, while false is zero. If the condition is true, then the statement will execute. On the other hand, if it is false, then the statement will not execute. For instance, see the comparison between true if and false if.

In an IF statement, the Expression is evaluated. If it is "true" (not equal to 0), Statement is evaluated:
         if ( Expression )
         Statement

Sometimes newcomers to DM as well as C++ are confused by the fact that any valid expression can be used to control the "if". That is, the type of expression need not be restricted to only those involving the relational and logical operators or to operands of type bool ("true or false" type of variables). All that is required in the IF statements is that the controlling expression evaluate to either a true or false result. A value of zero is automatically converted into false, and all non-zero values are converted to true. Therefore, any expression that results in a zero or non-zero value can be used to control the IF statements.

Table. 1150a. IF statement in DM.

IF (condition) statement (or called action) A statement to make decisions in a program to run the commands (if some condition is fulfilled) or not. Note that the C++ if statement operates in much the same way that an IF statement operates in any other language. E.g. Example1, Example2, Example3, Example4.  

number
void
{
if }
else
return}

Recursive/cyclic function: Example. Recursive/cyclic function accessible by libraries: Example.  

if ( Expression )
    Statement1
else
    Statement2

If the conditional expression is true (not equal to 0), the target (Statement1) of the "if" will be executed; otherwise, if it exists, the target (Statement2) of the "else" will be executed. At no time will both of them be executed. Example A.

 

IF (condition)
{
action1
action2
}

Make decisions in a program to run the commands or not Example1
Example2

IF (condition)
{
action1
action2
}
ELSE
{
alternative-action1
alternative-action2
}

Make decisions in a program to run the commands or not. {} brackets are used to create blocks of commands.  
else if   Example

IF (condition) action
ELSE alternative-action

Make decisions in a program to run the commands or not. Example1, Example2, Example3.  
if ( ... != ...) Example.  
if ( ... == ...) Example.  
IF (!GetNumber()) Exit(0), or IF (!GetNumber()) Break If not Get Number, then Exit. "IF (!...()) Exit(0)" have similar functions. Example1
Example2
Example3
if (0) or if (1) E.g. Example.  
     

A nested IF is an IF statement which is the target of another IF or an ELSE. Nested IFs are very common in programming. In this case, an ELSE statement always refers to the nearest IF statement that is within the same block as the ELSE and is not already associated with an ELSE. Note that C++ allows at least 256 levels of nesting.

Table. 1150b. Nested IFs statement in DM.

if(i) {
if(j) statement1
if(k) statement2
else statement3
}
else statement4
The else statement3 refers to the nearest IF statement (if(k) statement2), while the "else statement4" is associated with if(i) since they are in the same block. For instance, this addition in Example B, comparing to Example A above, provides the player with feedback about a wrong guess.
number EMi, EMj;
for(EMi=2; EMi<1000; EMi++)
{
for(EMj=2; EMj <= (EMi/EMj); EMj++)
if(!(EMi%EMj)) break; // if factor found, not prime
if(EMj > (EMi/EMj))
result ("The EMi" + "\t" + EMi + "\t" + "is a prime number\n")
}
Script code
Print all prime numbers less than 1000

A common programming construct, which is based on nested IFs, is the IF-ELSE-IF ladder.

Table. 1150c. IF-ELSE-IF ladder in DM.

if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
.
.
.
else
statement;
The expressions are evaluated from the top to the end of the program. As soon as a true condition is found, the statement associated with the condition is executed, and the rest of the ladder is then bypassed. If none of the conditions is true, the final ELSE statement will then be executed. The final ELSE often acts as a default condition; that is, if all other conditional tests fail, then the last ELSE statement is performed..
Example:

number x

for(x=0; x<6; x++)
{
if(x==1)
result ("x is " + x + " in the FOR loop now. \n")

else if(x==2)
result ("x is " + x + " in the FOR loop now. \n")

else if(x==3)
result ("x is " + x + " in the FOR loop now. \n")

else if(x==4)
result ("x is " + x + " in the FOR loop now. \n")

else
result ("x is not in between 1 and 4.\n\n\n\n")

}

DM output:

x is not in between 1 and 4. (Output because all "IF" and "ELSE IF" test fail when x = 0)

x is 1 in the FOR loop now. (Output when x = 1)
x is 2 in the FOR loop now. (Output when x = 2)
x is 3 in the FOR loop now. (Output when x = 3)
x is 4 in the FOR loop now. (Output when x = 4)
x is not in between 1 and 4. (Output when x = 5)

The ? operator can be used to replace if-else statements as shown in Table 1150d.

Table 1150d. Comparison of applications between ? operator and if-else statements.

? operator
if-else statement

Expression1 ? Expression2 : Expression3
----------------------------------
The Expression1, Expression2 and Expression3 are expressions. The value of a ? expression is determined in this way: Expression1 is evaluated. If it is true, then Expression2 is evaluated and becomes the value of the entire ? expression. If Expression1 is false, then Expression3 is evaluated and then its value becomes the value of the expression.

if (condition)
variable = expression1;
else
variable = expression2;
----------------------------------
The value assigned to variable depends on the outcome of the condition controlling the if

Comparison of applications between ? operator and if-else statements 
Script file
Here, x will be assigned the value 0 until round(random() is larger than or equal to 0.5.
Comparison of applications between ? operator and if-else statements 
Script file
Here, x will aslo be assigned the value 0 until round(random() is larger than or equal to 0.5.

 

 

=================================================================================