Electron microscopy
 
Exit, Break and Abort Statements in DM and C++
- Practical Electron Microscopy and Database -
- An Online Book -
Microanalysis | EM Book                                                                                   http://www.globalsino.com/EM/        


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

 

Table 1139a lists the comparisons between exit, break and abort statements in DM and C++.

Table 1139a. Comparisons between exit, break and abort statements in DM and C++.

 
Result from script execution
exit

The function is provided by the C++ library, which stops execution of the current script immediately in both DM and C++ . Does not need the user to press the cancel button to exit, which is different from “Break command”. Exit function is typically used to halt a program when a fatal error has occurred, which renders further execution of the program meaningless or harmful.

Because exit( ) causes immediate termination of the program, it then does not return to the calling program and does not have a return value. However, the value of status is still returned as an exit code to the calling process. In fact, a status value of 0 indicates successful termination. Any other value indicates that the program terminated because of some sort of error.

break A command, for instance, can be used to exit the current loop, e.g. by clicking "cancel" button, in both DM and C++. In this case, it is often used in combination with IF, WHILE or FOR statements. "Break command" is different from “Exit(0) command” which needs the user to press the cancel button to exit. Example1, Example2, Example3.
abort

This function causes the immediate termination of a program in a different way from that of exit function. The function is provided by the C++ library; however, this function is available in C++ only. Unlike exit( ), Abort does not return status information to the operating system, nor does it perform an orderly shutdown of the program.

In DM and C++, it is possible to force an immediate exit from a loop, bypassing the loop’s conditional evaluation, by using the break statement. When the break statement is encountered inside a loop, then the loop is immediately terminated, and program control resumes at the next statement following the loop. In DM, a break statement can exit the smallest enclosing while or for loop.

Table 1139b. Examples of DM scripts.

Script
Result from script execution
number EMx;
for(EMx=0; EMx<100; EMx++)
{
if(EMx==10) break;
result (EMx + "\n")
}
Script code
0
1
2
3
4
5
6
7
8
9
Script function: Loops from 0 to 9 since it will stop at 10.
number EMi, EMj
for(EMi=0; EMi<5; EMi++)
{
for (EMj = 1; EMj < 20; EMj++)
{
result (EMj + "\n")
if(EMj == 10) break
}
}
Script code
Print EMj from 1 to 10 since the program stops at EMj = 5 by the BREAK statement. This printing runs for 5 times since the control is passed back to the outer FOR loop after each printing from 1 to 10.
number EMi
for(EMi=0; EMi<1000; EMi++)
{
if(SpaceDown()) break
result (EMi + "\n")
}
A Space Down can stop the execution of the loop. The script on the right has similar function.
number EMi
for(EMi=0; EMi<1000; EMi++)
{
if(SpaceDown()) exit(0);
result (EMi + "\n")
}

 

 

 

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